search_api_meilisearch-1.0.x-dev/search_api_meilisearch.module

search_api_meilisearch.module
<?php

/**
 * @file
 * Contains meilisearch.module.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api_meilisearch\Plugin\search_api\backend\SearchApiMeilisearchBackend;

/**
 * Implements hook_help().
 */
function search_api_meilisearch_help($route_name, RouteMatchInterface $route_match): string {
  switch ($route_name) {
    // Name and description of the Search API Meilisearch module.
    case 'help.page.search_api_meilisearch':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Adds Meilisearch plugin for Search API.') . '</p>';
      return $output;

    default:
      return '';
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function search_api_meilisearch_search_api_index_presave(IndexInterface $entity) {
  $server = $entity->getServerInstance();
  if ($server && $server->getBackend() instanceof SearchApiMeilisearchBackend) {
    $fieldSettings = $entity->getFields();
    foreach ($fieldSettings as $id => $field) {
      if ($id === 'id') {
        throw new Exception(sprintf(
          '%s field uses machine name "id" which is reserved for Meilisearch document id.',
          $field->getLabel()
        ));
      }
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function search_api_meilisearch_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
  if ($form_id === 'search_api_index_fields') {
    $index = $form_state->getFormObject()->getEntity();
    $server = $index->getServerInstance();
    if ($server && $server->getBackend() instanceof SearchApiMeilisearchBackend) {
      $form['#validate'][] = 'search_api_meilisearch_validate_index_fields';
    }
  }

  if ($form_id === 'search_api_index_edit_form' && isset($form_state->getUserInput()['server'])) {
    $serverName = $form_state->getUserInput()['server'];
    $server = Drupal::entityTypeManager()->getStorage('search_api_server')->load($serverName);
    if ($server && $server->getBackend() instanceof SearchApiMeilisearchBackend) {
      $form['#validate'][] = 'search_api_meilisearch_validate_index';
    }
  }
}

/**
 * Validates that index fields do not use reserved machine name.
 *
 * @param array $form
 *   An associative array containing the structure of the form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 */
function search_api_meilisearch_validate_index_fields(array &$form, FormStateInterface $form_state): void {
  $values = $form_state->getValues();
  if (!isset($values['fields']) || !is_array($values['fields'])) {
    return;
  }

  $fields = $values['fields'];
  foreach ($fields as $id => $field) {
    if ($field['id'] === 'id') {
      $error = t('Machine name "id" is reserved for the Meilisearch document id and cannot be used.');
      $form_state->setErrorByName('fields][' . $id . "][id", $error);
    }
  }
}

/**
 * Validates that index fields do not use reserved machine name.
 *
 * @param array $form
 *   An associative array containing the structure of the form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 */
function search_api_meilisearch_validate_index(array &$form, FormStateInterface $form_state): void {
  /** @var \Drupal\search_api\IndexInterface $index */
  $index = $form_state->getFormObject()->getEntity();
  $fieldSettings = $index->getFields();
  foreach ($fieldSettings as $id => $field) {
    if ($id === 'id') {
      $error = t(
        '@label field uses machine name "id" which is reserved for Meilisearch document id. Please set a different machine name under Fields tab.',
        ['@label' => $field->getLabel()]
      );
      $form_state->setError($form, $error);
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc