You are here

public function SolrFieldTypeListBuilder::load in Search API Multilingual Solr Search 8

Loads entities of this type from storage for listing.

This allows the implementation to manipulate the listing, like filtering or sorting the loaded entities.

Return value

\Drupal\Core\Entity\EntityInterface[] An array of entities implementing \Drupal\Core\Entity\EntityInterface indexed by their IDs. Returns an empty array if no matching entities are found.

Overrides ConfigEntityListBuilder::load

3 calls to SolrFieldTypeListBuilder::load()
SolrFieldTypeListBuilder::generateSchemaExtraFieldsXml in src/Controller/SolrFieldTypeListBuilder.php
SolrFieldTypeListBuilder::generateSchemaExtraTypesXml in src/Controller/SolrFieldTypeListBuilder.php
SolrFieldTypeListBuilder::getConfigZip in src/Controller/SolrFieldTypeListBuilder.php

File

src/Controller/SolrFieldTypeListBuilder.php, line 68

Class

SolrFieldTypeListBuilder
Provides a listing of SolrFieldType.

Namespace

Drupal\search_api_solr_multilingual\Controller

Code

public function load() {
  static $entities;
  if (!$entities) {
    $solr_version = '9999.0.0';
    $operator = '>=';
    $domain = 'generic';
    $warning = FALSE;
    try {

      /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
      $backend = $this
        ->getBackend();
      $domain = $backend
        ->getDomain();
      $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;
    }
    $entity_ids = $this
      ->getEntityIds();

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

    // We filter to those field types that are relevant for the current server.
    // There are multiple entities having the same field_type.name but different
    // values for managed_schema, minimum_solr_version and domains.
    $selection = [];
    foreach ($entities as $key => $solr_field_type) {

      /** @var \Drupal\search_api_solr_multilingual\SolrFieldTypeInterface $solr_field_type */
      $version = $solr_field_type
        ->getMinimumSolrVersion();
      $domains = $solr_field_type
        ->getDomains();
      if ($solr_field_type
        ->isManagedSchema() != $this
        ->getBackend()
        ->isManagedSchema() || version_compare($version, $solr_version, '>') || !in_array($domain, $domains) && !in_array('generic', $domains)) {
        unset($entities[$key]);
      }
      else {
        $name = $solr_field_type
          ->getFieldTypeName();
        if (isset($selection[$name])) {

          // The more specific domain has precedence over a newer version.
          if ('generic' != $domain && 'generic' == $selection[$name]['domain'] && in_array($domain, $domains) || version_compare($version, $selection[$name]['version'], $operator) && in_array($selection[$name]['domain'], $domains)) {
            unset($entities[$selection[$name]['key']]);
            $selection[$name] = [
              'version' => $version,
              'key' => $key,
              'domain' => in_array($domain, $domains) ? $domain : 'generic',
            ];
          }
          else {
            unset($entities[$key]);
          }
        }
        else {
          $selection[$name] = [
            'version' => $version,
            'key' => $key,
            'domain' => in_array($domain, $domains) ? $domain : 'generic',
          ];
        }
      }
    }
    if ($warning) {
      $this->assumed_minimum_version = array_reduce($selection, function ($version, $item) {
        if (version_compare($item['version'], $version, '<')) {
          return $item['version'];
        }
        return $version;
      }, $solr_version);
      $this
        ->messenger()
        ->addWarning($this
        ->t('Unable to reach the Solr server (yet). Therefor 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 add 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->assumed_minimum_version,
      ]));
    }

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