Problem/Motivation

In node_search_execute() there is this:

$query = db_select('search_index', 'i', array('target' => 'slave'))->extend('SearchQuery')->extend('PagerDefault');

So there are two query extenders, each decorating the last. Pager needs to be last for paging to work correctly.

However, when execute() is called from PagerDefault, preExecute() is then called before execute() is called on the decorated extender (SearchQuery in this case) object. SearchQuery relies on adding a search_* tag in its execute() method.

By this point preExecute() has already been called from PagerDefault - this then runs hook_query_alter() and any hook_query_TAG_alter()s based on the tags. At this point the search_* tag has not yet been added. So unfortunately, E.g. hook_query_search_node_alter() will never get invoked. Also, hook_query_alter() implementations will never see the search_* tag.

Proposed resolution

Move the adding of search_* tags to preExecute() instead.

Remaining tasks

User interface changes

API changes

Comments

damiankloip’s picture

Status: Active » Needs review
StatusFileSize
new1.55 KB
damiankloip’s picture

Issue tags: -Needs tests
StatusFileSize
new2.8 KB
new4.34 KB

With some tests...

The last submitted patch, 2: 2364069-2-tests-only-FAIL.patch, failed testing.

fabianx’s picture

Status: Needs review » Reviewed & tested by the community

Given this has tests and is easy enough => RTBC

Did we check there is no similar bug in 8.0.x?

David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs review

Yeah, let's confirm if there is an equivalent Drupal 8 bug first and move the issue there first if so, then backport...

All the code in Drupal 8 looks very similar, so my best guess would be that the bug does exist there too, but I didn't test it.

damiankloip’s picture

StatusFileSize
new4.17 KB
new2.05 KB

No, this is not a problem in D8. It was already fixed. Looks like the fix never made it back to D7 - great! ;)

Anyway, the change in 8.0.x is to add the tag in the searchExpression method instead. So let's do that to keep parity between them. Tests should still pass because the tag will be added before preExecute() still.

fabianx’s picture

Status: Needs review » Reviewed & tested by the community

RTBC (based on tests still passing), thanks! The backport looks even better!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: 2364069-6.patch, failed testing.

Fabianx queued 6: 2364069-6.patch for re-testing.

damiankloip’s picture

Status: Needs work » Reviewed & tested by the community

So that is back then?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: 2364069-6.patch, failed testing.

Fabianx queued 6: 2364069-6.patch for re-testing.

damiankloip’s picture

Status: Needs work » Reviewed & tested by the community

And back.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: 2364069-6.patch, failed testing.

damiankloip queued 6: 2364069-6.patch for re-testing.

David_Rothstein’s picture

Status: Needs work » Reviewed & tested by the community

Testbot fluke.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: 2364069-6.patch, failed testing.

David_Rothstein queued 6: 2364069-6.patch for re-testing.

David_Rothstein’s picture

Status: Needs work » Reviewed & tested by the community

Looks like a testbot fluke.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: 2364069-6.patch, failed testing.

Fabianx queued 6: 2364069-6.patch for re-testing.

fabianx’s picture

Status: Needs work » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: 2364069-6.patch, failed testing.

damiankloip queued 6: 2364069-6.patch for re-testing.

fabianx’s picture

Status: Needs work » Reviewed & tested by the community
David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +7.36 release notes

Committed to 7.x - thanks!

Fixed a few small issues with the code comment on commit:

--- a/modules/search/search.extender.inc
+++ b/modules/search/search.extender.inc
@@ -151,13 +151,13 @@ class SearchQuery extends SelectQueryExtender {
 
     // Add a search_* tag. This needs to be added before any preExecute methods
     // for decorated queries are called, as $this->prepared will be set to TRUE
-    // and tag will added in execute will never get used. For example, if $query
-    // is extended by 'SearchQuery' then 'PagerDefault'. The search specific
-    // tag will be added too late (when preExecute() has already been called
-    // from the PagerDefault extender), and as a consequence will not be
-    // available to hook_query_alter() implementations. Nor will the correct
-    // hook_query_TAG_alter() implementations get invoked.
-    // @ see node_search_execute()
+    // and tags added in the execute method will never get used. For example,
+    // if $query is extended by 'SearchQuery' then 'PagerDefault', the
+    // search-specific tag will be added too late (when preExecute() has
+    // already been called from the PagerDefault extender), and as a
+    // consequence will not be available to hook_query_alter() implementations,
+    // nor will the correct hook_query_TAG_alter() implementations get invoked.
+    // See node_search_execute().
     $this->addTag('search_' . $module);
 
     return $this;

  • David_Rothstein committed 668db71 on 7.x
    Issue #2364069 by damiankloip: Search specific tags are not available...
David_Rothstein’s picture

Title: Search specific tags are not available and hook_query_TAG_alter hooks are not invoked » Search specific tags are not available and hook_query_TAG_alter hooks are not invoked (forward-port the tests)
Version: 7.x-dev » 8.0.x-dev
Category: Bug report » Task
Priority: Major » Normal
Status: Fixed » Patch (to be ported)

Actually, should these tests (and maybe the code comments that were added here) be forward-ported to Drupal 8?

It seems that Drupal 8 has some tests for the search query altering (see the testQueryAlter() method) but not as comprehensive as the ones here.

hass’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Patch (to be ported) » Needs work

This patch is not working properly. Try this:

  if ($query->hasTag('search_user')) {
    krumo('search_user');
  }

This goes back to #2116391: Tag user_search_execute() query for realname and other modules and #2117881: How extend query conditions via hook_query_alter()?. I hoped you fixed it here, but no - it is still broken.

damiankloip’s picture

Version: 7.x-dev » 8.0.x-dev
Status: Needs work » Patch (to be ported)

user_search_execute doesn't use the Search extender so why should it add those tags? This is working fine.

Also, your example is really bad, with no context.

hass’s picture

Also, your example is really bad, with no context.

What do you mean? Have you seen the lonk to my full example?

The code looks like a generic patch, but is not. It implements inconsistent behaviour and this is bad as every core search should have the same type of tag. User search is missing the tag for sure and I really need this for realname module.

There is a patch in my linked issue. Can you RTBC it, please?

kim.pepper’s picture

This looks like it was already done in 8.0.x #1366020: Overhaul SearchQuery; make search redirects use GET query params for keywords

Commit: 4e8713299993549a0841095dfb4ed8aebb72f903

   public function searchExpression($expression, $type) {
     $this->searchExpression = $expression;
     $this->type = $type;
 
+    // Add query tag.
+    $this->addTag('search_' . $type);
+
+    // Initialize conditions and status.
+    $this->conditions = db_and();
+    $this->status = 0;
+
     return $this;
   }

hass’s picture

damiankloip’s picture

Hass. This is a patch for search extender. So I don't see what your problem is. The issue you created looks sensible. This issue is defitiely not to blame though. This is for things using search extender.

jhodgdon’s picture

Status: Patch (to be ported) » Closed (duplicate)

This is actually a duplicate of #1435834: Cannot alter search queries, tag added too late, which was where this was fixed in 8.x and the backport has not happend yet.