class ViewfieldWidgetSelect in Viewfield 8.3
Plugin implementation of the 'viewfield_select' widget.
Plugin annotation
@FieldWidget(
  id = "viewfield_select",
  label = @Translation("Viewfield"),
  field_types = {"viewfield"}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface uses AllowedTagsXssTrait- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget- class \Drupal\viewfield\Plugin\Field\FieldWidget\ViewfieldWidgetSelect
 
 
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget
 
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase
 
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface uses AllowedTagsXssTrait
 
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
 
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ViewfieldWidgetSelect
File
- src/Plugin/ Field/ FieldWidget/ ViewfieldWidgetSelect.php, line 22 
Namespace
Drupal\viewfield\Plugin\Field\FieldWidgetView source
class ViewfieldWidgetSelect extends OptionsSelectWidget {
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $field_type = $this->fieldDefinition
      ->getType();
    $item = $items[$delta];
    $element['target_id'] = parent::formElement($items, $delta, $element, $form, $form_state);
    $element['target_id']['#field_type'] = $field_type;
    $element['target_id']['#field_item'] = $item;
    $element['target_id']['#description'] = $this
      ->t('View name.');
    $element['target_id']['#ajax'] = [
      'callback' => [
        $this,
        'ajaxGetDisplayOptions',
      ],
      'event' => 'change',
      'progress' => [
        'type' => 'throbber',
        'message' => $this
          ->t('Retrieving view displays.'),
      ],
    ];
    // Set up options for allowed views.
    $empty_label = $this
      ->getEmptyLabel() ?: $this
      ->t('- None -');
    // Always allow '_none' for non-required fields or second and greater delta.
    $none_option = !$this->fieldDefinition
      ->isRequired() || $delta > 0 ? [
      '_none' => $empty_label,
    ] : [];
    $element['target_id']['#options'] = array_merge($none_option, $item
      ->getViewOptions());
    $element['target_id']['#multiple'] = FALSE;
    // Build an array of keys to retrieve values from $form_state.
    $form_state_keys = [
      $this->fieldDefinition
        ->getName(),
      $delta,
    ];
    if (!empty($form['#parents'])) {
      $form_state_keys = array_merge($form['#parents'], $form_state_keys);
    }
    // Assign default values.
    $default_target_id = NULL;
    $display_id_options = NULL;
    $default_display_id = NULL;
    $default_arguments = NULL;
    $default_items_to_display = NULL;
    $item_value = $item
      ->getValue();
    // Set initial default values from the item if they exist.
    if (isset($item_value['target_id'])) {
      $default_target_id = $item_value['target_id'];
      $display_id_options = $item
        ->getDisplayOptions($item_value['target_id']);
      $default_display_id = $item_value['display_id'];
      $default_arguments = $item_value['arguments'];
      $default_items_to_display = $item_value['items_to_display'];
    }
    // Set default values based on values from form_state, if they exist.
    $values = $form_state
      ->getValue($this->fieldDefinition
      ->getName());
    if (!empty($values[$delta])) {
      $default_target_id = $values[$delta]['target_id'];
      $default_display_id = $values[$delta]['display_id'];
      $default_arguments = $values[$delta]['arguments'];
      $display_id_options = $item
        ->getDisplayOptions($default_target_id);
      $default_items_to_display = $values[$delta]['items_to_display'];
    }
    // #default_value needs special handling, otherwise it consists of an array
    // of values corresponding to field items, one for each #delta.
    $element['target_id']['#default_value'] = $default_target_id;
    // Construct CSS class to target ajax callback.
    $display_id_class = $this
      ->createDisplayClass($form_state_keys);
    // Use primary target_id field to control visibility of secondary ones.
    $field_key = $form_state_keys[0] . '[' . implode('][', array_slice($form_state_keys, 1)) . ']';
    $primary_field_name = $field_key . '[target_id]';
    $primary_field_visible_test = [
      ':input[name="' . $primary_field_name . '"]' => [
        '!value' => '_none',
      ],
    ];
    $element['display_id'] = [
      '#title' => $this
        ->t('Display'),
      '#type' => 'select',
      '#options' => $display_id_options,
      '#default_value' => $default_display_id,
      '#description' => $this
        ->t('View display to be used.'),
      '#attributes' => [
        'class' => [
          $display_id_class,
        ],
      ],
      '#weight' => 10,
      '#states' => [
        'visible' => $primary_field_visible_test,
      ],
    ];
    $element['view_options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced options'),
      '#weight' => 20,
      '#open' => false,
      '#parents' => $form_state_keys,
      '#states' => [
        'open' => [
          [
            ':input[name="' . $field_key . '[arguments]"]' => [
              'filled' => TRUE,
            ],
          ],
          [
            ':input[name="' . $field_key . '[items_to_display]"]' => [
              'filled' => TRUE,
            ],
          ],
        ],
        'visible' => $primary_field_visible_test,
      ],
    ];
    $element['view_options']['arguments'] = [
      '#title' => $this
        ->t('Arguments'),
      '#type' => 'textfield',
      '#default_value' => $default_arguments,
      '#description' => $this
        ->t('Separate contextual filters with a "/". Each filter may use "+" or "," for multi-value arguments.<br>This field supports tokens.'),
      '#weight' => 20,
      '#states' => [
        'visible' => $primary_field_visible_test,
      ],
      '#maxlength' => 255,
    ];
    // Get entity types from the $items.
    $entity_type = $items
      ->getEntity()
      ->getEntityTypeId();
    // Token expects the value "term" for taxonomy_term entities.
    if ($entity_type == 'taxonomy_term') {
      $entity_type = 'term';
    }
    $element['view_options']['token_help'] = [
      '#type' => 'item',
      '#weight' => 30,
      '#states' => [
        'visible' => $primary_field_visible_test,
      ],
      'tokens' => [
        '#theme' => 'token_tree_link',
        '#token_types' => [
          $entity_type,
        ],
      ],
    ];
    $element['view_options']['items_to_display'] = [
      '#title' => $this
        ->t('Items to display'),
      '#type' => 'textfield',
      '#default_value' => $default_items_to_display,
      '#description' => $this
        ->t('Override the number of items to display. This also disables the pager if one is configured. Leave empty for default limit.'),
      '#weight' => 40,
      '#states' => [
        'visible' => $primary_field_visible_test,
      ],
    ];
    $element['#attached']['library'][] = 'viewfield/viewfield';
    return $element;
  }
  /**
   * {@inheritdoc}
   */
  protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
    $elements = parent::formMultipleElements($items, $form, $form_state);
    // Must always show fields on configuration form.
    if (!$this
      ->isDefaultValueWidget($form_state) && $this
      ->getFieldSetting('force_default')) {
      $elements['#access'] = FALSE;
    }
    $max_delta = $elements['#max_delta'];
    $is_multiple = $elements['#cardinality_multiple'];
    if ($is_multiple) {
      for ($delta = 0; $delta <= $max_delta; $delta++) {
        $element =& $elements[$delta];
        // Change title to 'View #'.
        $element['target_id']['#title'] = $this
          ->t('View @number', [
          '@number' => $delta + 1,
        ]);
        // Force title display.
        $element['target_id']['#title_display'] = 'before';
      }
    }
    else {
      // $max_delta == 0 for this case.
      $element =& $elements[0];
      // Change title to simply 'View'.
      $element['target_id']['#title'] = $this
        ->t('View');
      // Wrap single values in a fieldset unless on the default settings form,
      // as long as the field is visible (!force_default).
      if (!$this
        ->isDefaultValueWidget($form_state) && !$this
        ->getFieldSetting('force_default')) {
        $element += [
          '#type' => 'fieldset',
          '#title' => $this->fieldDefinition
            ->getLabel(),
        ];
      }
    }
    return $elements;
  }
  /**
   * Overridden form validation handler for widget elements.
   *
   * Save selected target_id as a single item, since there will be at most one.
   * This prevents the value from being deeply nested inside $form_state.
   *
   * @param array $element
   *   The form element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @see OptionsWidgetBase::validateElement()
   */
  public static function validateElement(array $element, FormStateInterface $form_state) {
    if ($element['#required'] && $element['#value'] == '_none') {
      $form_state
        ->setError($element, t('@name field is required.', [
        '@name' => $element['#title'],
      ]));
    }
    // Massage submitted form values.
    // Drupal\Core\Field\WidgetBase::submit() expects values as
    // an array of values keyed by delta first, then by column, while our
    // widgets return the opposite.
    if (is_array($element['#value'])) {
      $values = array_values($element['#value']);
    }
    else {
      $values = [
        $element['#value'],
      ];
    }
    // Filter out the 'none' option. Use a strict comparison, because
    // 0 == 'any string'.
    $index = array_search('_none', $values, TRUE);
    if ($index !== FALSE) {
      unset($values[$index]);
    }
    $target_id = !empty($values[0]) ? $values[0] : NULL;
    $form_state
      ->setValueForElement($element, $target_id);
  }
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    $values = parent::massageFormValues($values, $form, $form_state);
    // TODO: Change the autogenerated stub
    foreach ($values as $key => $value) {
    }
    return $values;
  }
  /**
   * {@inheritdoc}
   */
  public function errorElement(array $element, ConstraintViolationInterface $error, array $form, FormStateInterface $form_state) {
    $element = $element;
    return $element['target_id'];
  }
  /**
   * Ajax callback to retrieve display IDs.
   *
   * @param array $form
   *   The form from which the display IDs are being requested.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state of the form.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The Ajax response.
   */
  public function ajaxGetDisplayOptions(array &$form, FormStateInterface $form_state) {
    $trigger = $form_state
      ->getTriggeringElement();
    $form_state_keys = array_slice($trigger['#parents'], 0, -1);
    $form_state_value = $form_state
      ->getValue($form_state_keys);
    $display_options = $trigger['#field_item']
      ->getDisplayOptions($form_state_value['target_id']);
    $html = '';
    foreach ($display_options as $key => $value) {
      $html .= '<option value="' . $key . '">' . $value . '</option>';
    }
    $html = '<optgroup>' . $html . '</optgroup>';
    // Create a class selector for Ajax response.
    $selector = '.' . $this
      ->createDisplayClass($form_state_keys);
    $response = new AjaxResponse();
    $response
      ->addCommand(new HtmlCommand($selector, $html));
    $response
      ->addCommand(new InvokeCommand($selector, 'ajaxEnableElements'));
    $form_state
      ->setRebuild(TRUE);
    return $response;
  }
  /**
   * Produce a class for a display input field.
   *
   * @param array $components
   *   An array of class components to be concatenated.
   *
   * @return string
   *   The display input field class.
   */
  protected function createDisplayClass(array $components) {
    return implode('-', $components) . '-display-id';
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| AllowedTagsXssTrait:: | public | function | Returns a list of tags allowed by AllowedTagsXssTrait::fieldFilterXss(). | |
| AllowedTagsXssTrait:: | public | function | Returns a human-readable list of allowed tags for display in help texts. | |
| AllowedTagsXssTrait:: | public | function | Filters an HTML string to prevent XSS vulnerabilities. | |
| DependencySerializationTrait:: | protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| DependencySerializationTrait:: | protected | property | An array of service IDs keyed by property name used for serialization. | |
| DependencySerializationTrait:: | public | function | 1 | |
| DependencySerializationTrait:: | public | function | 2 | |
| MessengerTrait:: | protected | property | The messenger. | 29 | 
| MessengerTrait:: | public | function | Gets the messenger. | 29 | 
| MessengerTrait:: | public | function | Sets the messenger. | |
| OptionsSelectWidget:: | protected | function | Returns the empty option label to add to the list of options, if any. Overrides OptionsWidgetBase:: | |
| OptionsSelectWidget:: | protected | function | Sanitizes a string label to display as an option. Overrides OptionsWidgetBase:: | |
| OptionsSelectWidget:: | protected | function | Indicates whether the widgets support optgroups. Overrides OptionsWidgetBase:: | |
| OptionsWidgetBase:: | protected | property | Abstract over the actual field columns, to allow different field types to reuse those widgets. | |
| OptionsWidgetBase:: | protected | function | Returns the array of options for the widget. | |
| OptionsWidgetBase:: | protected | function | Determines selected options from the incoming field values. | |
| OptionsWidgetBase:: | public | function | Constructs a WidgetBase object. Overrides WidgetBase:: | 1 | 
| PluginBase:: | protected | property | Configuration information passed into the plugin. | 1 | 
| PluginBase:: | protected | property | The plugin implementation definition. | 1 | 
| PluginBase:: | protected | property | The plugin_id. | |
| PluginBase:: | constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
| PluginBase:: | public | function | Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: | 3 | 
| PluginBase:: | public | function | Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: | |
| PluginBase:: | public | function | Determines if the plugin is configurable. | |
| PluginSettingsBase:: | protected | property | Whether default settings have been merged into the current $settings. | |
| PluginSettingsBase:: | protected | property | The plugin settings injected by third party modules. | |
| PluginSettingsBase:: | public | function | Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: | 6 | 
| PluginSettingsBase:: | public static | function | Defines the default settings for this plugin. Overrides PluginSettingsInterface:: | 42 | 
| PluginSettingsBase:: | public | function | Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: | |
| PluginSettingsBase:: | public | function | Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: | |
| PluginSettingsBase:: | public | function | Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: | |
| PluginSettingsBase:: | public | function | Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: | |
| PluginSettingsBase:: | public | function | Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: | |
| PluginSettingsBase:: | protected | function | Merges default settings values into $settings. | |
| PluginSettingsBase:: | public | function | Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: | 3 | 
| PluginSettingsBase:: | public | function | Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: | |
| PluginSettingsBase:: | public | function | Sets the settings for the plugin. Overrides PluginSettingsInterface:: | |
| PluginSettingsBase:: | public | function | Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: | |
| PluginSettingsBase:: | public | function | Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: | |
| StringTranslationTrait:: | protected | property | The string translation service. | 1 | 
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | |
| ViewfieldWidgetSelect:: | public | function | Ajax callback to retrieve display IDs. | |
| ViewfieldWidgetSelect:: | protected | function | Produce a class for a display input field. | |
| ViewfieldWidgetSelect:: | public | function | Assigns a field-level validation error to the right widget sub-element. Overrides WidgetBase:: | |
| ViewfieldWidgetSelect:: | public | function | Returns the form for a single field widget. Overrides OptionsSelectWidget:: | |
| ViewfieldWidgetSelect:: | protected | function | Special handling to create form elements for multiple values. Overrides WidgetBase:: | |
| ViewfieldWidgetSelect:: | public | function | Massages the form values into the format expected for field values. Overrides WidgetBase:: | |
| ViewfieldWidgetSelect:: | public static | function | Overridden form validation handler for widget elements. Overrides OptionsWidgetBase:: | |
| WidgetBase:: | protected | property | The field definition. | |
| WidgetBase:: | protected | property | The widget settings. Overrides PluginSettingsBase:: | |
| WidgetBase:: | public static | function | Ajax callback for the "Add another item" button. | |
| WidgetBase:: | public static | function | Submission handler for the "Add another item" button. | |
| WidgetBase:: | public static | function | After-build handler for field elements in a form. | |
| WidgetBase:: | public static | function | Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: | 5 | 
| WidgetBase:: | public | function | Extracts field values from submitted form values. Overrides WidgetBaseInterface:: | 2 | 
| WidgetBase:: | public | function | Reports field-level validation errors against actual form elements. Overrides WidgetBaseInterface:: | 2 | 
| WidgetBase:: | public | function | Creates a form element for a field. Overrides WidgetBaseInterface:: | 3 | 
| WidgetBase:: | protected | function | Generates the form element for a single copy of the widget. | |
| WidgetBase:: | protected | function | Returns the value of a field setting. | |
| WidgetBase:: | protected | function | Returns the array of field settings. | |
| WidgetBase:: | protected | function | Returns the filtered field description. | |
| WidgetBase:: | public static | function | Retrieves processing information about the widget from $form_state. Overrides WidgetBaseInterface:: | |
| WidgetBase:: | protected static | function | Returns the location of processing information within $form_state. | |
| WidgetBase:: | protected | function | Returns whether the widget handles multiple values. | |
| WidgetBase:: | public static | function | Returns if the widget can be used for the provided field. Overrides WidgetInterface:: | 4 | 
| WidgetBase:: | protected | function | Returns whether the widget used for default value form. | |
| WidgetBase:: | public | function | Returns a form to configure settings for the widget. Overrides WidgetInterface:: | 16 | 
| WidgetBase:: | public | function | Returns a short summary for the current widget settings. Overrides WidgetInterface:: | 15 | 
| WidgetBase:: | public static | function | Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface:: | 
