SearchPageRepository.php in Drupal 10
File
core/modules/search/src/SearchPageRepository.php
View source
<?php
namespace Drupal\search;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class SearchPageRepository implements SearchPageRepositoryInterface {
protected $configFactory;
protected $storage;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->configFactory = $config_factory;
$this->storage = $entity_type_manager
->getStorage('search_page');
}
public function getActiveSearchPages() {
$ids = $this
->getQuery()
->condition('status', TRUE)
->execute();
return $this->storage
->loadMultiple($ids);
}
public function isSearchActive() {
return (bool) $this
->getQuery()
->condition('status', TRUE)
->range(0, 1)
->execute();
}
public function getIndexableSearchPages() {
return array_filter($this
->getActiveSearchPages(), function (SearchPageInterface $search) {
return $search
->isIndexable();
});
}
public function getDefaultSearchPage() {
$search_pages = $this
->getQuery()
->condition('status', TRUE)
->execute();
$default = $this->configFactory
->get('search.settings')
->get('default_page');
if (isset($search_pages[$default])) {
return $default;
}
return is_array($search_pages) ? reset($search_pages) : FALSE;
}
public function clearDefaultSearchPage() {
$this->configFactory
->getEditable('search.settings')
->clear('default_page')
->save();
}
public function setDefaultSearchPage(SearchPageInterface $search_page) {
$this->configFactory
->getEditable('search.settings')
->set('default_page', $search_page
->id())
->save();
$search_page
->enable()
->save();
}
public function sortSearchPages($search_pages) {
$entity_type = $this->storage
->getEntityType();
uasort($search_pages, [
$entity_type
->getClass(),
'sort',
]);
return $search_pages;
}
protected function getQuery() {
return $this->storage
->getQuery();
}
}