You are here

public function IndexFieldsForm::buildForm in Search API 8

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/IndexFieldsForm.php, line 137

Class

IndexFieldsForm
Provides a form for configuring the fields of a search index.

Namespace

Drupal\search_api\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  $index = $this->entity;

  // Do not allow the form to be cached. See
  // \Drupal\views_ui\ViewEditForm::form().
  $form_state
    ->disableCache();
  $this
    ->checkEntityEditable($form, $index, TRUE);

  // Set an appropriate page title.
  $form['#title'] = $this
    ->t('Manage fields for search index %label', [
    '%label' => $index
      ->label(),
  ]);
  $form['#tree'] = TRUE;
  $form['add-field'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Add fields'),
    '#url' => $this->entity
      ->toUrl('add-fields'),
    '#attributes' => [
      'class' => [
        'use-ajax',
        'button',
        'button-action',
        'button--primary',
        'button--small',
      ],
      'data-dialog-type' => 'modal',
      'data-dialog-options' => Json::encode([
        'width' => 700,
      ]),
    ],
  ];
  $form['description']['#markup'] = $this
    ->t('<p>The data type of a field determines how it can be used for searching and filtering. The boost is used to give additional weight to certain fields, for example titles or tags.</p> <p>For information about the data types available for indexing, see the <a href=":url">data types table</a> at the bottom of the page.</p>', [
    ':url' => '#search-api-data-types-table',
  ]);
  if ($fields = $index
    ->getFieldsByDatasource(NULL)) {
    $form['_general'] = $this
      ->buildFieldsTable($fields);
    $form['_general']['#title'] = $this
      ->t('General');
  }
  foreach ($index
    ->getDatasources() as $datasource_id => $datasource) {
    $fields = $index
      ->getFieldsByDatasource($datasource_id);
    $form[$datasource_id] = $this
      ->buildFieldsTable($fields);
    $form[$datasource_id]['#title'] = $datasource
      ->label();
  }

  // Build the data type table.
  $instances = $this->dataTypePluginManager
    ->getInstances();
  $fallback_mapping = $this->dataTypeHelper
    ->getDataTypeFallbackMapping($index);
  $data_types = [];
  foreach ($instances as $name => $type) {
    if ($type
      ->isHidden()) {
      continue;
    }
    $data_types[$name] = [
      'label' => $type
        ->label(),
      'description' => $type
        ->getDescription(),
      'fallback' => $type
        ->getFallbackType(),
    ];
  }
  $form['data_type_explanation'] = [
    '#type' => 'details',
    '#id' => 'search-api-data-types-table',
    '#title' => $this
      ->t('Data types'),
    '#description' => $this
      ->t("The data types which can be used for indexing fields in this index. Whether a type is supported depends on the backend of the index's server. If a type is not supported, the fallback type that will be used instead is shown, too."),
    '#theme' => 'search_api_admin_data_type_table',
    '#data_types' => $data_types,
    '#fallback_mapping' => $fallback_mapping,
  ];
  $form['actions'] = $this
    ->actionsElement($form, $form_state);
  return $form;
}