comparison core/modules/search/src/Entity/SearchPage.php @ 0:4c8ae668cc8c

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