You are here

public function IndexForm::buildEntityForm in Search API 8

Builds the form for the basic index properties.

Parameters

array $form: The current form array.

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

\Drupal\search_api\IndexInterface $index: The index that is being created or edited.

1 call to IndexForm::buildEntityForm()
IndexForm::form in src/Form/IndexForm.php
Gets the actual form array to be built.

File

src/Form/IndexForm.php, line 147

Class

IndexForm
Provides a form for the Index entity.

Namespace

Drupal\search_api\Form

Code

public function buildEntityForm(array &$form, FormStateInterface $form_state, IndexInterface $index) {
  $form['#tree'] = TRUE;
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Index name'),
    '#description' => $this
      ->t('Enter the displayed name for the index.'),
    '#default_value' => $index
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $index
      ->isNew() ? NULL : $index
      ->id(),
    '#maxlength' => 50,
    '#required' => TRUE,
    '#machine_name' => [
      'exists' => '\\Drupal\\search_api\\Entity\\Index::load',
      'source' => [
        'name',
      ],
    ],
    '#disabled' => !$index
      ->isNew(),
  ];
  $form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css';
  $form['datasources'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Datasources'),
    '#description' => $this
      ->t('Select one or more datasources of items that will be stored in this index.'),
    '#default_value' => $index
      ->getDatasourceIds(),
    '#multiple' => TRUE,
    '#required' => TRUE,
    '#attributes' => [
      'class' => [
        'search-api-checkboxes-list',
      ],
    ],
    '#ajax' => [
      'trigger_as' => [
        'name' => 'datasources_configure',
      ],
      'callback' => '::buildAjaxDatasourceConfigForm',
      'wrapper' => 'search-api-datasources-config-form',
      'method' => 'replace',
      'effect' => 'fade',
    ],
  ];
  $datasource_options = [];
  foreach ($this->pluginHelper
    ->createDatasourcePlugins($index) as $datasource_id => $datasource) {
    if ($datasource
      ->isHidden()) {
      continue;
    }
    $datasource_options[$datasource_id] = Utility::escapeHtml($datasource
      ->label());
    $form['datasources'][$datasource_id]['#description'] = Utility::escapeHtml($datasource
      ->getDescription());
  }
  asort($datasource_options, SORT_NATURAL | SORT_FLAG_CASE);
  $form['datasources']['#options'] = $datasource_options;
  $form['datasource_configs'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'search-api-datasources-config-form',
    ],
    '#tree' => TRUE,
  ];
  $form['datasource_configure_button'] = [
    '#type' => 'submit',
    '#name' => 'datasources_configure',
    '#value' => $this
      ->t('Configure'),
    '#limit_validation_errors' => [
      [
        'datasources',
      ],
    ],
    '#submit' => [
      '::submitAjaxDatasourceConfigForm',
    ],
    '#ajax' => [
      'callback' => '::buildAjaxDatasourceConfigForm',
      'wrapper' => 'search-api-datasources-config-form',
    ],
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
  ];
  $this
    ->buildDatasourcesConfigForm($form, $form_state, $index);
  $form['tracker'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Tracker'),
    '#description' => $this
      ->t('Select the type of tracker which should be used for keeping track of item changes.'),
    '#default_value' => $index
      ->getTrackerId(),
    '#required' => TRUE,
    '#ajax' => [
      'trigger_as' => [
        'name' => 'tracker_configure',
      ],
      'callback' => '::buildAjaxTrackerConfigForm',
      'wrapper' => 'search-api-tracker-config-form',
      'method' => 'replace',
      'effect' => 'fade',
    ],
  ];
  $tracker_options = [];
  foreach ($this->pluginHelper
    ->createTrackerPlugins($index) as $tracker_id => $tracker) {
    if ($tracker
      ->isHidden()) {
      continue;
    }
    $tracker_options[$tracker_id] = Utility::escapeHtml($tracker
      ->label());
    $form['tracker'][$tracker_id]['#description'] = Utility::escapeHtml($tracker
      ->getDescription());
  }
  asort($tracker_options, SORT_NATURAL | SORT_FLAG_CASE);
  $form['tracker']['#options'] = $tracker_options;
  $form['tracker']['#access'] = !$index
    ->hasValidTracker() || count($tracker_options) > 1;
  $form['tracker_config'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'search-api-tracker-config-form',
    ],
    '#tree' => TRUE,
  ];
  $form['tracker_configure_button'] = [
    '#type' => 'submit',
    '#name' => 'tracker_configure',
    '#value' => $this
      ->t('Configure'),
    '#limit_validation_errors' => [
      [
        'tracker',
      ],
    ],
    '#submit' => [
      '::submitAjaxTrackerConfigForm',
    ],
    '#ajax' => [
      'callback' => '::buildAjaxTrackerConfigForm',
      'wrapper' => 'search-api-tracker-config-form',
    ],
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
    '#access' => count($tracker_options) > 1,
  ];
  $this
    ->buildTrackerConfigForm($form, $form_state, $index);
  $form['server'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Server'),
    '#description' => $this
      ->t('Select the server this index should use. Indexes cannot be enabled without a connection to a valid, enabled server.'),
    '#options' => [
      '' => '<em>' . $this
        ->t('- No server -') . '</em>',
    ] + $this
      ->getServerOptions(),
    '#default_value' => $index
      ->hasValidServer() ? $index
      ->getServerId() : '',
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#description' => $this
      ->t('Only enabled indexes can be used for indexing and searching. This setting will only take effect if the selected server is also enabled.'),
    '#default_value' => $index
      ->status(),
    // Can't enable an index lying on a disabled server or no server at all.
    '#disabled' => !$index
      ->status() && (!$index
      ->hasValidServer() || !$index
      ->getServerInstance()
      ->status()),
    // @todo This doesn't seem to work and should also hide for disabled
    //   servers. If that works, we can probably remove the last sentence of
    //   the description.
    '#states' => [
      'invisible' => [
        ':input[name="server"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('Enter a description for the index.'),
    '#default_value' => $index
      ->getDescription(),
  ];
  $form['options'] = [
    '#tree' => TRUE,
    '#type' => 'details',
    '#title' => $this
      ->t('Index options'),
    '#collapsed' => TRUE,
  ];

  // We display the "read-only" flag along with the other options, even though
  // it is a property directly on the index object. We use "#parents" to move
  // it to the correct place in the form values.
  $form['options']['read_only'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Read only'),
    '#description' => $this
      ->t('Do not write to this index or track the status of items in this index.'),
    '#default_value' => $index
      ->isReadOnly(),
    '#parents' => [
      'read_only',
    ],
  ];
  $form['options']['index_directly'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Index items immediately'),
    '#description' => $this
      ->t('Immediately index new or updated items instead of waiting for the next cron run. This might have serious performance drawbacks and is generally not advised for larger sites.'),
    '#default_value' => $index
      ->getOption('index_directly'),
  ];
  $form['options']['cron_limit'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Cron batch size'),
    '#description' => $this
      ->t('Set how many items will be indexed at once when indexing items during a cron run. "0" means that no items will be indexed by cron for this index, "-1" means that cron should index all items at once.'),
    '#default_value' => $index
      ->getOption('cron_limit'),
    '#size' => 4,
  ];
}