class SearchPage

Same name in this branch
  1. 9 core/modules/search/src/Plugin/migrate/source/d6/SearchPage.php \Drupal\search\Plugin\migrate\source\d6\SearchPage
  2. 9 core/modules/search/src/Plugin/migrate/source/d7/SearchPage.php \Drupal\search\Plugin\migrate\source\d7\SearchPage
Same name and namespace in other branches
  1. 11.x core/modules/search/src/Entity/SearchPage.php \Drupal\search\Entity\SearchPage
  2. 11.x core/modules/search/src/Plugin/migrate/source/d6/SearchPage.php \Drupal\search\Plugin\migrate\source\d6\SearchPage
  3. 11.x core/modules/search/src/Plugin/migrate/source/d7/SearchPage.php \Drupal\search\Plugin\migrate\source\d7\SearchPage

Defines a configured search page.

Plugin annotation


@ConfigEntityType(
  id = "search_page",
  label = @Translation("Search page"),
  label_collection = @Translation("Search pages"),
  label_singular = @Translation("search page"),
  label_plural = @Translation("search pages"),
  label_count = @PluralTranslation(
    singular = "@count search page",
    plural = "@count search pages",
  ),
  handlers = {
    "access" = "Drupal\search\SearchPageAccessControlHandler",
    "list_builder" = "Drupal\search\SearchPageListBuilder",
    "form" = {
      "add" = "Drupal\search\Form\SearchPageAddForm",
      "edit" = "Drupal\search\Form\SearchPageEditForm",
      "delete" = "Drupal\Core\Entity\EntityDeleteForm"
    }
  },
  admin_permission = "administer search",
  links = {
    "edit-form" = "/admin/config/search/pages/manage/{search_page}",
    "delete-form" = "/admin/config/search/pages/manage/{search_page}/delete",
    "enable" = "/admin/config/search/pages/manage/{search_page}/enable",
    "disable" = "/admin/config/search/pages/manage/{search_page}/disable",
    "set-default" = "/admin/config/search/pages/manage/{search_page}/set-default",
    "collection" = "/admin/config/search/pages",
  },
  config_prefix = "page",
  entity_keys = {
    "id" = "id",
    "label" = "label",
    "weight" = "weight",
    "status" = "status"
  },
  config_export = {
    "id",
    "label",
    "path",
    "weight",
    "plugin",
    "configuration",
  }
)

Hierarchy

Expanded class hierarchy of SearchPage

8 files declare their use of SearchPage
ConfigEntityImportTest.php in core/modules/system/tests/src/Kernel/Entity/ConfigEntityImportTest.php
MigrateSearchPageTest.php in core/modules/search/tests/src/Kernel/Migrate/d6/MigrateSearchPageTest.php
MigrateSearchPageTest.php in core/modules/search/tests/src/Kernel/Migrate/d7/MigrateSearchPageTest.php
SearchConfigSettingsFormTest.php in core/modules/search/tests/src/Functional/SearchConfigSettingsFormTest.php
SearchPageRepositoryTest.php in core/modules/search/tests/src/Unit/SearchPageRepositoryTest.php
Contains \Drupal\Tests\search\Unit\SearchPageRepositoryTest.

... See full list

File

core/modules/search/src/Entity/SearchPage.php, line 61

Namespace

Drupal\search\Entity
View source
class SearchPage extends ConfigEntityBase implements SearchPageInterface, EntityWithPluginCollectionInterface {
  
  /**
   * The name (plugin ID) of the search page entity.
   *
   * @var string
   */
  protected $id;
  
  /**
   * The label of the search page entity.
   *
   * @var string
   */
  protected $label;
  
  /**
   * The configuration of the search page entity.
   *
   * @var array
   */
  protected $configuration = [];
  
  /**
   * 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 collection that stores search plugins.
   *
   * @var \Drupal\search\Plugin\SearchPluginCollection
   */
  protected $pluginCollection;
  
  /**
   * {@inheritdoc}
   */
  public function getPlugin() {
    return $this->getPluginCollection()
      ->get($this->plugin);
  }
  
  /**
   * Encapsulates the creation of the search page's LazyPluginCollection.
   *
   * @return \Drupal\Component\Plugin\LazyPluginCollection
   *   The search page's plugin collection.
   */
  protected function getPluginCollection() {
    if (!$this->pluginCollection) {
      $this->pluginCollection = new SearchPluginCollection($this->searchPluginManager(), $this->plugin, $this->configuration, $this->id());
    }
    return $this->pluginCollection;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getPluginCollections() {
    return [
      'configuration' => $this->getPluginCollection(),
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function setPlugin($plugin_id) {
    $this->plugin = $plugin_id;
    $this->getPluginCollection()
      ->addInstanceID($plugin_id);
  }
  
  /**
   * {@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 postCreate(EntityStorageInterface $storage) {
    parent::postCreate($storage);
    // @todo Use self::applyDefaultValue() once
    //   https://www.drupal.org/node/2004756 is in.
    if (!isset($this->weight)) {
      $this->weight = $this->isDefaultSearch() ? -10 : 0;
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);
    $this->routeBuilder()
      ->setRebuildNeeded();
  }
  
  /**
   * {@inheritdoc}
   */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage, $entities);
    $search_page_repository = \Drupal::service('search.search_page_repository');
    if (!$search_page_repository->isSearchActive()) {
      $search_page_repository->clearDefaultSearchPage();
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
    /** @var \Drupal\search\SearchPageInterface $a */
    /** @var \Drupal\search\SearchPageInterface $b */
    $a_status = (int) $a->status();
    $b_status = (int) $b->status();
    if ($a_status != $b_status) {
      return $b_status <=> $a_status;
    }
    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.
   *
   * @return \Drupal\Core\Config\ConfigFactoryInterface
   *   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');
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.