You are here

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

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

Throws

\Drupal\search_api\SearchApiException

Overrides AbstractSolrEntityListBuilder::load

File

src/Controller/SolrFieldTypeListBuilder.php, line 74

Class

SolrFieldTypeListBuilder
Provides a listing of SolrFieldType.

Namespace

Drupal\search_api_solr\Controller

Code

public function load() {
  static $entities;
  $active_languages = array_keys(\Drupal::languageManager()
    ->getLanguages());

  // Ignore region and variant of the locale string the langauge manager
  // returns as we provide language fallbacks. For example, 'de' should be
  // used for 'de-at' if there's no dedicated 'de-at' field type.
  array_walk($active_languages, function (&$value) {
    list($value, ) = explode('-', $value);
  });
  $active_languages[] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
  if (!$entities || $this->reset) {
    $solr_version = '9999.0.0';
    $operator = '>=';
    $domain = 'generic';
    $warning = FALSE;
    $disabled_field_types = [];
    try {

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

    // 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\Entity\SolrFieldType[] $entities */
    $entities = $storage
      ->loadMultipleOverrideFree($entity_ids);

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

      /** @var \Drupal\search_api_solr\SolrFieldTypeInterface $solr_field_type */
      $version = $solr_field_type
        ->getMinimumSolrVersion();
      $domains = $solr_field_type
        ->getDomains();
      list($language, ) = explode('-', $solr_field_type
        ->getFieldTypeLanguageCode());
      if ($solr_field_type
        ->requiresManagedSchema() != $this
        ->getBackend()
        ->isManagedSchema() || version_compare($version, $solr_version, '>') || !in_array($language, $active_languages) || !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)) {
            $this
              ->mergeFieldTypes($entities[$key], $entities[$selection[$name]['key']]);
            unset($entities[$selection[$name]['key']]);
            $selection[$name] = [
              'version' => $version,
              'key' => $key,
              'domain' => in_array($domain, $domains) ? $domain : 'generic',
            ];
          }
          else {
            $this
              ->mergeFieldTypes($entities[$selection[$name]['key']], $entities[$key]);
            unset($entities[$key]);
          }
        }
        else {
          $selection[$name] = [
            'version' => $version,
            'key' => $key,
            'domain' => in_array($domain, $domains) ? $domain : 'generic',
          ];
        }
      }
    }
    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;
}