1 EntityReferenceSelectionHandlerViews.inc public static EntityReferenceSelectionHandlerViews::settingsForm($field, $instance)

Implements EntityReferenceHandler::settingsForm().

Overrides EntityReferenceSelectionHandlerInterface::settingsForm

File

core/modules/entityreference/plugins/selection/EntityReferenceSelectionHandlerViews.inc, line 32

Class

EntityReferenceSelectionHandlerViews
Entity handler for Views.

Code

public static function settingsForm($field, $instance) {
  $view_settings = empty($field['settings']['handler_settings']['view']) ? '' : $field['settings']['handler_settings']['view'];
  $displays = views_get_applicable_views('entityreference 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->base_table == $entity_info['base table']) {
      $options[$view->name . ':' . $display_id] = $view->name . ' - ' . $view->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
  // massage the data at validate time on the wrapping element (not
  // ideal).
  $form['view']['#element_validate'] = array('entityreference_view_settings_validate');

  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['args']) ? implode(', ', $view_settings['args']) : '';
    $description = t('Provide a comma separated list of arguments to pass to the view.') . '<br />' . t('This field supports tokens.');

    $form['view']['args'] = array(
      '#type' => 'textfield',
      '#title' => t('View arguments'),
      '#default_value' => $default,
      '#required' => FALSE,
      '#description' => $description,
      '#maxlength' => '512',
    );
    // Get the token type for the entity type our field is in (a type 'taxonomy_term' has a 'term' type token).
    $info = entity_get_info($instance['entity_type']);

    $form['view']['tokens'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array($info['token type']),
      '#global_types' => TRUE,
      '#click_insert' => TRUE,
      '#dialog' => TRUE,
    );
  }
  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>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;
}