View source
<?php
namespace Drupal\search_api_solr\Controller;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\search_api_solr\SearchApiSolrConflictingEntitiesException;
use Drupal\search_api_solr\SearchApiSolrException;
use Drupal\search_api_solr\SolrConfigInterface;
abstract class AbstractSolrEntityListBuilder extends ConfigEntityListBuilder {
use BackendTrait;
protected $label = '';
protected $option_label = 'Environment';
protected $default_option = 'default';
protected $limit = FALSE;
public function buildHeader() {
$header = [
'label' => $this
->t('@label', [
'@before' => $this->label,
]),
'minimum_solr_version' => $this
->t('Minimum Solr Version'),
'option' => $this
->t('@optionLabel', [
'@optionLabel' => $this->option_label,
]),
'id' => $this
->t('Machine name'),
'enabled' => $this
->t('Enabled'),
];
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $solr_entity) {
$options = $solr_entity
->getOptions();
if (empty($options)) {
$options = [
$this->default_option,
];
}
$enabled_label = $solr_entity->disabledOnServer ? $this
->t('Disabled') : $this
->t('Enabled');
$enabled_icon = [
'#theme' => 'image',
'#uri' => !$solr_entity->disabledOnServer ? 'core/misc/icons/73b355/check.svg' : 'core/misc/icons/e32700/error.svg',
'#width' => 18,
'#height' => 18,
'#alt' => $enabled_label,
'#title' => $enabled_label,
];
$row = [
'label' => $solr_entity
->label(),
'minimum_solr_version' => $solr_entity
->getMinimumSolrVersion(),
'options' => implode(', ', $options),
'id' => $solr_entity
->id(),
'enabled' => [
'data' => $enabled_icon,
'class' => [
'checkbox',
],
],
];
return $row + parent::buildRow($solr_entity);
}
public function getEnabledEntities() : array {
$solr_entities = [];
$entities = $this
->load();
foreach ($this
->load() as $solr_entity) {
if (!$solr_entity->disabledOnServer) {
$solr_entities[] = $solr_entity;
}
}
if ($conflicting_entities = $this
->getConflictingEntities($solr_entities)) {
$exception = new SearchApiSolrConflictingEntitiesException();
$exception
->setConflictingEntities($conflicting_entities);
throw $exception;
}
return $solr_entities;
}
public function getDefaultOperations(EntityInterface $solr_entity) {
$operations = parent::getDefaultOperations($solr_entity);
unset($operations['delete']);
if (!$solr_entity->disabledOnServer && $solr_entity
->access('view') && $solr_entity
->hasLinkTemplate('disable-for-server')) {
$operations['disable_for_server'] = [
'title' => $this
->t('Disable'),
'weight' => 10,
'url' => $solr_entity
->toUrl('disable-for-server'),
];
}
if ($solr_entity->disabledOnServer && $solr_entity
->access('view') && $solr_entity
->hasLinkTemplate('enable-for-server')) {
$operations['enable_for_server'] = [
'title' => $this
->t('Enable'),
'weight' => 10,
'url' => $solr_entity
->toUrl('enable-for-server'),
];
}
return $operations;
}
protected abstract function getDisabledEntities() : array;
public function load() {
static $entities;
if (!$entities || $this->reset) {
$solr_version = '9999.0.0';
$operator = '>=';
$option = $this->default_option;
$warning = FALSE;
$disabled_entities = [];
try {
$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;
}
$this->limit = FALSE;
$entity_ids = $this
->getEntityIds();
$storage = $this
->getStorage();
$entities = $storage
->loadMultipleOverrideFree($entity_ids);
$selection = [];
foreach ($entities as $key => $entity) {
$entities[$key]->disabledOnServer = in_array($entity
->id(), $disabled_entities);
$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])) {
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,
]));
}
uasort($entities, [
$this->entityType
->getClass(),
'sort',
]);
$this->reset = FALSE;
}
return $entities;
}
protected function mergeSolrConfigs(SolrConfigInterface $target, SolrConfigInterface $source) {
if (empty($target
->getSolrConfigs()) && !empty($source
->getSolrConfigs())) {
$target
->setSolrConfigs($source
->getSolrConfigs());
}
}
public function getXml() {
$xml = '';
foreach ($this
->getEnabledEntities() as $solr_entities) {
$xml .= $solr_entities
->getAsXml();
}
foreach ($this
->load() as $solr_entities) {
$xml .= $solr_entities
->getSolrConfigsAsXml();
}
return $xml;
}
public function getRecommendedEntities() : array {
$entities = $this
->load();
foreach ($entities as $key => $entity) {
if (!$entity
->isRecommended()) {
unset($entities[$key]);
}
}
return $entities;
}
public function getNotRecommendedEntities() : array {
$entities = $this
->load();
foreach ($entities as $key => $entity) {
if ($entity
->isRecommended()) {
unset($entities[$key]);
}
}
return $entities;
}
public function getAllRecommendedEntities() : array {
$entities = ConfigEntityListBuilder::load();
foreach ($entities as $key => $entity) {
if (!$entity
->isRecommended()) {
unset($entities[$key]);
}
}
return $entities;
}
public function getAllNotRecommendedEntities() : array {
$entities = ConfigEntityListBuilder::load();
foreach ($entities as $key => $entity) {
if ($entity
->isRecommended()) {
unset($entities[$key]);
}
}
return $entities;
}
public function getConflictingEntities(array $entities) : array {
$conflicting_entities = [];
$purpose_ids = [];
foreach ($entities as $key => $entity) {
$purpose_id = $entity
->getPurposeId();
if (!in_array($purpose_id, $purpose_ids)) {
$purpose_ids[] = $purpose_id;
}
else {
$conflicting_entities[$key] = $entity;
}
}
return $conflicting_entities;
}
}