LIVE FROM THE MINNESOTA SEARCH SPRINT, this patch refactors the advanced search form and search keys so that modules can add additional "things" to search on. In the current code only node types, taxonomy terms, and languages are added to the advanced search form. Then, everywhere the search keys are processed, these three special use cases are checked for.

This patch makes the system more generic.

I've added two new hooks, hook_searchform and hook_searchkeys:

hook_searchform returns the form that gets added to the advanced search fieldset. For example,

function taxonomy_searchform() {
  if ($taxonomy = taxonomy_form_all(1)) {
    $form['category'] = array(
      '#type' => 'select',
      '#title' => t('Only in the category(s)'),
      '#prefix' => '<div class="criterion">',
      '#size' => 10,
      '#suffix' => '</div>',
      '#options' => $taxonomy,
      '#multiple' => TRUE,
    );
    return $form;
  }
}

Notice that the form returned does not included $form['advanced'] because it is relative to the advanced fieldset.

hook_searchkeys returns the keys used in this form along with the SQL to generate. hook_searchkeys may may also return additional join clause. For example,

function taxonomy_searchkeys() {
  return array(
    'category' => array(
      'where' => "tn.cid = '%s'",
      'join' => ' INNER JOIN {term_node} tn ON n.vid = tn.vid'
    ),
  );
}

I'm open to different hook names.

This patch accomplishes similar refactoring to the search_rankings patch #145242, although the two patches refactor different things for different purposes.

Comments

robertdouglass’s picture

StatusFileSize
new8.57 KB

Rerolled to avoid E_ALL errors and to remove the leading space in the 'join' bit of searchkeys. TODO includes decide if the hook names are appropriate and to write tests.

David Lesieur’s picture

StatusFileSize
new8.47 KB

An occurence of the hardcoded 'type' key was remaining in node_search_validate(), causing the language and taxonomy options to use the wrong key. Fixed.

David Lesieur’s picture

I love this patch. It makes the advanced search form customizable without adding much complexity. It also makes the core cleaner by moving module-specific search terms where they really belong, and out of the node module.

My only concern is whether the language form element really belongs to the locale module. Should it go into the translate.module instead? It is translate that handles content translation, while locale is more about the user interface language.

douggreen’s picture

Status: Needs work » Needs review

I wasn't sure about where to put language either. I'm working on the tests...

David Lesieur’s picture

Oh, just checked this more closely... The stuff that enables the language setting for content types is in locale.module. That surprises me a little, but that means the patch would be okay.

dries’s picture

Only skimmed this patch briefly and it is not up to standards yet:

* We don't use the word 'Node' in output.
* We write 'Node is sticky' not 'Node is Sticky'.
* Code comments are inconsistent.
* It would be useful to document the score functions (i.e. the math).

Gotta run now, more detailed review forthcoming. Other than the above details, it looks good.

douggreen’s picture

I think that Dries's comment above was meant for #145252, which is where we added "Node is Sticky." and where the math functions accidentally were uncommented.

Susurrus’s picture

subscribe

catch’s picture

Status: Needs review » Needs work

Shouldn't it be hook_search_form() and hook_search_keys() instead of searchform/searchkeys ?

Overall this looks like a lovely cleanup patch otherwise. No test regressions.

Susurrus’s picture

Status: Needs work » Needs review
StatusFileSize
new8.49 KB

This relates to #233476: Add search by node creation date and the author and would be nice to get into core. Here's a reroll with the changes in #9.

catch’s picture

One thing that'd be nice, would be the possibility to remove stuff from the advanced_search form too - say I don't want a full list of taxonomy terms on there etc., it's not clear to me whether that's possible with this patch. hook_search_form_alter() ? Could be a followup though if it's not already covered.

Susurrus’s picture

hook_form_alter() should still work. I'm not sure the order in which hooks are called by module, but any module's hook_form_alter() could remove items as long as it is called after node.module's hook_form_alter().

I had a thought about the search form, though. Wouldn't it be more efficient to use the specific alter hook for the search form instead of the general hook_form_alter()? That's one less function that's called every time a form is altered.

catch’s picture

Susurrus, of course! doh. And yeah the hook_$searchfromid_form_alter() version would be more efficient, especially with registry.

Susurrus’s picture

