You are here

public function IndexProcessorsForm::form in Search API 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/IndexProcessorsForm.php, line 98

Class

IndexProcessorsForm
Provides a form for configuring the processors of a search index.

Namespace

Drupal\search_api\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css';

  // Retrieve lists of all processors, and the stages and weights they have.
  if (!$form_state
    ->has('processors')) {
    $all_processors = $this
      ->getAllProcessors();
    $sort_processors = function (ProcessorInterface $a, ProcessorInterface $b) {
      return strnatcasecmp($a
        ->label(), $b
        ->label());
    };
    uasort($all_processors, $sort_processors);
    $form_state
      ->set('processors', $all_processors);
  }
  else {
    $all_processors = $form_state
      ->get('processors');
  }
  $stages = $this->processorPluginManager
    ->getProcessingStages();

  /** @var \Drupal\search_api\Processor\ProcessorInterface[][] $processors_by_stage */
  $processors_by_stage = [];
  foreach ($all_processors as $processor_id => $processor) {
    foreach ($stages as $stage => $definition) {
      if ($processor
        ->supportsStage($stage)) {
        $processors_by_stage[$stage][$processor_id] = $processor;
      }
    }
  }
  $enabled_processors = $this->entity
    ->getProcessors();
  $discouraged_processors = [];
  $discouraged_warning = '';
  if ($this->entity
    ->getServerInstance()) {
    $discouraged_processors = $this->entity
      ->getServerInstance()
      ->getDiscouragedProcessors();
    $discouraged_processors = array_flip($discouraged_processors);
    $discouraged_warning = '<br /><strong>' . $this
      ->t('It is recommended not to use this processor with the selected server.') . '</strong>';
  }
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'search_api/drupal.search_api.processors';
  $form['#title'] = $this
    ->t('Manage processors for search index %label', [
    '%label' => $this->entity
      ->label(),
  ]);
  $form['description']['#markup'] = '<p>' . $this
    ->t('Configure processors which will pre- and post-process data at index and search time. Find more information on the <a href=":url" target="_blank">processors documentation page</a>.', [
    ':url' => 'https://www.drupal.org/docs/8/modules/search-api/getting-started/processors',
  ]) . '</p>';

  // Add the list of processors with checkboxes to enable/disable them.
  $form['status'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Enabled'),
    '#attributes' => [
      'class' => [
        'search-api-status-wrapper',
      ],
    ],
  ];
  foreach ($all_processors as $processor_id => $processor) {
    $clean_css_id = Html::cleanCssIdentifier($processor_id);
    $is_enabled = !empty($enabled_processors[$processor_id]);
    $is_locked = $processor
      ->isLocked();
    $is_discouraged = isset($discouraged_processors[$processor_id]);
    $form['status'][$processor_id] = [
      '#type' => 'checkbox',
      '#title' => $processor
        ->label(),
      '#default_value' => $is_locked || $is_enabled,
      '#description' => $processor
        ->getDescription(),
      '#attributes' => [
        'class' => [
          'search-api-processor-status-' . $clean_css_id,
        ],
        'data-id' => $clean_css_id,
      ],
      '#disabled' => $is_locked || !$is_enabled && $is_discouraged,
      '#access' => !$processor
        ->isHidden(),
    ];
    if ($is_discouraged) {
      $form['status'][$processor_id]['#description'] .= $discouraged_warning;
    }
  }
  $form['weights'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Processor order'),
  ];

  // Order enabled processors per stage.
  foreach ($stages as $stage => $description) {
    $form['weights'][$stage] = [
      '#type' => 'fieldset',
      '#title' => $description['label'],
      '#attributes' => [
        'class' => [
          'search-api-stage-wrapper',
          'search-api-stage-wrapper-' . Html::cleanCssIdentifier($stage),
        ],
      ],
    ];
    $form['weights'][$stage]['order'] = [
      '#type' => 'table',
    ];
    $form['weights'][$stage]['order']['#tabledrag'][] = [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => 'search-api-processor-weight-' . Html::cleanCssIdentifier($stage),
    ];
  }
  foreach ($processors_by_stage as $stage => $processors) {

    // Sort the processors by weight for this stage.
    $processor_weights = [];
    foreach ($processors as $processor_id => $processor) {
      if (!isset($discouraged_processors[$processor_id])) {
        $processor_weights[$processor_id] = $processor
          ->getWeight($stage);
      }
    }
    asort($processor_weights);
    foreach ($processor_weights as $processor_id => $weight) {
      $processor = $processors[$processor_id];
      if ($processor
        ->isHidden()) {
        $form['processors'][$processor_id]['weights'][$stage] = [
          '#type' => 'value',
          '#value' => $weight,
        ];
        continue;
      }
      $form['weights'][$stage]['order'][$processor_id]['#attributes']['class'][] = 'draggable';
      $form['weights'][$stage]['order'][$processor_id]['#attributes']['class'][] = 'search-api-processor-weight--' . Html::cleanCssIdentifier($processor_id);
      $form['weights'][$stage]['order'][$processor_id]['#weight'] = $weight;
      $form['weights'][$stage]['order'][$processor_id]['label']['#plain_text'] = $processor
        ->label();
      $form['weights'][$stage]['order'][$processor_id]['weight'] = [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for processor %title', [
          '%title' => $processor
            ->label(),
        ]),
        '#title_display' => 'invisible',
        '#delta' => 50,
        '#default_value' => $weight,
        '#parents' => [
          'processors',
          $processor_id,
          'weights',
          $stage,
        ],
        '#attributes' => [
          'class' => [
            'search-api-processor-weight-' . Html::cleanCssIdentifier($stage),
          ],
        ],
      ];
    }
  }

  // Add vertical tabs containing the settings for the processors. Tabs for
  // disabled processors are hidden with JS magic, but need to be included in
  // case the processor is enabled.
  $form['processor_settings'] = [
    '#title' => $this
      ->t('Processor settings'),
    '#type' => 'vertical_tabs',
  ];
  foreach ($all_processors as $processor_id => $processor) {
    if ($processor instanceof PluginFormInterface) {
      $form['settings'][$processor_id] = [
        '#type' => 'details',
        '#title' => $processor
          ->label(),
        '#group' => 'processor_settings',
        '#parents' => [
          'processors',
          $processor_id,
          'settings',
        ],
        '#attributes' => [
          'class' => [
            'search-api-processor-settings-' . Html::cleanCssIdentifier($processor_id),
          ],
        ],
      ];
      $processor_form_state = SubformState::createForSubform($form['settings'][$processor_id], $form, $form_state);
      $form['settings'][$processor_id] += $processor
        ->buildConfigurationForm($form['settings'][$processor_id], $processor_form_state);
    }
    else {
      unset($form['settings'][$processor_id]);
    }
  }
  return $form;
}