function EntityReference::buildExtraOptionsForm

Provide a form for setting options.

Parameters

array $form: An alterable, associative array containing the structure of the form, passed by reference.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides HandlerBase::buildExtraOptionsForm

File

core/modules/views/src/Plugin/views/filter/EntityReference.php, line 189

Class

EntityReference
Filters a view by entity references.

Namespace

Drupal\views\Plugin\views\filter

Code

public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) : void {
  $form['sub_handler'] = [
    '#type' => 'select',
    '#title' => $this->t('Reference method'),
    '#options' => $this->getSubHandlerOptions(),
    '#default_value' => $this->options['sub_handler'],
    '#required' => TRUE,
  ];
  // We store the settings from any sub handler in sub_handler_settings, but
  // in this form, we have multiple sub handlers conditionally displayed.
  // Copy the active sub_handler_settings into the handler specific settings
  // to set the defaults to match the saved options on build.
  if (!empty($this->options['sub_handler']) && !empty($this->options['sub_handler_settings'])) {
    $this->options[static::SUBFORM_PREFIX . $this->options['sub_handler']] = $this->options['sub_handler_settings'];
  }
  foreach ($this->getSubHandlerOptions() as $sub_handler => $sub_handler_label) {
    $subform_key = static::SUBFORM_PREFIX . $sub_handler;
    $subform = [
      '#type' => 'fieldset',
      '#title' => $this->t('Reference type "@type"', [
        '@type' => $sub_handler_label,
      ]),
      '#tree' => TRUE,
      '#parents' => [
        'options',
        $subform_key,
      ],
      // Make the sub handler settings conditional on the selected selection
      // handler.
'#states' => [
        'visible' => [
          'select[name="options[sub_handler]"]' => [
            'value' => $sub_handler,
          ],
        ],
      ],
    ];
    // Build the sub form and sub for state.
    $selection_handler = $this->getSelectionHandler($sub_handler);
    if (!empty($this->options[$subform_key])) {
      $selection_config = $selection_handler->getConfiguration();
      $selection_config = NestedArray::mergeDeepArray([
        $selection_config,
        $this->options[$subform_key],
      ], TRUE);
      $selection_handler->setConfiguration($selection_config);
    }
    $subform_state = SubformState::createForSubform($subform, $form, $form_state);
    $sub_handler_settings = $selection_handler->buildConfigurationForm($subform, $subform_state);
    if ($selection_handler instanceof ViewsSelection) {
      if (isset($sub_handler_settings['view']['no_view_help'])) {
        // If there are no views with entity reference displays,
        // ViewsSelection still validates the view.
        // This will prevent form config extra form submission,
        // so we remove it here.
        unset($sub_handler_settings['view']['#element_validate']);
      }
    }
    else {
      // Remove unnecessary and inappropriate handler settings from the
      // filter config form.
      $sub_handler_settings['target_bundles_update']['#access'] = FALSE;
      $sub_handler_settings['auto_create']['#access'] = FALSE;
      $sub_handler_settings['auto_create_bundle']['#access'] = FALSE;
    }
    $subform = NestedArray::mergeDeepArray([
      $subform,
      $sub_handler_settings,
    ], TRUE);
    $form[$subform_key] = $subform;
    $this->cleanUpSubformChildren($form[$subform_key]);
  }
  $form['widget'] = [
    '#type' => 'radios',
    '#title' => $this->t('Selection type'),
    '#default_value' => $this->options['widget'],
    '#options' => [
      static::WIDGET_SELECT => $this->t('Select list'),
      static::WIDGET_AUTOCOMPLETE => $this->t('Autocomplete'),
    ],
    '#description' => $this->t('For performance and UX reasons, the maximum count of selectable entities for the "Select list" selection type is limited to @count. If more is expected, select "Autocomplete" instead.', [
      '@count' => static::WIDGET_SELECT_LIMIT,
    ]),
  ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.