1

I am having a content type with fields select list, js, css, and body. In the select box it list a number of templates to apply on the current node. I have these custom templates in the templates folder of the active theme, in the format

my-module-blank.tpl.php, my-module-custom1.tpl.php

And on select list I have blank and custom1 as options. In the hook_preprocess_node, using theme_hook_suggestions I have did the same,

  $variables['theme_hook_suggestions'][] = 'my_module_' . $field_template_list;

But it is not taking the template, In addition I need to use hook_preprocess_html to include the custom css and js added to fields of content type to the header portion. I didn't find any proper documentation on it.

Please show me a better documentation so that I could implement.

  • Have you tried clearing cache? – Sumit Madan Apr 7 '15 at 8:08
  • @SumitMadan I wanted to know what is the purpose of html preprocess, would you mind providing a link to a good documentation on it. – Fazeela Abu Zohra Apr 7 '15 at 8:43
1

hook_preprocess_page() is called before hook_preprocess_html(). hook_preprocess_html() would probably be the last one that's run in a page build.

If you want your CSS and JavaScript when displaying a node, you can add the files it in the preprocess function for the node template.

function THEME_preprocess_node(&$variables) {
  if ($variables['view_mode'] == 'full') {
    $node =& $variables['node'];
    if ($node->type == 'something') {
      $path = drupal_get_path('theme', 'THEME');
      drupal_add_css($path . '/misc/jquery.fancybox-1.3.4.css');
      drupal_add_js($path . '/misc/jquery.mousewheel-3.0.4.pack.js', array('type' => 'file', 'scope' => 'header'));
      drupal_add_js($path . '/misc/jquery.fancybox-1.3.4.pack.js', array('type' => 'file', 'scope' => 'header'));
      drupal_add_js($path . '/misc/jquery.fancybox-calls.js', array('type' => 'file', 'scope' => 'header'));
    }
  }
}

You can add a custom template for any content type like:

node--[node-type].tpl.php

I would suggest you to use hook_preprocess_page at template.php for this:

function MYTHEME_preprocess_page(&$variables, $hook) {  
    if (isset($variables['node'])) {
        if ($variables['node']->type == 'something') {
            $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
        }
    }
}

Note: Don't forget to clear your cache after this :)

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.