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