You are here

public function FieldsProcessorPluginBase::buildConfigurationForm in Search API 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

5 calls to FieldsProcessorPluginBase::buildConfigurationForm()
HtmlFilter::buildConfigurationForm in src/Plugin/search_api/processor/HtmlFilter.php
Form constructor.
IgnoreCharacters::buildConfigurationForm in src/Plugin/search_api/processor/IgnoreCharacters.php
Form constructor.
Stemmer::buildConfigurationForm in src/Plugin/search_api/processor/Stemmer.php
Form constructor.
Stopwords::buildConfigurationForm in src/Plugin/search_api/processor/Stopwords.php
Form constructor.
Tokenizer::buildConfigurationForm in src/Plugin/search_api/processor/Tokenizer.php
Form constructor.
5 methods override FieldsProcessorPluginBase::buildConfigurationForm()
HtmlFilter::buildConfigurationForm in src/Plugin/search_api/processor/HtmlFilter.php
Form constructor.
IgnoreCharacters::buildConfigurationForm in src/Plugin/search_api/processor/IgnoreCharacters.php
Form constructor.
Stemmer::buildConfigurationForm in src/Plugin/search_api/processor/Stemmer.php
Form constructor.
Stopwords::buildConfigurationForm in src/Plugin/search_api/processor/Stopwords.php
Form constructor.
Tokenizer::buildConfigurationForm in src/Plugin/search_api/processor/Tokenizer.php
Form constructor.

File

src/Processor/FieldsProcessorPluginBase.php, line 197

Class

FieldsProcessorPluginBase
Provides a base class for processors that work on individual fields.

Namespace

Drupal\search_api\Processor

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $fields = $this->index
    ->getFields();
  $field_options = [];
  $default_fields = [];
  $all_fields = $this->configuration['all_fields'];
  $fields_configured = isset($this->configuration['fields']);
  if ($fields_configured && !$all_fields) {
    $default_fields = $this->configuration['fields'];
  }
  foreach ($fields as $name => $field) {
    if (!$field
      ->isHidden() && $this
      ->testType($field
      ->getType())) {
      $field_options[$name] = Html::escape($field
        ->getPrefixedLabel());
      if ($all_fields || !$fields_configured && $this
        ->testField($name, $field)) {
        $default_fields[] = $name;
      }
    }
  }
  $form['all_fields'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable on all supported fields'),
    '#description' => $this
      ->t('Enable this processor for all supported fields. This will also automatically update the setting when new supported fields are added to the index.'),
    '#default_value' => $all_fields,
  ];

  // Unfortunately, Form API doesn't seem to automatically add the default
  // "#pre_render" callbacks to an element if we set some of our own. We
  // therefore need to explicitly include those, too.
  $pre_render = $this
    ->getElementInfoManager()
    ->getInfoProperty('checkboxes', '#pre_render', []);
  $pre_render[] = [
    static::class,
    'preRenderFieldsCheckboxes',
  ];
  $form['fields'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Enable this processor on the following fields'),
    '#description' => $this
      ->t("Note: The Search API currently doesn't support per-field keywords processing, so this setting will be ignored when preprocessing search keywords. It is therefore usually recommended that you enable the processor for all fields that you intend to use as fulltext search fields, to avoid undesired consequences."),
    '#options' => $field_options,
    '#default_value' => $default_fields,
    '#pre_render' => $pre_render,
  ];
  return $form;
}