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:
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:
Your custom function get_filtered_tags($node->taxonomy) which simply decide which of the terms will get displayed.
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.
Make a Payment to Christian Roy