Skip to main content

How to index panelizer node pages using Drupal Apache Solr module

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.

by Jibran Ijaz /

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.

Posted by Jibran Ijaz
Senior Drupal Developer

Dated

Comments

Comment by Damien McKenna

Dated

Thanks for posting this, it's good to know it doesn't take much effort to enable this.

BTW you might consider using the Panelizer variables to identify whether the entity is enabled for use with Panelizer, you could then make it generic and handle all entities, not just nodes.

Pagination

Add new comment

Restricted HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Not sure where to start? Try typing "hello" or "help" if you get stuck.