6f4dc64d5c340c67e155af3e32217b8d391c488b
[yaffs-website] / web / core / modules / search / src / SearchPageRepository.php
1 <?php
2
3 namespace Drupal\search;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7
8 /**
9  * Provides a repository for Search Page config entities.
10  */
11 class SearchPageRepository implements SearchPageRepositoryInterface {
12
13   /**
14    * The config factory.
15    *
16    * @var \Drupal\Core\Config\ConfigFactoryInterface
17    */
18   protected $configFactory;
19
20   /**
21    * The search page storage.
22    *
23    * @var \Drupal\Core\Entity\EntityStorageInterface
24    */
25   protected $storage;
26
27   /**
28    * Constructs a new SearchPageRepository.
29    *
30    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
31    *   The config factory.
32    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
33    *   The entity manager.
34    */
35   public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager) {
36     $this->configFactory = $config_factory;
37     $this->storage = $entity_manager->getStorage('search_page');
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function getActiveSearchPages() {
44     $ids = $this->getQuery()
45       ->condition('status', TRUE)
46       ->execute();
47     return $this->storage->loadMultiple($ids);
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function isSearchActive() {
54     return (bool) $this->getQuery()
55       ->condition('status', TRUE)
56       ->range(0, 1)
57       ->execute();
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getIndexableSearchPages() {
64     return array_filter($this->getActiveSearchPages(), function (SearchPageInterface $search) {
65       return $search->isIndexable();
66     });
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getDefaultSearchPage() {
73     // Find all active search pages (without loading them).
74     $search_pages = $this->getQuery()
75       ->condition('status', TRUE)
76       ->execute();
77
78     // If the default page is active, return it.
79     $default = $this->configFactory->get('search.settings')->get('default_page');
80     if (isset($search_pages[$default])) {
81       return $default;
82     }
83
84     // Otherwise, use the first active search page.
85     return is_array($search_pages) ? reset($search_pages) : FALSE;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function clearDefaultSearchPage() {
92     $this->configFactory->getEditable('search.settings')->clear('default_page')->save();
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function setDefaultSearchPage(SearchPageInterface $search_page) {
99     $this->configFactory->getEditable('search.settings')->set('default_page', $search_page->id())->save();
100     $search_page->enable()->save();
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function sortSearchPages($search_pages) {
107     $entity_type = $this->storage->getEntityType();
108     uasort($search_pages, [$entity_type->getClass(), 'sort']);
109     return $search_pages;
110   }
111
112   /**
113    * Returns an entity query instance.
114    *
115    * @return \Drupal\Core\Entity\Query\QueryInterface
116    *   The query instance.
117    */
118   protected function getQuery() {
119     return $this->storage->getQuery();
120   }
121
122 }