now I'm wondering if this hook should be run from search.module. This would also make all alter hooks of the form be able to remove any form item, not just those modules called after node.module.

This sound more like the right approach?

robertdouglass’s picture

@Susurrus: not sure about putting the hook in search module. The node search implementation is distinct from the search module, which is supposed to be just a framework. This distinction needs to be preserved. Nothing specific to any individual search should be put in search.module. But maybe I don't understand what you're proposing?

Also, I think we should focus on getting the current patch in as it is a clear improvement, and then proceed with further refactoring.

catch’s picture

Tested the patch and it works fine.

I noticed there's no tests for ensuring the advanced search form itself works properly - is there an issue elsewhere for that?

robertdouglass’s picture

@catch: I don't think so, and that would be a great thing to write some tests for.

catch’s picture

Opened an issue here for advanced search form tests #280794: TestingParty08: Tests needed for advanced search form. Since it ought to work the same on the surface with or without this patch, it's probably good in a separate issue.

Susurrus’s picture

@robertDouglass: I'm not proposing putting any additional fields into search.module. What I'm proposing is just moving the hooks into search.module since they're search hooks. I find it weird that node.module is calling hooks like hook_search_keys(), as I would think that would be called from search.module.

What I think should happen is that the advanced search form be supported by search.module itself instead of tacked on through node.module.

Anonymous’s picture

Status: Needs review » Needs work

The last submitted patch failed testing.

nedjo’s picture

Title: refactor advanced search form and search keywords » Enable custom advanced search operands (alongside type:, category:)

More precise title.

webchick’s picture

Subscribe. I hate search module so bad it hurts.

Looks like this patch needs a re-roll, some tests, and some docs.

douggreen’s picture

Status: Needs work » Needs review
StatusFileSize
new8.44 KB

rerolled

Status: Needs review » Needs work

The last submitted patch failed testing.

douggreen’s picture

Status: Needs work » Needs review
StatusFileSize
new8.47 KB

Simpletests rock, sorry, this patch fixes the problem and should pass all tests.

dmitrig01’s picture

Status: Needs review » Needs work

The names should be hook_search_form and hook_search_keys or hook_node_search_form and hook_node_search_keys if this is only for the node search form.

jhodgdon’s picture

Version: 7.x-dev » 8.x-dev

I'm not sure why this didn't get in, but at this point it's too late for D7...

jhodgdon’s picture

Assigned: douggreen » pwolanin

I think that we want to do this for 8.x, but the patch we have here will not work since search is now plugin-based.

klonos’s picture

The part of this issue discussed back in #3 (2008) where code was moved out of node.module etc was actually a duplicate of #237748: Decouple core search module implementations from node and user module (and turn search/node and search/user to views). I believe.

Anyways, I think we should postpone this on #2083717: Convert Search Results to Views (or close it as a duplicate or even make the other issue a META and this one one of its sub-issues).

jhodgdon’s picture

Title: Enable custom advanced search operands (alongside type:, category:) » Figure out how to allow advanced search operands (alongside type:, category:)
Category: feature » task

I'm closing #2087195: Figure out how to let the NodeSearch's supported advanced search keys be modified by developers as a duplicate of this issue, and updating the title/category here.

From the description of that issue, and the one relevant comment:

The NodeSearch class has a array of advanced search keys it support parsing from the query string.

Rather than e.g. overriding this class, it would be better if the plugin could e.g. take in other values from its config, or otherwise allowed developers to add or remove values from this list programatically.

One possible way to do it would be to add an alter hook to the manager.

pwolanin’s picture

The original proposal is outdated. We to look at how the node search plugin can be configured to look for additional filters to parse from the query string

pwolanin’s picture

Issue tags: +beta target
jhodgdon’s picture

Version: 8.0.x-dev » 8.1.x-dev
Category: Task » Feature request
Issue tags: -beta target

This is really a feature. Moving to 8.1.x for now.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mgifford’s picture

Assigned: pwolanin » Unassigned

Unassigning stale issue. Hopefully someone else will pursue this.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +stale-issue-cleanup

Thank you for sharing your idea for improving Drupal.

We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.

Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

Thanks!

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Postponed

Search isn't officially being deprecated but discussed here #3476883: [Policy, no patch] Move Search module to contrib think this is something that IF it does happen the new maintainer could decide.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.