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