You are here

public function SearchApiEntityField::buildOptionsForm in Search API 8

Provide a form to edit options for this plugin.

Parameters

array|\ArrayAccess $form: The existing form structure, passed by reference.

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

Overrides SearchApiFieldTrait::buildOptionsForm

See also

\Drupal\views\Plugin\views\ViewsPluginInterface::buildOptionsForm()

File

src/Plugin/views/field/SearchApiEntityField.php, line 165

Class

SearchApiEntityField
Displays entity field data.

Namespace

Drupal\search_api\Plugin\views\field

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  $form['field_rendering'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use entity field rendering'),
    '#description' => $this
      ->t("If checked, Drupal's built-in field rendering mechanism will be used for rendering this field's values, which requires the entity to be loaded. If unchecked, a type-specific, entity-independent rendering mechanism will be used."),
    '#default_value' => $this->options['field_rendering'],
  ];

  // Wrap the (immediate) parent options in their own field set, to clean up
  // the UI when (un)checking the above checkbox.
  $form['parent_options'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Render settings'),
    '#states' => [
      'visible' => [
        ':input[name="options[field_rendering]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Include the parent options form and move all fields that were added by
  // our direct parent (\Drupal\views\Plugin\views\field\Field) to the
  // "parent_options" fieldset.
  parent::buildOptionsForm($form, $form_state);
  $parent_keys = $this
    ->getParentOptionKeys(TRUE);
  foreach ($parent_keys as $key) {
    if (!empty($form[$key])) {
      $form[$key]['#fieldset'] = 'parent_options';
    }
  }

  // The Core boolean formatter hard-codes the field name to "field_boolean".
  // This breaks the parent class's call of rewriteStatesSelector() for fixing
  // "#states". We therefore apply that behavior again here.
  if (!empty($form['settings'])) {
    FormHelper::rewriteStatesSelector($form['settings'], "fields[field_boolean][settings_edit_form]", 'options');
  }

  // Get the options form for the fallback handler.
  $fallback_form = [];
  $this->fallbackHandler
    ->buildOptionsForm($fallback_form, $form_state);

  // Remove all fields from FieldPluginBase from the fallback form, but leave
  // those in that were only added by our immediate parent,
  // \Drupal\views\Plugin\views\field\Field. (For example, the "type" option
  // is especially prone to conflicts here.) The others come from the plugin
  // base classes and will be identical, so it would be confusing to include
  // them twice.
  $parent_keys[] = '#pre_render';
  $remove_from_fallback = array_diff_key($form, array_flip($parent_keys));
  $fallback_form = array_diff_key($fallback_form, $remove_from_fallback);

  // Fix the "#states" selectors in the fallback form, and put an additional
  // "#states" directive on it to only be visible for the corresponding
  // "field_rendering" setting.
  if ($fallback_form) {
    FormHelper::rewriteStatesSelector($fallback_form, '"options[', '"options[fallback_options][');
    $form['fallback_options'] = $fallback_form;
    $form['fallback_options']['#type'] = 'fieldset';
    $form['fallback_options']['#title'] = $this
      ->t('Render settings');
    $form['fallback_options']['#states']['visible'][':input[name="options[field_rendering]"]'] = [
      'checked' => FALSE,
    ];
  }
}