search_api_algolia.module in Search API Algolia 3.0.x
Same filename and directory in other branches
Provides an Algolia Search based service class for the Search API.
File
search_api_algolia.moduleView source
<?php
/**
* @file
* Provides an Algolia Search based service class for the Search API.
*/
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\Entity\Index;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_form_FORM_ID_alter().
*/
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.'));
}
}
}
/**
* Form validation handler.
*
*/
function search_api_algolia_index_edit_form_validate($form, FormStateInterface $form_state) {
if (!$form_state
->getValue('options')['algolia_index_batch_deletion'] && !empty($form_state
->getValue('options')['object_id_field'])) {
$form_state
->setErrorByName('options][object_id_field', t('To use Custom Object ID, it is required to delete the items in batches.'));
}
}
/**
* Implements hook_entity_delete().
*
*/
function search_api_algolia_entity_delete(EntityInterface $entity) {
\Drupal::service('search_api_algolia.helper')
->entityDelete($entity);
}
Functions
Name![]() |
Description |
---|---|
search_api_algolia_entity_delete | Implements hook_entity_delete(). |
search_api_algolia_form_search_api_index_edit_form_alter | Implements hook_form_FORM_ID_alter(). |
search_api_algolia_index_edit_form_validate | Form validation handler. |