Newer
Older
<?php
/**
* @file
* Contains \Drupal\search\Entity\SearchPage.
*/
namespace Drupal\search\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\search\Plugin\SearchIndexingInterface;
use Drupal\search\Plugin\SearchPluginBag;
use Drupal\search\SearchPageInterface;
/**
* Defines a configured search page.
*
Alex Pott
committed
* @ConfigEntityType(
* id = "search_page",
* label = @Translation("Search page"),
* controllers = {
* "access" = "Drupal\search\SearchPageAccessController",
* "storage" = "Drupal\Core\Config\Entity\ConfigStorageController",
* "list" = "Drupal\search\SearchPageListController",
* "form" = {
* "add" = "Drupal\search\Form\SearchPageAddForm",
* "edit" = "Drupal\search\Form\SearchPageEditForm",
* "search" = "Drupal\search\Form\SearchPageForm",
* "delete" = "Drupal\search\Form\SearchPageDeleteForm"
* }
* },
* admin_permission = "administer search",
* links = {
Alex Pott
committed
* "edit-form" = "search.edit",
* "delete-form" = "search.delete",
* "enable" = "search.enable",
* "disable" = "search.disable",
* "set-default" = "search.set_default"
* },
* config_prefix = "page",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "weight" = "weight",
* "status" = "status"
* }
* )
*/
class SearchPage extends ConfigEntityBase implements SearchPageInterface, EntityWithPluginBagInterface {
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* The name (plugin ID) of the search page entity.
*
* @var string
*/
public $id;
/**
* The label of the search page entity.
*
* @var string
*/
public $label;
/**
* The UUID of the search page entity.
*
* @var string
*/
public $uuid;
/**
* The configuration of the search page entity.
*
* @var array
*/
protected $configuration = array();
/**
* The search plugin ID.
*
* @var string
*/
protected $plugin;
/**
* The path this search page will appear upon.
*
* This value is appended to 'search/' when building the path.
*
* @var string
*/
protected $path;
/**
* The weight of the search page.
*
* @var int
*/
protected $weight;
/**
* The plugin bag that stores search plugins.
*
* @var \Drupal\search\Plugin\SearchPluginBag
*/
protected $pluginBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'configuration';
/**
* {@inheritdoc}
*/
public function getPlugin() {
return $this->getPluginBag()->get($this->plugin);
}
/**
* {@inheritdoc}
*/
public function getPluginBag() {
if (!$this->pluginBag) {
$this->pluginBag = new SearchPluginBag($this->searchPluginManager(), $this->plugin, $this->configuration, $this->id());
}
return $this->pluginBag;
}
/**
* {@inheritdoc}
*/
public function setPlugin($plugin_id) {
$this->plugin = $plugin_id;
$this->getPluginBag()->addInstanceID($plugin_id);
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
}
/**
* {@inheritdoc}
*/
public function isIndexable() {
return $this->status() && $this->getPlugin() instanceof SearchIndexingInterface;
}
/**
* {@inheritdoc}
*/
public function isDefaultSearch() {
return $this->searchPageRepository()->getDefaultSearchPage() == $this->id();
}
/**
* {@inheritdoc}
*/
public function getPath() {
return $this->path;
}
/**
* {@inheritdoc}
*/
public function getWeight() {
return $this->weight;
}
/**
* {@inheritdoc}
*/
public function getExportProperties() {
$properties = parent::getExportProperties();
$names = array(
'path',
'weight',
'plugin',
'configuration',
);
foreach ($names as $name) {
$properties[$name] = $this->get($name);
}
return $properties;
}
/**
* {@inheritdoc}
*/
public function postCreate(EntityStorageControllerInterface $storage_controller) {
parent::postCreate($storage_controller);
// @todo Use self::applyDefaultValue() once https://drupal.org/node/2004756
// is in.
if (!isset($this->weight)) {
$this->weight = $this->isDefaultSearch() ? -10 : 0;
}
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) {
parent::postSave($storage_controller, $update);
$this->routeBuilder()->setRebuildNeeded();
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
parent::postDelete($storage_controller, $entities);
$search_page_repository = \Drupal::service('search.search_page_repository');
if (!$search_page_repository->isSearchActive()) {
$search_page_repository->clearDefaultSearchPage();
}
}
/**
* {@inheritdoc}
*/
public static function sort($a, $b) {
/** @var $a \Drupal\search\SearchPageInterface */
/** @var $b \Drupal\search\SearchPageInterface */
$a_status = (int) $a->status();
$b_status = (int) $b->status();
if ($a_status != $b_status) {
return ($a_status > $b_status) ? -1 : 1;
}
return parent::sort($a, $b);
}
/**
* Wraps the route builder.
* @return \Drupal\Core\Routing\RouteBuilderInterface
* An object for state storage.
*/
protected function routeBuilder() {
return \Drupal::service('router.builder');
}
/**
* Wraps the config factory.
*
Alex Pott
committed
* @return \Drupal\Core\Config\ConfigFactoryInterface
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
* A config factory object.
*/
protected function configFactory() {
return \Drupal::service('config.factory');
}
/**
* Wraps the search page repository.
*
* @return \Drupal\search\SearchPageRepositoryInterface
* A search page repository object.
*/
protected function searchPageRepository() {
return \Drupal::service('search.search_page_repository');
}
/**
* Wraps the search plugin manager.
*
* @return \Drupal\Component\Plugin\PluginManagerInterface
* A search plugin manager object.
*/
protected function searchPluginManager() {
return \Drupal::service('plugin.manager.search');
}
}