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 * "delete" = "Drupal\Core\Entity\EntityDeleteForm"
28 * admin_permission = "administer search",
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",
37 * config_prefix = "page",
41 * "weight" = "weight",
54 class SearchPage extends ConfigEntityBase implements SearchPageInterface, EntityWithPluginCollectionInterface {
57 * The name (plugin ID) of the search page entity.
64 * The label of the search page entity.
71 * The configuration of the search page entity.
75 protected $configuration = [];
78 * The search plugin ID.
85 * The path this search page will appear upon.
87 * This value is appended to 'search/' when building the path.
94 * The weight of the search page.
101 * The plugin collection that stores search plugins.
103 * @var \Drupal\search\Plugin\SearchPluginCollection
105 protected $pluginCollection;
110 public function getPlugin() {
111 return $this->getPluginCollection()->get($this->plugin);
115 * Encapsulates the creation of the search page's LazyPluginCollection.
117 * @return \Drupal\Component\Plugin\LazyPluginCollection
118 * The search page's plugin collection.
120 protected function getPluginCollection() {
121 if (!$this->pluginCollection) {
122 $this->pluginCollection = new SearchPluginCollection($this->searchPluginManager(), $this->plugin, $this->configuration, $this->id());
124 return $this->pluginCollection;
130 public function getPluginCollections() {
131 return ['configuration' => $this->getPluginCollection()];
137 public function setPlugin($plugin_id) {
138 $this->plugin = $plugin_id;
139 $this->getPluginCollection()->addInstanceID($plugin_id);
145 public function isIndexable() {
146 return $this->status() && $this->getPlugin() instanceof SearchIndexingInterface;
152 public function isDefaultSearch() {
153 return $this->searchPageRepository()->getDefaultSearchPage() == $this->id();
159 public function getPath() {
166 public function getWeight() {
167 return $this->weight;
173 public function postCreate(EntityStorageInterface $storage) {
174 parent::postCreate($storage);
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;
186 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
187 parent::postSave($storage, $update);
188 $this->routeBuilder()->setRebuildNeeded();
194 public static function postDelete(EntityStorageInterface $storage, array $entities) {
195 parent::postDelete($storage, $entities);
197 $search_page_repository = \Drupal::service('search.search_page_repository');
198 if (!$search_page_repository->isSearchActive()) {
199 $search_page_repository->clearDefaultSearchPage();
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;
214 return parent::sort($a, $b);
218 * Wraps the route builder.
220 * @return \Drupal\Core\Routing\RouteBuilderInterface
221 * An object for state storage.
223 protected function routeBuilder() {
224 return \Drupal::service('router.builder');
228 * Wraps the config factory.
230 * @return \Drupal\Core\Config\ConfigFactoryInterface
231 * A config factory object.
233 protected function configFactory() {
234 return \Drupal::service('config.factory');
238 * Wraps the search page repository.
240 * @return \Drupal\search\SearchPageRepositoryInterface
241 * A search page repository object.
243 protected function searchPageRepository() {
244 return \Drupal::service('search.search_page_repository');
248 * Wraps the search plugin manager.
250 * @return \Drupal\Component\Plugin\PluginManagerInterface
251 * A search plugin manager object.
253 protected function searchPluginManager() {
254 return \Drupal::service('plugin.manager.search');