3 namespace Drupal\search\Entity;
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;
14 * Defines a configured search page.
18 * label = @Translation("Search page"),
20 * "access" = "Drupal\search\SearchPageAccessControlHandler",
21 * "list_builder" = "Drupal\search\SearchPageListBuilder",
23 * "add" = "Drupal\search\Form\SearchPageAddForm",
24 * "edit" = "Drupal\search\Form\SearchPageEditForm",
25 * "search" = "Drupal\search\Form\SearchPageForm",
26 * "delete" = "Drupal\Core\Entity\EntityDeleteForm"
29 * admin_permission = "administer search",
31 * "edit-form" = "/admin/config/search/pages/manage/{search_page}",
32 * "delete-form" = "/admin/config/search/pages/manage/{search_page}/delete",
33 * "enable" = "/admin/config/search/pages/manage/{search_page}/enable",
34 * "disable" = "/admin/config/search/pages/manage/{search_page}/disable",
35 * "set-default" = "/admin/config/search/pages/manage/{search_page}/set-default",
36 * "collection" = "/admin/config/search/pages",
38 * config_prefix = "page",
42 * "weight" = "weight",
55 class SearchPage extends ConfigEntityBase implements SearchPageInterface, EntityWithPluginCollectionInterface {
58 * The name (plugin ID) of the search page entity.
65 * The label of the search page entity.
72 * The configuration of the search page entity.
76 protected $configuration = [];
79 * The search plugin ID.
86 * The path this search page will appear upon.
88 * This value is appended to 'search/' when building the path.
95 * The weight of the search page.
102 * The plugin collection that stores search plugins.
104 * @var \Drupal\search\Plugin\SearchPluginCollection
106 protected $pluginCollection;
111 public function getPlugin() {
112 return $this->getPluginCollection()->get($this->plugin);
116 * Encapsulates the creation of the search page's LazyPluginCollection.
118 * @return \Drupal\Component\Plugin\LazyPluginCollection
119 * The search page's plugin collection.
121 protected function getPluginCollection() {
122 if (!$this->pluginCollection) {
123 $this->pluginCollection = new SearchPluginCollection($this->searchPluginManager(), $this->plugin, $this->configuration, $this->id());
125 return $this->pluginCollection;
131 public function getPluginCollections() {
132 return ['configuration' => $this->getPluginCollection()];
138 public function setPlugin($plugin_id) {
139 $this->plugin = $plugin_id;
140 $this->getPluginCollection()->addInstanceID($plugin_id);
146 public function isIndexable() {
147 return $this->status() && $this->getPlugin() instanceof SearchIndexingInterface;
153 public function isDefaultSearch() {
154 return $this->searchPageRepository()->getDefaultSearchPage() == $this->id();
160 public function getPath() {
167 public function getWeight() {
168 return $this->weight;
174 public function postCreate(EntityStorageInterface $storage) {
175 parent::postCreate($storage);
177 // @todo Use self::applyDefaultValue() once
178 // https://www.drupal.org/node/2004756 is in.
179 if (!isset($this->weight)) {
180 $this->weight = $this->isDefaultSearch() ? -10 : 0;
187 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
188 parent::postSave($storage, $update);
189 $this->routeBuilder()->setRebuildNeeded();
195 public static function postDelete(EntityStorageInterface $storage, array $entities) {
196 parent::postDelete($storage, $entities);
198 $search_page_repository = \Drupal::service('search.search_page_repository');
199 if (!$search_page_repository->isSearchActive()) {
200 $search_page_repository->clearDefaultSearchPage();
207 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
208 /** @var $a \Drupal\search\SearchPageInterface */
209 /** @var $b \Drupal\search\SearchPageInterface */
210 $a_status = (int) $a->status();
211 $b_status = (int) $b->status();
212 if ($a_status != $b_status) {
213 return ($a_status > $b_status) ? -1 : 1;
215 return parent::sort($a, $b);
219 * Wraps the route builder.
221 * @return \Drupal\Core\Routing\RouteBuilderInterface
222 * An object for state storage.
224 protected function routeBuilder() {
225 return \Drupal::service('router.builder');
229 * Wraps the config factory.
231 * @return \Drupal\Core\Config\ConfigFactoryInterface
232 * A config factory object.
234 protected function configFactory() {
235 return \Drupal::service('config.factory');
239 * Wraps the search page repository.
241 * @return \Drupal\search\SearchPageRepositoryInterface
242 * A search page repository object.
244 protected function searchPageRepository() {
245 return \Drupal::service('search.search_page_repository');
249 * Wraps the search plugin manager.
251 * @return \Drupal\Component\Plugin\PluginManagerInterface
252 * A search plugin manager object.
254 protected function searchPluginManager() {
255 return \Drupal::service('plugin.manager.search');