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"),
19 * label_collection = @Translation("Search pages"),
20 * label_singular = @Translation("search page"),
21 * label_plural = @Translation("search pages"),
22 * label_count = @PluralTranslation(
23 * singular = "@count search page",
24 * plural = "@count search pages",
27 * "access" = "Drupal\search\SearchPageAccessControlHandler",
28 * "list_builder" = "Drupal\search\SearchPageListBuilder",
30 * "add" = "Drupal\search\Form\SearchPageAddForm",
31 * "edit" = "Drupal\search\Form\SearchPageEditForm",
32 * "delete" = "Drupal\Core\Entity\EntityDeleteForm"
35 * admin_permission = "administer search",
37 * "edit-form" = "/admin/config/search/pages/manage/{search_page}",
38 * "delete-form" = "/admin/config/search/pages/manage/{search_page}/delete",
39 * "enable" = "/admin/config/search/pages/manage/{search_page}/enable",
40 * "disable" = "/admin/config/search/pages/manage/{search_page}/disable",
41 * "set-default" = "/admin/config/search/pages/manage/{search_page}/set-default",
42 * "collection" = "/admin/config/search/pages",
44 * config_prefix = "page",
48 * "weight" = "weight",
61 class SearchPage extends ConfigEntityBase implements SearchPageInterface, EntityWithPluginCollectionInterface {
64 * The name (plugin ID) of the search page entity.
71 * The label of the search page entity.
78 * The configuration of the search page entity.
82 protected $configuration = [];
85 * The search plugin ID.
92 * The path this search page will appear upon.
94 * This value is appended to 'search/' when building the path.
101 * The weight of the search page.
108 * The plugin collection that stores search plugins.
110 * @var \Drupal\search\Plugin\SearchPluginCollection
112 protected $pluginCollection;
117 public function getPlugin() {
118 return $this->getPluginCollection()->get($this->plugin);
122 * Encapsulates the creation of the search page's LazyPluginCollection.
124 * @return \Drupal\Component\Plugin\LazyPluginCollection
125 * The search page's plugin collection.
127 protected function getPluginCollection() {
128 if (!$this->pluginCollection) {
129 $this->pluginCollection = new SearchPluginCollection($this->searchPluginManager(), $this->plugin, $this->configuration, $this->id());
131 return $this->pluginCollection;
137 public function getPluginCollections() {
138 return ['configuration' => $this->getPluginCollection()];
144 public function setPlugin($plugin_id) {
145 $this->plugin = $plugin_id;
146 $this->getPluginCollection()->addInstanceID($plugin_id);
152 public function isIndexable() {
153 return $this->status() && $this->getPlugin() instanceof SearchIndexingInterface;
159 public function isDefaultSearch() {
160 return $this->searchPageRepository()->getDefaultSearchPage() == $this->id();
166 public function getPath() {
173 public function getWeight() {
174 return $this->weight;
180 public function postCreate(EntityStorageInterface $storage) {
181 parent::postCreate($storage);
183 // @todo Use self::applyDefaultValue() once
184 // https://www.drupal.org/node/2004756 is in.
185 if (!isset($this->weight)) {
186 $this->weight = $this->isDefaultSearch() ? -10 : 0;
193 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
194 parent::postSave($storage, $update);
195 $this->routeBuilder()->setRebuildNeeded();
201 public static function postDelete(EntityStorageInterface $storage, array $entities) {
202 parent::postDelete($storage, $entities);
204 $search_page_repository = \Drupal::service('search.search_page_repository');
205 if (!$search_page_repository->isSearchActive()) {
206 $search_page_repository->clearDefaultSearchPage();
213 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
214 /** @var $a \Drupal\search\SearchPageInterface */
215 /** @var $b \Drupal\search\SearchPageInterface */
216 $a_status = (int) $a->status();
217 $b_status = (int) $b->status();
218 if ($a_status != $b_status) {
219 return ($a_status > $b_status) ? -1 : 1;
221 return parent::sort($a, $b);
225 * Wraps the route builder.
227 * @return \Drupal\Core\Routing\RouteBuilderInterface
228 * An object for state storage.
230 protected function routeBuilder() {
231 return \Drupal::service('router.builder');
235 * Wraps the config factory.
237 * @return \Drupal\Core\Config\ConfigFactoryInterface
238 * A config factory object.
240 protected function configFactory() {
241 return \Drupal::service('config.factory');
245 * Wraps the search page repository.
247 * @return \Drupal\search\SearchPageRepositoryInterface
248 * A search page repository object.
250 protected function searchPageRepository() {
251 return \Drupal::service('search.search_page_repository');
255 * Wraps the search plugin manager.
257 * @return \Drupal\Component\Plugin\PluginManagerInterface
258 * A search plugin manager object.
260 protected function searchPluginManager() {
261 return \Drupal::service('plugin.manager.search');