You are here

public function Highlight::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/Highlight.php, line 141

Class

Highlight
Adds a highlighted excerpt to results and highlights returned fields.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $parent_name = 'processors[highlight][settings]';
  if (!empty($form['#parents'])) {
    $parents = $form['#parents'];
    $parent_name = $root = array_shift($parents);
    if ($parents) {
      $parent_name = $root . '[' . implode('][', $parents) . ']';
    }
  }
  $form['highlight'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Highlight returned field data'),
    '#description' => $this
      ->t('Select whether returned fields should be highlighted.'),
    '#options' => [
      'always' => $this
        ->t('Always'),
      'server' => $this
        ->t('If the server returns fields'),
      'never' => $this
        ->t('Never'),
    ],
    '#default_value' => $this->configuration['highlight'],
  ];
  $form['highlight_partial'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Highlight partial matches'),
    '#description' => $this
      ->t('When enabled, matches in parts of words will be highlighted as well.'),
    '#default_value' => $this->configuration['highlight_partial'],
  ];
  $form['excerpt'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Create excerpt'),
    '#description' => $this
      ->t('When enabled, an excerpt will be created for searches with keywords, containing all occurrences of keywords in a fulltext field.'),
    '#default_value' => $this->configuration['excerpt'],
  ];
  $form['excerpt_length'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Excerpt length'),
    '#description' => $this
      ->t('The requested length of the excerpt, in characters'),
    '#default_value' => $this->configuration['excerpt_length'],
    '#min' => 50,
    '#states' => [
      'visible' => [
        ":input[name=\"{$parent_name}[excerpt]\"]" => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Exclude certain fulltext fields.
  $fields = $this->index
    ->getFields();
  $fulltext_fields = [];
  foreach ($this->index
    ->getFulltextFields() as $field_id) {
    $fulltext_fields[$field_id] = $fields[$field_id]
      ->getLabel() . ' (' . $field_id . ')';
  }
  $form['exclude_fields'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Exclude fields from excerpt'),
    '#description' => $this
      ->t('Exclude certain fulltext fields from being included in the excerpt.'),
    '#options' => $fulltext_fields,
    '#default_value' => $this->configuration['exclude_fields'],
    '#attributes' => [
      'class' => [
        'search-api-checkboxes-list',
      ],
    ],
    '#states' => [
      'visible' => [
        ":input[name=\"{$parent_name}[excerpt]\"]" => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['prefix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Highlighting prefix'),
    '#description' => $this
      ->t('Text/HTML that will be prepended to all occurrences of search keywords in highlighted text'),
    '#default_value' => $this->configuration['prefix'],
  ];
  $form['suffix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Highlighting suffix'),
    '#description' => $this
      ->t('Text/HTML that will be appended to all occurrences of search keywords in highlighted text'),
    '#default_value' => $this->configuration['suffix'],
  ];
  return $form;
}