0

I'm using hook_preprocess_node in a module. In that, I check whether the node type, and on satisfying the condition I'm doing a theme_hook_suggestions. My tpl.php files are inside my modules template folder, It is not taking the tpl file I have suggested. I have searched a lot, but didn't find any solutions

Any solution would be apprecaited Here is my code

/**
 * Implements hook_preprocess_node().
 */
function mymodule_preprocess_node(&$varaiables) {
  if (isset($varaiables['node'])) {
    $node = $varaiables['node'];
    $field_template_list = $node->field_template_list[$node->language][0]['value'];
      if ($node->type == 'event') {
        $varaiables['theme_hook_suggestions'][] = 'module_name_' . $field_template_list;
      }
   }
}
  • 1
    Can you be sure $node->field_template_list[$node->language][0]['value'] always has a value? Also, what is in that field? You'll want to make sure to handle any punctuation etc. that users might be able to enter. – rooby Apr 2 '15 at 5:18
  • 1
    What is the file name of the template file you are trying to use that isn't working as you expect? – rooby Apr 2 '15 at 5:19
  • @rooby. 1. I have dpm the value. It have value. 2. File name I'm using is module-name-$field_template_list.tpl.php. Sorry there is a mistake in the code I have included. I'll update. – Fazeela Abu Zohra Apr 2 '15 at 5:23
2

Drupal will not look for template overrides in custom modules, only themes.

If you really want to put a template override in a custom module see the answer to this question.

Extra notes are:

  • Make sure to handle punctuation and any other undesirable characters in your field value.
  • field_get_items() is a better way to get the value of a field from a node.
  • It is generally best to keep your template suggestions in line with what is already there. For example starting this suggestion with 'node__' followed by your custom stuff, like maybe 'node__event__' . $field_template_list or something like that. It makes it easier in future to know what kind of template it is.

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.