Drupal Indexing - node.tpl.php is for layout only

If you are working on drupal, I am sure you were asked at one point to display additionnal data on a node page. Or maybe to filter the data. For example, only show tags from some vocabularies, but not all of them. The very first reaction might be to modify node.tpl.php to call some function that will return the new data and echo the data.

Let's assume (for simplicity) that the defualt node.tpl.php looks like this by default:

<div class="node">
  <h2><?php echo $title ?></h2>
  <?php echo $author ?> wrote on <?php $node->created ?><br />
  <?php echo $content ?><br />
  <?php echo $terms ?><br />

The $terms variable is being created from $node->taxonomy. This magic occurs in phptemplate.engine.

To accomodate your client's requirement, you might be tempted to do this:

<div class="node">
  <h2><?php echo $title ?></h2>
  <?php echo $author ?> wrote on <?php $node->created ?><br />
  <?php echo $content ?><br />
  <?php echo get_filtered_tags($node->taxonomy) ?><br />
  <?php echo get_extra_data($node) ?><br />

Your custom function get_filtered_tags($node->taxonomy) which simply decide which of the terms will get displayed.
And your custom function get_extra_data($node) might connect to a legacy DB to get related information.
That works... until you search (using drupal search) for that data and you cannot find the article you are looking for.

My first suggestion is: Did you run cron recently? Content will not appear in the search result until it has been indexed. If you check the search settings you will see how much of your content has been indexed so far and how much is left. Content is only indexed when cron is executed.

If you added the data directly in node.tpl.php, then no matter how many time you run cron, you will not find the articles by searching by one of the extra data (unless those words are also in the title, body or taxonomy, or CCK). In this case, let's say that the extra data that is NOT in any of these places.

As a new drupal developer, I thought that the search was an SQL query that looked in the "node" table.

$result = db_query("SELECT * FROM node WHERE title like '%%%s%%'", $form_values['keys']);

Nope. I was wrong. Instead it looks in the search_dataset, search_index and search_total. When drupal cron is executed, it will add every article to these tables. If the article has not been indexed into these tables, it will not show in the search results.

How new articles get indexed?

Drupal calls all the hooks_nodeapi("view", $node) which popular the $node->content with HTML. Each module can add stuff to that. The search module then extract every words from the $node->content and adds these words in the search_index table. The cck module adds all the fields to the $node->content array. That is why the CCK fields gets indexed by the search engine. The taxonomy module does not add anything to the $node->content array. So how do they get indexed? The search module calls another hook after hook_nodeapi("view", $node). It calls hook_nodeapi("update index", $node) which allow all modules to return HTML that will be indexed.

The data returned by get_extra_data($node) will not get indexed since it is NOT being added to the $node->content array when hook_nodeapi('view', $node) is being called.

You have two choices. Either you implement nodeapi hook that handles "update index" operation and return the value form your custom function. Or you remove that from node.tpl.php and you implement a nodeapi hook that handles "view" operation which will handle both getting that in the $content variable (it will be displayed) and handle the search indexing.

The second option is preferable since you only have one place to change if you remove the extra data. And with the second option, you keep the node.tpl.php as it was originally. No need to modify it.

To learn more about Drupal search, check out the "Pro Drupal Development" book written by the great Matt Westgate.
You can also check this article about D6: http://acquia.com/blog/drupal-search-how-indexing-works

The complete guidance on

The complete guidance on indexing. The codes and step by step procedure on indexing will be really very helpful. I also think that Drupal indexing provides a quantitative analysis of Drupal module which make them more interactive too. Thanks for posting.


Interesting, thanks to you.


Nice article.

Really Nice. I have had the opportunity sometimes to kind of edit the page.tpl.php file.
this is interesting stuf. thanks for the heads up.


whenever i see so much

whenever i see so much scripting needed to be done i get goose skin, lol
good post btw

The lesson on drupal

The lesson on drupal indexing - node.tpl.php is for layout is an excellent tutorial. I've found the post very useful and informative. keep it up.

I am very benefited by the

I am very benefited by the lesson on drupal indexing I have been waiting for. It is a useful discussion. keep it up.

I am quite impressed with

I am quite impressed with the designing structure of drupal,it is easy to use and also flexible, thanks for your valuable tips.

Drupal indexing provides a

Drupal indexing provides a quantitative analysis of Drupal module which make them more interactive too.This base is all about adding additional information about indexing on the webpage while working on Drupal platform.

Hy, very interesting &

Hy, very interesting & informative blog, gud job done. thanks

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You may post PHP code. You should include <?php ?> tags.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options