You are here

class SearchPageRepository in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/search/src/SearchPageRepository.php \Drupal\search\SearchPageRepository

Provides a repository for Search Page config entities.

Hierarchy

Expanded class hierarchy of SearchPageRepository

1 file declares its use of SearchPageRepository
SearchPageRepositoryTest.php in core/modules/search/tests/src/Unit/SearchPageRepositoryTest.php
Contains \Drupal\Tests\search\Unit\SearchPageRepositoryTest.
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\search
View 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

Namesort descending Modifiers Type Description Overrides
SearchPageRepository::$configFactory protected property The config factory.
SearchPageRepository::$storage protected property The search page storage.
SearchPageRepository::clearDefaultSearchPage public function Clears the default search page. Overrides SearchPageRepositoryInterface::clearDefaultSearchPage
SearchPageRepository::getActiveSearchPages public function Returns all active search page entities. Overrides SearchPageRepositoryInterface::getActiveSearchPages
SearchPageRepository::getDefaultSearchPage public function Returns the default search page. Overrides SearchPageRepositoryInterface::getDefaultSearchPage
SearchPageRepository::getIndexableSearchPages public function Returns all active, indexable search page entities. Overrides SearchPageRepositoryInterface::getIndexableSearchPages
SearchPageRepository::getQuery protected function Returns an entity query instance.
SearchPageRepository::isSearchActive public function Returns whether search is active. Overrides SearchPageRepositoryInterface::isSearchActive
SearchPageRepository::setDefaultSearchPage public function Sets a given search page as the default. Overrides SearchPageRepositoryInterface::setDefaultSearchPage
SearchPageRepository::sortSearchPages public function Sorts a list of search pages. Overrides SearchPageRepositoryInterface::sortSearchPages
SearchPageRepository::__construct public function Constructs a new SearchPageRepository.