class SearchPageRepository
Same name in other branches
- 9 core/modules/search/src/SearchPageRepository.php \Drupal\search\SearchPageRepository
- 8.9.x core/modules/search/src/SearchPageRepository.php \Drupal\search\SearchPageRepository
- 11.x core/modules/search/src/SearchPageRepository.php \Drupal\search\SearchPageRepository
Provides a repository for Search Page config entities.
Hierarchy
- class \Drupal\search\SearchPageRepository implements \Drupal\search\SearchPageRepositoryInterface
Expanded class hierarchy of SearchPageRepository
2 files declare their use of SearchPageRepository
- SearchPageRepositoryTest.php in core/
modules/ search/ tests/ src/ Unit/ SearchPageRepositoryTest.php - SearchPageValidationTest.php in core/
modules/ search/ tests/ src/ Kernel/ SearchPageValidationTest.php
1 string reference to 'SearchPageRepository'
- search.services.yml in core/
modules/ search/ search.services.yml - core/modules/search/search.services.yml
1 service uses SearchPageRepository
- search.search_page_repository in core/
modules/ search/ search.services.yml - Drupal\search\SearchPageRepository
File
-
core/
modules/ search/ src/ SearchPageRepository.php, line 11
Namespace
Drupal\searchView source
class SearchPageRepository implements SearchPageRepositoryInterface {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The search page storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* Constructs a new SearchPageRepository.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->configFactory = $config_factory;
$this->storage = $entity_type_manager->getStorage('search_page');
}
/**
* {@inheritdoc}
*/
public function getActiveSearchPages() {
$ids = $this->getQuery()
->condition('status', TRUE)
->execute();
return $this->storage
->loadMultiple($ids);
}
/**
* {@inheritdoc}
*/
public function isSearchActive() {
return (bool) $this->getQuery()
->condition('status', TRUE)
->range(0, 1)
->execute();
}
/**
* {@inheritdoc}
*/
public function getIndexableSearchPages() {
return array_filter($this->getActiveSearchPages(), function (SearchPageInterface $search) {
return $search->isIndexable();
});
}
/**
* {@inheritdoc}
*/
public function getDefaultSearchPage() {
// Find all active search pages (without loading them).
$search_pages = $this->getQuery()
->condition('status', TRUE)
->execute();
// If the default page is active, return it.
$default = $this->configFactory
->get('search.settings')
->get('default_page');
if (isset($search_pages[$default])) {
return $default;
}
// Otherwise, use the first active search page.
return is_array($search_pages) ? reset($search_pages) : FALSE;
}
/**
* {@inheritdoc}
*/
public function clearDefaultSearchPage() {
$this->configFactory
->getEditable('search.settings')
->clear('default_page')
->save();
}
/**
* {@inheritdoc}
*/
public function setDefaultSearchPage(SearchPageInterface $search_page) {
$this->configFactory
->getEditable('search.settings')
->set('default_page', $search_page->id())
->save();
$search_page->enable()
->save();
}
/**
* {@inheritdoc}
*/
public function sortSearchPages($search_pages) {
$entity_type = $this->storage
->getEntityType();
uasort($search_pages, [
$entity_type->getClass(),
'sort',
]);
return $search_pages;
}
/**
* Returns an entity query instance.
*
* @return \Drupal\Core\Entity\Query\QueryInterface
* The query instance.
*/
protected function getQuery() {
return $this->storage
->getQuery();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
SearchPageRepository::$configFactory | protected | property | The config factory. |
SearchPageRepository::$storage | protected | property | The search page storage. |
SearchPageRepository::clearDefaultSearchPage | public | function | |
SearchPageRepository::getActiveSearchPages | public | function | |
SearchPageRepository::getDefaultSearchPage | public | function | |
SearchPageRepository::getIndexableSearchPages | public | function | |
SearchPageRepository::getQuery | protected | function | Returns an entity query instance. |
SearchPageRepository::isSearchActive | public | function | |
SearchPageRepository::setDefaultSearchPage | public | function | |
SearchPageRepository::sortSearchPages | public | function | |
SearchPageRepository::__construct | public | function | Constructs a new SearchPageRepository. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.