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