You are here

public function AbstractSolrEntityListBuilder::load in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Controller/AbstractSolrEntityListBuilder.php \Drupal\search_api_solr\Controller\AbstractSolrEntityListBuilder::load()

Throws

\Drupal\search_api\SearchApiException

Overrides ConfigEntityListBuilder::load

4 calls to AbstractSolrEntityListBuilder::load()
AbstractSolrEntityListBuilder::getEnabledEntities in src/Controller/AbstractSolrEntityListBuilder.php
Returns a list of all enabled Solr config entities for current server.
AbstractSolrEntityListBuilder::getNotRecommendedEntities in src/Controller/AbstractSolrEntityListBuilder.php
Get all not recommended entities.
AbstractSolrEntityListBuilder::getRecommendedEntities in src/Controller/AbstractSolrEntityListBuilder.php
Get all recommended Entities.
AbstractSolrEntityListBuilder::getXml in src/Controller/AbstractSolrEntityListBuilder.php
Returns the formatted XML for the entities.
1 method overrides AbstractSolrEntityListBuilder::load()
SolrFieldTypeListBuilder::load in src/Controller/SolrFieldTypeListBuilder.php

File

src/Controller/AbstractSolrEntityListBuilder.php, line 161

Class

AbstractSolrEntityListBuilder
Provides a listing of Solr Entities.

Namespace

Drupal\search_api_solr\Controller

Code

public function load() {
  static $entities;
  if (!$entities || $this->reset) {
    $solr_version = '9999.0.0';
    $operator = '>=';
    $option = $this->default_option;
    $warning = FALSE;
    $disabled_entities = [];
    try {

      /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
      $backend = $this
        ->getBackend();
      $disabled_entities = $this
        ->getDisabledEntities();
      $option = $backend
        ->getEnvironment();
      $solr_version = $backend
        ->getSolrConnector()
        ->getSolrVersion();
      if (version_compare($solr_version, '0.0.0', '==')) {
        $solr_version = '9999.0.0';
        throw new SearchApiSolrException();
      }
    } catch (SearchApiSolrException $e) {
      $operator = '<=';
      $warning = TRUE;
    }

    // We need the whole list to work on.
    $this->limit = FALSE;
    $entity_ids = $this
      ->getEntityIds();

    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
    $storage = $this
      ->getStorage();

    /** @var \Drupal\search_api_solr\SolrConfigInterface[] $entities */
    $entities = $storage
      ->loadMultipleOverrideFree($entity_ids);

    // We filter those entities that are relevant for the current server.
    // There are multiple entities having the same entity name but different
    // values for minimum_solr_version and environment.
    $selection = [];
    foreach ($entities as $key => $entity) {
      $entities[$key]->disabledOnServer = in_array($entity
        ->id(), $disabled_entities);

      /** @var \Drupal\search_api_solr\SolrConfigInterface $entity
       */
      $version = $entity
        ->getMinimumSolrVersion();
      $environments = $entity
        ->getEnvironments();
      if (version_compare($version, $solr_version, '>') || !in_array($option, $environments) && !in_array($this->default_option, $environments)) {
        unset($entities[$key]);
      }
      else {
        $name = $entity
          ->getName();
        if (isset($selection[$name])) {

          // The more specific environment has precedence
          // over a newer version.
          if ($this->default_option !== $option && $this->default_option === $selection[$name]['option'] && in_array($option, $environments) || version_compare($version, $selection[$name]['version'], $operator) && in_array($selection[$name]['option'], $environments)) {
            $this
              ->mergeSolrConfigs($entities[$key], $entities[$selection[$name]['key']]);
            unset($entities[$selection[$name]['key']]);
            $selection[$name] = [
              'version' => $version,
              'key' => $key,
              'option' => in_array($option, $environments) ? $option : $this->default_option,
            ];
          }
          else {
            $this
              ->mergeSolrConfigs($entities[$selection[$name]['key']], $entities[$key]);
            unset($entities[$key]);
          }
        }
        else {
          $selection[$name] = [
            'version' => $version,
            'key' => $key,
            'option' => in_array($option, $environments) ? $option : $this->default_option,
          ];
        }
      }
    }
    if ($warning) {
      $this->assumedMinimumVersion = array_reduce($selection, function ($version, $item) {
        if (version_compare($item['version'], $version, '<')) {
          return $item['version'];
        }
        return $version;
      }, $solr_version);
      \Drupal::messenger()
        ->addWarning($this
        ->t('Unable to reach the Solr server (yet). Therefore the lowest supported Solr version %version is assumed. Once the connection works and the real Solr version could be detected it might be necessary to deploy an adjusted config to the server to get the best search results. If the server does not start using the downloadable config, you should edit the server and manually set the Solr version override temporarily that fits your server best and download the config again. But it is recommended to remove this override once the server is running.', [
        '%version' => $this->assumedMinimumVersion,
      ]));
    }

    // Sort the entities using the entity class's sort() method.
    // See \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
    uasort($entities, [
      $this->entityType
        ->getClass(),
      'sort',
    ]);
    $this->reset = FALSE;
  }
  return $entities;
}