Version 1
[yaffs-website] / web / core / modules / search / src / Entity / SearchPage.php
1 <?php
2
3 namespace Drupal\search\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\Core\Config\Entity\ConfigEntityInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
9 use Drupal\search\Plugin\SearchIndexingInterface;
10 use Drupal\search\Plugin\SearchPluginCollection;
11 use Drupal\search\SearchPageInterface;
12
13 /**
14  * Defines a configured search page.
15  *
16  * @ConfigEntityType(
17  *   id = "search_page",
18  *   label = @Translation("Search page"),
19  *   handlers = {
20  *     "access" = "Drupal\search\SearchPageAccessControlHandler",
21  *     "list_builder" = "Drupal\search\SearchPageListBuilder",
22  *     "form" = {
23  *       "add" = "Drupal\search\Form\SearchPageAddForm",
24  *       "edit" = "Drupal\search\Form\SearchPageEditForm",
25  *       "search" = "Drupal\search\Form\SearchPageForm",
26  *       "delete" = "Drupal\Core\Entity\EntityDeleteForm"
27  *     }
28  *   },
29  *   admin_permission = "administer search",
30  *   links = {
31  *     "edit-form" = "/admin/config/search/pages/manage/{search_page}",
32  *     "delete-form" = "/admin/config/search/pages/manage/{search_page}/delete",
33  *     "enable" = "/admin/config/search/pages/manage/{search_page}/enable",
34  *     "disable" = "/admin/config/search/pages/manage/{search_page}/disable",
35  *     "set-default" = "/admin/config/search/pages/manage/{search_page}/set-default",
36  *     "collection" = "/admin/config/search/pages",
37  *   },
38  *   config_prefix = "page",
39  *   entity_keys = {
40  *     "id" = "id",
41  *     "label" = "label",
42  *     "weight" = "weight",
43  *     "status" = "status"
44  *   },
45  *   config_export = {
46  *     "id",
47  *     "label",
48  *     "path",
49  *     "weight",
50  *     "plugin",
51  *     "configuration",
52  *   }
53  * )
54  */
55 class SearchPage extends ConfigEntityBase implements SearchPageInterface, EntityWithPluginCollectionInterface {
56
57   /**
58    * The name (plugin ID) of the search page entity.
59    *
60    * @var string
61    */
62   protected $id;
63
64   /**
65    * The label of the search page entity.
66    *
67    * @var string
68    */
69   protected $label;
70
71   /**
72    * The configuration of the search page entity.
73    *
74    * @var array
75    */
76   protected $configuration = [];
77
78   /**
79    * The search plugin ID.
80    *
81    * @var string
82    */
83   protected $plugin;
84
85   /**
86    * The path this search page will appear upon.
87    *
88    * This value is appended to 'search/' when building the path.
89    *
90    * @var string
91    */
92   protected $path;
93
94   /**
95    * The weight of the search page.
96    *
97    * @var int
98    */
99   protected $weight;
100
101   /**
102    * The plugin collection that stores search plugins.
103    *
104    * @var \Drupal\search\Plugin\SearchPluginCollection
105    */
106   protected $pluginCollection;
107
108   /**
109    * {@inheritdoc}
110    */
111   public function getPlugin() {
112     return $this->getPluginCollection()->get($this->plugin);
113   }
114
115   /**
116    * Encapsulates the creation of the search page's LazyPluginCollection.
117    *
118    * @return \Drupal\Component\Plugin\LazyPluginCollection
119    *   The search page's plugin collection.
120    */
121   protected function getPluginCollection() {
122     if (!$this->pluginCollection) {
123       $this->pluginCollection = new SearchPluginCollection($this->searchPluginManager(), $this->plugin, $this->configuration, $this->id());
124     }
125     return $this->pluginCollection;
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function getPluginCollections() {
132     return ['configuration' => $this->getPluginCollection()];
133   }
134
135   /**
136    * {@inheritdoc}
137    */
138   public function setPlugin($plugin_id) {
139     $this->plugin = $plugin_id;
140     $this->getPluginCollection()->addInstanceID($plugin_id);
141   }
142
143   /**
144    * {@inheritdoc}
145    */
146   public function isIndexable() {
147     return $this->status() && $this->getPlugin() instanceof SearchIndexingInterface;
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public function isDefaultSearch() {
154     return $this->searchPageRepository()->getDefaultSearchPage() == $this->id();
155   }
156
157   /**
158    * {@inheritdoc}
159    */
160   public function getPath() {
161     return $this->path;
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function getWeight() {
168     return $this->weight;
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public function postCreate(EntityStorageInterface $storage) {
175     parent::postCreate($storage);
176
177     // @todo Use self::applyDefaultValue() once
178     //   https://www.drupal.org/node/2004756 is in.
179     if (!isset($this->weight)) {
180       $this->weight = $this->isDefaultSearch() ? -10 : 0;
181     }
182   }
183
184   /**
185    * {@inheritdoc}
186    */
187   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
188     parent::postSave($storage, $update);
189     $this->routeBuilder()->setRebuildNeeded();
190   }
191
192   /**
193    * {@inheritdoc}
194    */
195   public static function postDelete(EntityStorageInterface $storage, array $entities) {
196     parent::postDelete($storage, $entities);
197
198     $search_page_repository = \Drupal::service('search.search_page_repository');
199     if (!$search_page_repository->isSearchActive()) {
200       $search_page_repository->clearDefaultSearchPage();
201     }
202   }
203
204   /**
205    * {@inheritdoc}
206    */
207   public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
208     /** @var $a \Drupal\search\SearchPageInterface */
209     /** @var $b \Drupal\search\SearchPageInterface */
210     $a_status = (int) $a->status();
211     $b_status = (int) $b->status();
212     if ($a_status != $b_status) {
213       return ($a_status > $b_status) ? -1 : 1;
214     }
215     return parent::sort($a, $b);
216   }
217
218   /**
219    * Wraps the route builder.
220    *
221    * @return \Drupal\Core\Routing\RouteBuilderInterface
222    *   An object for state storage.
223    */
224   protected function routeBuilder() {
225     return \Drupal::service('router.builder');
226   }
227
228   /**
229    * Wraps the config factory.
230    *
231    * @return \Drupal\Core\Config\ConfigFactoryInterface
232    *   A config factory object.
233    */
234   protected function configFactory() {
235     return \Drupal::service('config.factory');
236   }
237
238   /**
239    * Wraps the search page repository.
240    *
241    * @return \Drupal\search\SearchPageRepositoryInterface
242    *   A search page repository object.
243    */
244   protected function searchPageRepository() {
245     return \Drupal::service('search.search_page_repository');
246   }
247
248   /**
249    * Wraps the search plugin manager.
250    *
251    * @return \Drupal\Component\Plugin\PluginManagerInterface
252    *   A search plugin manager object.
253    */
254   protected function searchPluginManager() {
255     return \Drupal::service('plugin.manager.search');
256   }
257
258 }