You are here

public function Glossary::buildConfigurationForm in Search API AZ Glossary 8.4

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/processor/Glossary.php \Drupal\search_api_glossary\Plugin\search_api\processor\Glossary::buildConfigurationForm()

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/Glossary.php, line 145

Class

Glossary
Adds the item's AZ to the indexed data.

Namespace

Drupal\search_api_glossary\Plugin\search_api\processor

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $fields = $this->index
    ->getFields();
  $form['glossarytable'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Field'),
      $this
        ->t('Glossary Grouping'),
    ],
  ];
  foreach ($fields as $name => $field) {

    // Filter out hidden fields
    // and fields that do not match
    // our required criteria.
    if ($field
      ->isHidden() == FALSE && $this
      ->testType($field
      ->getType()) && $this
      ->checkFieldName($name) == FALSE) {

      // Check the config if the field has been enabled?
      $glossary_fields = $this
        ->getConfig();
      $field_enabled = isset($glossary_fields[$name]['glossary']) ? $glossary_fields[$name]['glossary'] : $this->configuration['field_enabled'];

      // Check the config if the field has been enabled?
      $field_grouping = isset($glossary_fields[$name]['grouping']) ? $glossary_fields[$name]['grouping'] : $this->configuration['grouping_defaults'];
      $form['glossarytable'][$name]['glossary'] = [
        '#type' => 'checkbox',
        '#title' => Html::escape($field
          ->getPrefixedLabel()),
        '#default_value' => $field_enabled,
      ];

      // Finally add the glossary grouping options per field.
      $form['glossarytable'][$name]['grouping'] = [
        '#type' => 'checkboxes',
        '#description' => t('When grouping is enabled, individual values such as 1, 2, 3 will get grouped like "0-9"'),
        '#options' => [
          'grouping_az' => 'Group Alphabetic (A-Z)',
          'grouping_09' => 'Group Numeric (0-9)',
          'grouping_other' => 'Group Non Alpha Numeric (#)',
        ],
        '#default_value' => $field_grouping,
        '#required' => FALSE,
        '#states' => [
          'visible' => [
            [
              ':input[name="processors[glossary][settings][glossarytable][' . $name . '][glossary]"]' => [
                'checked' => TRUE,
              ],
            ],
          ],
        ],
      ];
    }
  }
  return $form;
}