Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By dreadlocks1221 on
In short, I need to execute a php function in the html.tpl.php file on a particular content type and I'm curious as to the best way to do this. I tried just changing the filename to html-contenttype.tpl.php and cleared cache but that didn't seem to work.
Thank you in advance for any help you could give me ^_^
Comments
A custom html.tpl.php seems
A custom html.tpl.php seems like overkill. I suspect there is a better way to do what you are after, using a preprocess function springs to mind. What is the actual goal?
I need to insert some custom
I need to insert some custom javascript into the head portion which will change based on the node being viewed (every node in the content type will have it, but each will be different)
So the next question is how
So the next question is how is it different?
My guess is you can use hook_preprocess_page() check that it is a node page and use drupal_add_js() to add the javascript. I am thinking you can use a "generic" script and provide a setting.
It would be something along
It would be something along the lines of this:
I would use a php script like this to grab the field data from a node of a particular content type and then use it to generate javascript in the format of the following in the head portion of the page:
I was looking through the API for hook_preprocess_page() but didn't see it, is this in the D7 api somewhere?
While you could use
While you could use hook_preprocess_page() (note the reference is generic and not specific to page), in this case it would make more sense to implement hook_node_view or even write a custom field formatter.
You managed to reply before I
You managed to reply before I could expand on my code:
It would be something along the lines of this:
I would use a php script like this to grab the field data from a node of a particular content type and then use it to generate javascript in head of the page
The Result should look like this (obviously this code hasn't been fully tested yet, I am able to get a variation of it to work with 1 file into an unordered HTML list but I am confident I can get two files to work with some tweaking):
But Because I have to put this Javascript in the head portion would hook_node_view() be able to do that? Also its only for a particular content type, all others would not have these scripts included. Also note I am using $nodeplaylist = node_load(arg(1)); to basically interpret the URL to get the fields form the node being viewed (not sure if thats the best way)
I was thinking of what you said earlier about using drupal_add_js(), I am thinking that is the way to go and wondering how I can set that up so it only applies to the particular content type
Make a module that implements
Make a module that implements hook_node_view(), since one of the arguments to the function is $node, you can check $node->type and conditional run your code that way.
You can use drupal_add_js() to add inline javacsript, which will end up in the head of the page correctly. See the comment on drupal_add_js() on how to properly wrap your code in
<?php (function ($) {... })(jQuery); ?>
since $ is not longer defined by default. You generated code would replace the '...'.In all honesty I probably
In all honesty I probably don't know drupal well enough to be able to write my own module, I was hoping to just embed the php script into html.tpl.php, if I did that would $nodeplaylist = node_load(arg(1)); work to load the node fields for the node being viewed?
Also how would hook_node_view() be setup to only run on a particular content type?
Yes that would work in
Yes that would work in html.tpl.php but it is not a "best practice".
As for hook_node_view() you would want something like
_
Writing a module is probably easier than you think. Make a folder called yourmodule. In that folder create an info file called yourmodule.info and a module file called yourmodule.module. Then you can put the function nevets suggested in the module file.
Are there any blank modules I
Are there any blank modules I can use for references? Or should I just be reading through the documentation?
_
You might find this helpful:
http://drupal.org/project/examples