You are here

function search_api_algolia_form_search_api_index_edit_form_alter in Search API Algolia 3.0.x

Same name and namespace in other branches
  1. 8 search_api_algolia.module \search_api_algolia_form_search_api_index_edit_form_alter()
  2. 2.0.x search_api_algolia.module \search_api_algolia_form_search_api_index_edit_form_alter()

Implements hook_form_FORM_ID_alter().

File

./search_api_algolia.module, line 17
Provides an Algolia Search based service class for the Search API.

Code

function search_api_algolia_form_search_api_index_edit_form_alter(&$form, FormStateInterface $form_state) {
  $index = Index::load($form['id']['#default_value']);
  $form['options']['algolia_index_name'] = [
    '#type' => 'textfield',
    '#title' => t('Algolia index'),
    '#default_value' => $index
      ->getOption('algolia_index_name') ?? '',
    '#weight' => 2,
  ];
  if (\Drupal::languageManager()
    ->isMultilingual()) {
    $form['options']['algolia_index_apply_suffix'] = [
      '#type' => 'select',
      '#title' => t('Apply language suffix'),
      '#options' => [
        0 => t('No'),
        1 => t('Yes'),
      ],
      '#default_value' => $index
        ->getOption('algolia_index_apply_suffix') ?? 0,
      '#weight' => 1,
    ];
    $form['options']['algolia_index_name']['#description'] = t('Avoid language suffix here if the flag to apply language suffix is enabled');
  }
  $form['options']['algolia_index_batch_deletion'] = [
    '#type' => 'select',
    '#title' => t('Enabled items deletion in Batches'),
    '#options' => [
      0 => t('No'),
      1 => t('Yes'),
    ],
    '#default_value' => $index
      ->getOption('algolia_index_batch_deletion') ?? 0,
    '#weight' => 1,
  ];
  $form['options']['object_id_field'] = [
    '#type' => 'textfield',
    '#title' => t('Object ID Field'),
    '#default_value' => $index
      ->getOption('object_id_field') ?? '',
    '#weight' => 1,
    '#description' => t('Specify the custom field to be use for Object ID field in Algolia. Leave empty to use default from search_api.'),
  ];
  $form['#validate'][] = 'search_api_algolia_index_edit_form_validate';

  // Show the list of available indexes for user to select from.
  $server = $index
    ->getServerInstance();
  if ($server
    ->getBackendId() == 'search_api_algolia') {
    try {
      $indices = $server
        ->getBackend()
        ->listIndexes();
      $element = [
        '#theme' => 'item_list',
        '#items' => $indices,
        '#title' => t('Available Algolia indexes'),
      ];
      $form['options']['algolia_index_list'] = [
        '#type' => 'item',
        '#markup' => render($element),
        '#weight' => 3,
      ];
    } catch (AlgoliaException $exception) {
      watchdog_exception('search_api_algolia', $exception, 'Unable to connect to Algolia.');
      \Drupal::messenger()
        ->addError(t('Unable to connect to Algolia.'));
    }
  }
}