1 views_ui_base_views_wizard.php protected ViewsUiBaseViewsWizard::build_filters(&$form, &$form_state)

Build the part of the form that allows the user to select the view's filters.

By default, this adds "of type" and "tagged with" filters (when they are available).

File

core/modules/views_ui/wizards/views_ui_base_views_wizard.php, line 364
Provides the interface and base class for Views Wizard plugins.

Class

ViewsUiBaseViewsWizard
A very generic Views Wizard class - can be constructed for any base table.

Code

protected function build_filters(&$form, &$form_state) {
  // Find all the fields we are allowed to filter by.
  $fields = views_fetch_fields($this->base_table, 'filter');

  $entity_info = $this->entity_info;
  // If the current base table support bundles and has more than one (like user).
  if (isset($entity_info['bundle keys']) && isset($entity_info['bundles'])) {
    // Get all bundles and their human readable names.
    $options = array('all' => t('All'));
    foreach ($entity_info['bundles'] as $type => $bundle) {
      $options[$type] = $bundle['label'];
    }
    $form['displays']['show']['type'] = array(
      '#type' => 'select',
      '#title' => t('of type'),
      '#options' => $options,
    );
    $selected_bundle = views_ui_get_selected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']);
    $form['displays']['show']['type']['#default_value'] = $selected_bundle;
    // Changing this dropdown updates the entire content of $form['displays']
    // via AJAX, since each bundle might have entirely different fields
    // attached to it, etc.
    views_ui_add_ajax_trigger($form['displays']['show'], 'type', array('displays'));
  }

  // Check if we are allowed to filter by taxonomy, and if so, add the
  // "tagged with" filter to the view.
  //
  // We construct this filter using taxonomy_index.tid (which limits the
  // filtering to a specific vocabulary) rather than taxonomy_term_data.name
  // (which matches terms in any vocabulary). This is because it is a more
  // commonly-used filter that works better with the autocomplete UI, and
  // also to avoid confusion with other vocabularies on the site that may
  // have terms with the same name but are not used for free tagging.
  //
  // The downside is that if there *is* more than one vocabulary on the site
  // that is used for free tagging, the wizard will only be able to make the
  // "tagged with" filter apply to one of them (see below for the method it
  // uses to choose).
  if (isset($fields['taxonomy_index.tid'])) {
    // Check if this view will be displaying fieldable entities.
    if (!empty($entity_info['fieldable'])) {
      // Find all "tag-like" taxonomy fields associated with the view's
      // entities. If a particular entity type (i.e., bundle) has been
      // selected above, then we only search for taxonomy fields associated
      // with that bundle. Otherwise, we use all bundles.
      $bundles = array_keys($entity_info['bundles']);
      // Double check that this is a real bundle before using it (since above
      // we added a dummy option 'all' to the bundle list on the form).
      if (isset($selected_bundle) && in_array($selected_bundle, $bundles)) {
        $bundles = array($selected_bundle);
      }
      $tag_fields = array();
      foreach ($bundles as $bundle) {
        foreach (field_info_instances($this->entity_type, $bundle) as $instance) {
          // We define "tag-like" taxonomy fields as ones that use the
          // "Autocomplete term widget (tagging)" widget.
          if ($instance['widget']['type'] == 'taxonomy_autocomplete') {
            $tag_fields[] = $instance['field_name'];
          }
        }
      }
      $tag_fields = array_unique($tag_fields);
      if (!empty($tag_fields)) {
        // If there is more than one "tag-like" taxonomy field available to
        // the view, we can only make our filter apply to one of them (as
        // described above). We choose 'field_tags' if it is available, since
        // that is created by the Standard install profile in core and also
        // commonly used by contrib modules; thus, it is most likely to be
        // associated with the "main" free-tagging vocabulary on the site.
        if (in_array('field_tags', $tag_fields)) {
          $tag_field_name = 'field_tags';
        }
        else {
          $tag_field_name = reset($tag_fields);
        }
        // Add the autocomplete textfield to the wizard.
        $form['displays']['show']['tagged_with'] = array(
          '#type' => 'textfield',
          '#title' => t('tagged with'),
          '#autocomplete_path' => 'taxonomy/autocomplete/' . $tag_field_name,
          '#size' => 30,
          '#maxlength' => 1024,
          '#field_name' => $tag_field_name,
          '#element_validate' => array('views_ui_taxonomy_autocomplete_validate'),
        );
      }
    }
  }
}