You are here

function term_reference_filter_by_views_form_field_ui_field_edit_form_alter in Taxonomy Term Reference Filter by Views 7.2

Same name and namespace in other branches
  1. 7 term_reference_filter_by_views.module \term_reference_filter_by_views_form_field_ui_field_edit_form_alter()

File

./term_reference_filter_by_views.module, line 111

Code

function term_reference_filter_by_views_form_field_ui_field_edit_form_alter(&$form) {
  if ($form['#field']['type'] == 'taxonomy_term_reference') {
    $field = $form['#field'];
    $view_settings = empty($form['#instance']['view']) ? '' : $form['#instance']['view'];
    $displays = views_get_applicable_views('term_reference display');

    // Filter views that list the terms we want, and group the separate
    // displays by view.
    $entity_info = entity_get_info('taxonomy_term');
    $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;
      }
    }
    if ($options) {

      // 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).
      $options = array(
        '' => '<' . t('none') . '>',
      ) + $options;
      $default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : '';
      $form['instance']['view'] = array(
        '#type' => 'fieldset',
        '#title' => t('Filter by Views settings'),
        '#element_validate' => array(
          'term_reference_view_settings_validate',
        ),
      );
      $form['instance']['view']['view_and_display'] = array(
        '#type' => 'select',
        '#title' => t('View used to select the entities'),
        '#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 "Term Reference" are eligible.') . '</p>',
      );
      $default = !empty($view_settings['args']) ? implode(', ', $view_settings['args']) : '';
      $form['instance']['view']['args'] = 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.'),
      );
      $form['instance']['view']['match_operator'] = array(
        '#type' => 'select',
        '#title' => t('Autocomplete matching'),
        '#default_value' => isset($view_settings['match_operator']) ? $view_settings['match_operator'] : 'CONTAINS',
        '#options' => array(
          'STARTS_WITH' => t('Starts with'),
          'CONTAINS' => t('Contains'),
        ),
        '#description' => t('Select the method used to collect autocomplete suggestions. Note that <em>Contains</em> can cause performance issues on sites with thousands of terms.'),
      );
      $form['instance']['view']['size'] = array(
        '#type' => 'textfield',
        '#title' => t('Size of textfield'),
        '#default_value' => isset($view_settings['size']) ? $view_settings['size'] : NULL,
        '#element_validate' => array(
          '_element_validate_integer_positive',
        ),
      );
      if (!empty($view_settings['view_name']) && isset($form['instance']['widget']['settings']['autocomplete_match'])) {
        $form['instance']['widget']['settings']['autocomplete_match']['#access'] = FALSE;
      }
    }
    else {
      $form['instance']['view']['no_view_help'] = array(
        '#markup' => '<p>' . t('No eligible views were found. <a href="@create">Create a view</a> with an <em>Term 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>',
      );
    }
  }
}