setSearch example

foreach ($node_ranks as $node_rank) {
      // Enable the ranking we are testing.       $edit['rankings[' . $node_rank . '][value]'] = 10;
      $this->drupalGet('admin/config/search/pages/manage/node_search');
      $this->submitForm($edit, 'Save search page');
      $this->drupalGet('admin/config/search/pages/manage/node_search');
      $this->assertSession()->optionExists('edit-rankings-' . $node_rank . '-value', '10');

      // Reload the plugin to get the up-to-date values.       $this->nodeSearch = SearchPage::load('node_search');
      // Do the search and assert the results.       $this->nodeSearch->getPlugin()->setSearch('rocks', [][]);
      $set = $this->nodeSearch->getPlugin()->execute();
      $this->assertEquals($nodes[$node_rank][1]->id()$set[0]['node']->id(), 'Search ranking "' . $node_rank . '" order.');

      // Clear this ranking for the next test.       $edit['rankings[' . $node_rank . '][value]'] = 0;
    }

    // Save the final node_rank change then check that all rankings are visible     // and have been set back to 0.     $this->drupalGet('admin/config/search/pages/manage/node_search');
    $this->submitForm($edit, 'Save search page');
    
// Enable views ranking.     $edit['rankings[views][value]'] = 10;
    $this->drupalGet('admin/config/search/pages/manage/node_search');
    $this->submitForm($edit, 'Save search page');
    $this->drupalGet('admin/config/search/pages/manage/node_search');
    $this->assertSession()->optionExists('edit-rankings-views-value', '10');

    // Reload the plugin to get the up-to-date values.     $this->nodeSearch = SearchPage::load('node_search');
    // Do the search and assert the results.     $this->nodeSearch->getPlugin()->setSearch('rocks', [][]);
    $set = $this->nodeSearch->getPlugin()->execute();
    $this->assertEquals($nodes['views'][1]->id()$set[0]['node']->id());
  }

}

class SearchExtraTypeSearch extends ConfigurableSearchPluginBase {

  /** * {@inheritdoc} */
  public function setSearch($keywords, array $parameters, array $attributes) {
    if (empty($parameters['search_conditions'])) {
      $parameters['search_conditions'] = '';
    }
    parent::setSearch($keywords$parameters$attributes);
    return $this;
  }

  /** * Verifies if the given parameters are valid enough to execute a search for. * * @return bool * TRUE if there are keywords or search conditions in the query. */
  public function isSearchExecutable() {
    return (bool) ($this->keywords || !empty($this->searchParameters['search_conditions']));
  }

  public function view(Request $request, SearchPageInterface $entity) {
    $build = [];
    $plugin = $entity->getPlugin();

    // Build the form first, because it may redirect during the submit,     // and we don't want to build the results based on last time's request.     $build['#cache']['contexts'][] = 'url.query_args:keys';
    if ($request->query->has('keys')) {
      $keys = trim($request->query->get('keys'));
      $plugin->setSearch($keys$request->query->all()$request->attributes->all());
    }

    $build['#title'] = $plugin->suggestedTitle();
    $build['search_form'] = $this->formBuilder()->getForm(SearchPageForm::class$entity);

    // Build search results, if keywords or other search parameters are in the     // GET parameters. Note that we need to try the search if 'keys' is in     // there at all, vs. being empty, due to advanced search.     $results = [];
    if ($request->query->has('keys')) {
      if ($plugin->isSearchExecutable()) {
        
$criteria = new ExtensionCriteria();

        if (isset($parameter['limit'])) {
            $criteria->setLimit((int) $parameter['limit']);
        }

        if (isset($parameter['page'])) {
            $criteria->setOffset(((int) $parameter['page'] - 1) * $criteria->getLimit());
        }

        if (isset($parameter['term'])) {
            $criteria->setSearch($parameter['term']);
        }

        $sorting = $parameter['sort'][0] ?? null;
        if ($sorting !== null) {
            $criteria->setOrderBy($sorting['field']);
            $criteria->setOrderSequence($sorting['order']);
        }

        if (isset($parameter['filter'])) {
            foreach ($parameter['filter'] as $filter) {
                $criteria->addFilter($filter);
            }
static::assertEquals(
            ExtensionCriteria::ORDER_SEQUENCE_ASC,
            $extensionCriteria->getQueryParameter()['orderSequence']
        );
    }

    public function testToQueryStringSkipsSearchIfNotPresent(): void
    {
        $extensionCriteria = new ExtensionCriteria();
        static::assertArrayNotHasKey('search', $extensionCriteria->getQueryParameter());

        $extensionCriteria->setSearch('my search');
        static::assertEquals('my search', $extensionCriteria->getQueryParameter()['search']);
        static::assertEquals('my search', $extensionCriteria->getSearch());
    }

    public function testItFlattensFilterInQuery(): void
    {
        $extensionCriteria = new ExtensionCriteria();
        $extensionCriteria->addFilter([
            'type' => 'multi',
            'operator' => 'AND',
            'queries' => [
                [
    $this->drupalCreateNode(['body' => [['value' => 'tapir']]]);
    // Update the search index.     $this->nodeSearchPlugin->updateIndex();
  }

  /** * Verify that search works with a numeric locale set. */
  public function testSearchWithNumericLocale() {
    // French decimal point is comma.     setlocale(LC_NUMERIC, 'fr_FR');
    $this->nodeSearchPlugin->setSearch('tapir', [][]);
    // The call to execute will throw an exception if a float in the wrong     // format is passed in the query to the database, so an assertion is not     // necessary here.     $this->nodeSearchPlugin->execute();
  }

}
// Click the reindex button on the admin page, verify counts, and reindex.     $this->drupalGet('admin/config/search/pages');
    $this->submitForm([], 'Re-index site');
    $this->submitForm([], 'Re-index site');
    $this->assertIndexCounts(8, 8, 'after reindex');
    $this->assertDatabaseCounts(8, 0, 'after reindex');
    $this->plugin->updateIndex();

    // Test search results.
    // This should find two results for the second and third node.     $this->plugin->setSearch('English OR Hungarian', [][]);
    $search_result = $this->plugin->execute();
    $this->assertCount(2, $search_result, 'Found two results.');
    // Nodes are saved directly after each other and have the same created time     // so testing for the order is not possible.     $results = [$search_result[0]['title']$search_result[1]['title']];
    $this->assertContains('Third node this is the Hungarian title', $results, 'The search finds the correct Hungarian title.');
    $this->assertContains('Second node this is the English title', $results, 'The search finds the correct English title.');

    // Now filter for Hungarian results only.     $this->plugin->setSearch('English OR Hungarian', ['f' => ['language:hu']][]);
    $search_result = $this->plugin->execute();

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