getDefaultSearchPage example

/** * Returns an array of route objects. * * @return \Symfony\Component\Routing\Route[] * An array of route objects. */
  public function routes() {
    $routes = [];
    // @todo Decide if /search should continue to redirect to /search/$default,     // or just perform the appropriate search.     if ($default_page = $this->searchPageRepository->getDefaultSearchPage()) {
      $routes['search.view'] = new Route(
        '/search',
        [
          '_controller' => 'Drupal\search\Controller\SearchController::redirectSearchPage',
          '_title' => 'Search',
          'entity' => $default_page,
        ],
        [
          '_entity_access' => 'entity.view',
          '_permission' => 'search content',
        ],
        [
$visibility['request_path']['pages'] = 'search';
    $block->setVisibilityConfig('request_path', $visibility['request_path']);

    $this->drupalGet('');
    $this->submitForm($terms, 'Search');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Your search yielded no results');

    // Confirm that the form submits to the default search page.     /** @var \Drupal\search\SearchPageRepositoryInterface $search_page_repository */
    $search_page_repository = \Drupal::service('search.search_page_repository');
    $entity_id = $search_page_repository->getDefaultSearchPage();
    $this->assertEquals(
      $this->getUrl(),
      Url::fromRoute('search.view_' . $entity_id[]['query' => ['keys' => $terms['keys']], 'absolute' => TRUE])->toString(),
      'Submitted to correct URL.'
    );

    // Test an empty search via the block form, from the front page.     $terms = ['keys' => ''];
    $this->drupalGet('');
    $this->submitForm($terms, 'Search');
    $this->assertSession()->statusCodeEquals(200);
    
return new static(
      $container->get('search.search_page_repository')
    );
  }

  /** * {@inheritdoc} */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->derivatives = [];

    if ($default = $this->searchPageRepository->getDefaultSearchPage()) {
      $active_search_pages = $this->searchPageRepository->getActiveSearchPages();
      foreach ($this->searchPageRepository->sortSearchPages($active_search_pages) as $entity_id => $entity) {
        $this->derivatives[$entity_id] = [
          'title' => $entity->label(),
          'route_name' => 'search.view_' . $entity_id,
          'base_route' => 'search.plugins:' . $default,
          'weight' => $entity->getWeight(),
        ];
      }
    }
    return $this->derivatives;
  }
/** * {@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} */
->disableOriginalConstructor()
      ->getMock();
    $config->expects($this->once())
      ->method('get')
      ->with('default_page')
      ->willReturn('test');
    $this->configFactory->expects($this->once())
      ->method('get')
      ->with('search.settings')
      ->willReturn($config);

    $this->assertSame('test', $this->searchPageRepository->getDefaultSearchPage());
  }

  /** * Tests the getDefaultSearchPage() method when the default is inactive. */
  public function testGetDefaultSearchPageWithInactiveDefault() {
    $this->query->expects($this->once())
      ->method('condition')
      ->with('status', TRUE)
      ->willReturn($this->query);
    $this->query->expects($this->once())
      

  public function getFormId() {
    return 'search_block_form';
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state$entity_id = NULL) {
    // Set up the form to submit using GET to the correct search page.     if (!$entity_id) {
      $entity_id = $this->searchPageRepository->getDefaultSearchPage();
      // SearchPageRepository::getDefaultSearchPage() depends on       // search.settings. The dependency needs to be added before the       // conditional return, otherwise the block would get cached without the       // necessary cacheability metadata in case there is no default search page       // and would not be invalidated if that changes.       $this->renderer->addCacheableDependency($form$this->configFactory->get('search.settings'));
    }

    if (!$entity_id) {
      $form['message'] = [
        '#markup' => $this->t('Search is currently disabled'),
      ];
protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form$form_state);
    $actions['submit']['#value'] = $this->t('Save');
    return $actions;
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    // If there is no default search page, make the added search the default.     if (!$this->searchPageRepository->getDefaultSearchPage()) {
      $this->searchPageRepository->setDefaultSearchPage($this->entity);
    }

    parent::save($form$form_state);

    $this->messenger()->addStatus($this->t('The %label search page has been added.', ['%label' => $this->entity->label()]));
  }

}

  protected function assertDefaultSearch($expected, string $message = ''): void {
    /** @var \Drupal\search\SearchPageRepositoryInterface $search_page_repository */
    $search_page_repository = \Drupal::service('search.search_page_repository');
    $this->assertSame($expected$search_page_repository->getDefaultSearchPage()$message);
  }

  /** * Sets a search page as the default in the UI. * * @param string $entity_id * The search page entity ID to enable. */
  protected function setDefaultThroughUi($entity_id) {
    $this->drupalGet('admin/config/search/pages');
    preg_match('|href="([^"]+' . $entity_id . '/set-default[^"]+)"|', $this->getSession()->getPage()->getContent()$matches);

    
Home | Imprint | This part of the site doesn't use cookies.