1

I have two custom content entity types that I've created with a module. Neither one of these content entity types extends Node.

Our site has a search bar, and we want to include these two custom content entity types in the core search results (searching within the name of the entity only).

The only demo I can find on this topic is this, but this doesn't seem to be working for me. I've modified the code to match the identifiers and properties of my custom entity type, placed the code in the module's src/Plugin folder, cleared the cache, but my entity type still does not appear as an option in /admin/config/search/pages. Only Content and Users continue to be available search pages. Additionally I am suspicious that this work for a custom content entity type since I am trying to match search index IDs with entity IDs in the queries within the code, but I think this consistency is only maintained for node entities.

I have also tried following this method of defining a search page through Views, but none of the options this poster has selected are available when I try to make a search page for a custom content entity type (for example "Search" as a filter criteria is not present).

I can't seem to find very much useful info on this topic and am wondering if there are any resources I'm simply missing or misunderstanding.

  • 2
    My recommendation would be too look into the Search API project for searching. Not only does it support any entity type out of the box, you can even combine multiple different types into a single index. It offers a lot of flexibility to configure what is indexed, and it is easy to use Solr for perfomant searching and its additional features. – Berdir Feb 24 '17 at 16:33
  • Seconded. Don't even bother with core search. – Kevin Feb 24 '17 at 16:35
  • The "Search bar" or Search Block is set to use the default entity for searching (nodes iirc). This means that the URL action for the search is probably going to be restricted for nodes. You would probably need to create a custom block to do it. – mradcliffe Feb 24 '17 at 16:36
2

To answer the question, if an entity wants to be included in drupal search, it has to provide a search plugin, which is when discovered listed by the plugin manager from the search module. If not you should first check the annotations of the plugin, see for example the plugin from the node entity:

namespace Drupal\node\Plugin\Search;

/**
 * Handles searching for node entities using the Search module index.
 *
 * @SearchPlugin(
 *   id = "node_search",
 *   title = @Translation("Content")
 * )
 */
class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInterface, SearchIndexingInterface {

To debug this, clear the cache and list the discovered plugins:

drush ev "var_dump(\Drupal::service('plugin.manager.search')->getDefinitions())"

If all went well you have a third plugin here together with the node and user plugin and you should see it in the admin UI of the search module in the select box Search page type.


To give a recommendation, I agree with the commment from @Berdir. It's not worth the effort to write a custom search plugins, better use Search API:

This module provides a framework for easily creating searches on any entity known to Drupal, using any kind of search engine.

  • I appreciate the recommendations for Search API and am using it now. It is indexing my entities but they are still not showing up in search results. I'll have to keep trying to debug this. – saramm1 Feb 27 '17 at 14:17
  • create views for your index, add exposed filter search api fulltext, set exposed filters as block, and use this block as search form instead core search – svetlio Feb 28 '17 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.