You are here

function search_api_solr_form_search_api_index_form_validate_server in Search API Solr 8

Same name and namespace in other branches
  1. 8.3 search_api_solr.module \search_api_solr_form_search_api_index_form_validate_server()
  2. 8.2 search_api_solr.module \search_api_solr_form_search_api_index_form_validate_server()
  3. 4.x search_api_solr.module \search_api_solr_form_search_api_index_form_validate_server()
1 string reference to 'search_api_solr_form_search_api_index_form_validate_server'
search_api_solr_form_search_api_index_form_alter in ./search_api_solr.module

File

./search_api_solr.module, line 138
Provides a Solr-based service class for the Search API.

Code

function search_api_solr_form_search_api_index_form_validate_server(&$element, FormStateInterface $form_state, $form) {
  if ($server = Server::load($form_state
    ->getValue('server'))) {
    if ($server
      ->getBackend() instanceof SolrBackendInterface) {

      /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
      $form_object = $form_state
        ->getFormObject();
      $this_index = $form_object
        ->getEntity();
      $indexes = $server
        ->getIndexes();
      $index_count = 0;
      foreach ($indexes as $index) {
        if ($index
          ->status()) {
          if (!$this_index
            ->isNew() && $this_index
            ->id() == $index
            ->id()) {
            continue;
          }
          ++$index_count;
        }
      }
      if ($index_count > 0 && $form_state
        ->getValue('status')) {
        $msg = t('The concept of storing multiple "virtual" Search API indexes in one Solr index (aka core) is bad practice and randomly breaks a lot of advanced features like spell checking, suggestions, automplete and others. Create a second core within your Solr server and assign this "index" to that core.');
        if ($this_index
          ->isNew()) {

          // Avoid creating multiple indexes on one server.
          $form_state
            ->setError($element, $msg);
        }
        else {

          // Allow editing existing multiple indexes on one server for backward
          // compatibility.
          \Drupal::messenger()
            ->addError($msg);
        }
      }
    }
  }
}