Skip to main content
Start of main content.

How to index panelizer node pages using Drupal Apache Solr module

by jibran.ijaz /

Share this post on social media

Apache Solr Search is a great module for integrating your Drupal site with the powerful Apache Solr search tool. Out of the box it can index nodes and their fields, but Panelizer pages won't be indexed. In this post I show how you can get around this by indexing the rendered HTML of a panelizer node page.

Panelizer is a very powerful module.

The panelizer module allows you to attach panels to any node in the system.

Sometimes panelizer shows private nodes in pane that can't be indexed using Solr. The Apache Solr Search module can index nodes and their fields but Solr can't index rendered HTML, which in this case is quite different.

 

First step is to take a look at the available hooks in the module. Documentation for hook_apachesolr_index_document_build says:

Build the documents before sending them to Solr.

To make your rendered HTML searchable, all you need is following piece of code:

/**
 * Implements hook_apachesolr_index_document_build().
 */
function mymodule_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
  if ($entity_type == 'node' && in_array($entity->type, array('panelizer_node', 'my_node_type'))) {
    ctools_include('node_view', 'page_manager', 'plugins/tasks');
    $output = page_manager_node_view_page($entity);
    $content = apachesolr_clean_text($output);
    $document->setField('content', $content);
  }
}

We are getting the rendered output form page_manager, cleaning it using apachesolr clean text function, and setting it as a content of apachesolr document.

That's it!

If you've got other ways of solving the problem, please let me know about it in the comments.

PS: Thanks larowlan for telling me about it.