You are here

public function AddHierarchy::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

File

src/Plugin/search_api/processor/AddHierarchy.php, line 211

Class

AddHierarchy
Adds all ancestors' IDs to a hierarchical field.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function buildConfigurationForm(array $form, FormStateInterface $formState) {
  $form['#description'] = $this
    ->t('Select the fields to which hierarchical data should be added.');
  foreach ($this
    ->getHierarchyFields() as $field_id => $options) {
    $enabled = !empty($this->configuration['fields'][$field_id]);
    $form['fields'][$field_id]['status'] = [
      '#type' => 'checkbox',
      '#title' => $this->index
        ->getField($field_id)
        ->getLabel(),
      '#default_value' => $enabled,
    ];
    reset($options);
    $form['fields'][$field_id]['property'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Hierarchy property to use'),
      '#description' => $this
        ->t("This field has several nested properties which look like they might contain hierarchy data for the field. Please pick the one that should be used."),
      '#options' => $options,
      '#default_value' => $enabled ? $this->configuration['fields'][$field_id] : key($options),
      '#access' => count($options) > 1,
      '#states' => [
        'visible' => [
          // @todo This shouldn't be dependent on the form array structure.
          //   Use the '#process' trick instead.
          ":input[name=\"processors[hierarchy][settings][fields][{$field_id}][status]\"]" => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  return $form;
}