public static function ViewsSelection::settingsForm

Implements \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::settingsForm().

Overrides SelectionInterface::settingsForm

File

drupal/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php, line 48
Contains \Drupal\views\Plugin\entity_reference\selection\ViewsSelection.

Class

ViewsSelection
Plugin implementation of the 'selection' entity_reference.

Namespace

Drupal\views\Plugin\entity_reference\selection

Code

public static function settingsForm(&$field, &$instance) {
  $view_settings = empty($instance['settings']['handler_settings']['view']) ? array() : $instance['settings']['handler_settings']['view'];
  $displays = views_get_applicable_views('entity_reference_display');

  // Filter views that list the entity type we want, and group the separate
  // displays by view.
  $entity_info = entity_get_info($field['settings']['target_type']);
  $options = array();
  foreach ($displays as $data) {
    list($view, $display_id) = $data;
    if ($view->storage
      ->get('base_table') == $entity_info['base_table']) {
      $name = $view->storage
        ->get('id');
      $display = $view->storage
        ->get('display');
      $options[$name . ':' . $display_id] = $name . ' - ' . $display[$display_id]['display_title'];
    }
  }

  // The value of the 'view_and_display' select below will need to be split
  // into 'view_name' and 'view_display' in the final submitted values, so
  // we massage the data at validate time on the wrapping element (not
  // ideal).
  $plugin = new static($field, $instance);
  $form['view']['#element_validate'] = array(
    array(
      $plugin,
      'settingsFormValidate',
    ),
  );
  if ($options) {
    $default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL;
    $form['view']['view_and_display'] = array(
      '#type' => 'select',
      '#title' => t('View used to select the entities'),
      '#required' => TRUE,
      '#options' => $options,
      '#default_value' => $default,
      '#description' => '<p>' . t('Choose the view and display that select the entities that can be referenced.<br />Only views with a display of type "Entity Reference" are eligible.') . '</p>',
    );
    $default = !empty($view_settings['arguments']) ? implode(', ', $view_settings['arguments']) : '';
    $form['view']['arguments'] = array(
      '#type' => 'textfield',
      '#title' => t('View arguments'),
      '#default_value' => $default,
      '#required' => FALSE,
      '#description' => t('Provide a comma separated list of arguments to pass to the view.'),
    );
  }
  else {
    $form['view']['no_view_help'] = array(
      '#markup' => '<p>' . t('No eligible views were found. <a href="@create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href="@existing">existing view</a>.', array(
        '@create' => url('admin/structure/views/add'),
        '@existing' => url('admin/structure/views'),
      )) . '</p>',
    );
  }
  return $form;
}