Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function form(array $form, FormStateInterface $form_state)
 {
     $form['#attached']['css'] = static::getAdminCSS();
     $form['#attached']['js'][] = drupal_get_path('module', 'views_ui') . '/js/views-admin.js';
     $form['#attributes']['class'] = array('views-admin');
     $form['name'] = array('#type' => 'fieldset', '#title' => t('View basic information'), '#attributes' => array('class' => array('fieldset-no-legend')));
     $form['name']['label'] = array('#type' => 'textfield', '#title' => $this->t('View name'), '#required' => TRUE, '#size' => 32, '#default_value' => '', '#maxlength' => 255);
     $form['name']['id'] = array('#type' => 'machine_name', '#maxlength' => 128, '#machine_name' => array('exists' => '\\Drupal\\views\\Views::getView', 'source' => array('name', 'label')), '#description' => $this->t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'));
     $form['name']['description_enable'] = array('#type' => 'checkbox', '#title' => $this->t('Description'));
     $form['name']['description'] = array('#type' => 'textfield', '#title' => $this->t('Provide description'), '#title_display' => 'invisible', '#size' => 64, '#default_value' => '', '#states' => array('visible' => array(':input[name="description_enable"]' => array('checked' => TRUE))));
     // Create a wrapper for the entire dynamic portion of the form. Everything
     // that can be updated by AJAX goes somewhere inside here. For example, this
     // is needed by "Show" dropdown (below); it changes the base table of the
     // view and therefore potentially requires all options on the form to be
     // dynamically updated.
     $form['displays'] = array();
     // Create the part of the form that allows the user to select the basic
     // properties of what the view will display.
     $form['displays']['show'] = array('#type' => 'fieldset', '#title' => t('View settings'), '#tree' => TRUE, '#attributes' => array('class' => array('container-inline')));
     // Create the "Show" dropdown, which allows the base table of the view to be
     // selected.
     $wizard_plugins = $this->wizardManager->getDefinitions();
     $options = array();
     foreach ($wizard_plugins as $key => $wizard) {
         $options[$key] = $wizard['title'];
     }
     $form['displays']['show']['wizard_key'] = array('#type' => 'select', '#title' => $this->t('Show'), '#options' => $options);
     $show_form =& $form['displays']['show'];
     $default_value = \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'users';
     $show_form['wizard_key']['#default_value'] = WizardPluginBase::getSelected($form_state, array('show', 'wizard_key'), $default_value, $show_form['wizard_key']);
     // Changing this dropdown updates the entire content of $form['displays'] via
     // AJAX.
     views_ui_add_ajax_trigger($show_form, 'wizard_key', array('displays'));
     // Build the rest of the form based on the currently selected wizard plugin.
     $wizard_key = $show_form['wizard_key']['#default_value'];
     $wizard_instance = $this->wizardManager->createInstance($wizard_key);
     $form = $wizard_instance->buildForm($form, $form_state);
     return $form;
 }
  /**
   * 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).
   */
  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'),
          );
        }
      }
    }
  }
 /**
  * Builds the form structure for selecting the view's filters.
  *
  * By default, this adds "of type" and "tagged with" filters (when they are
  * available).
  */
 protected function buildFilters(&$form, FormStateInterface $form_state)
 {
     module_load_include('inc', 'views_ui', 'admin');
     $bundles = entity_get_bundles($this->entityTypeId);
     // If the current base table support bundles and has more than one (like user).
     if (!empty($bundles) && $this->entityType && $this->entityType->hasKey('bundle')) {
         // Get all bundles and their human readable names.
         $options = array('all' => $this->t('All'));
         foreach ($bundles as $type => $bundle) {
             $options[$type] = $bundle['label'];
         }
         $form['displays']['show']['type'] = array('#type' => 'select', '#title' => $this->t('of type'), '#options' => $options);
         $selected_bundle = static::getSelected($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'));
     }
 }