Add JS file on a certain page on Drupal - drupal

I've got a JS file that I want to add to Admin>Add Content>Certain Content type
After looking at template.php and checking out the function theme_preprocess_node
I tried to add the JS through drupal_add_js(...) but no go.
Now, I know that there's a similar question however my case is about adding
a JS file to a certain page and nothing else (better seperation of JS files).
Thanks.
(Drupal 6)

Check out drupal_add_js() in page template not working.
The gist of it is that calling drupal_add_js() (or drupal_add_css()) during preprocess functions is basically to late, as the markup for the js/css inclusion has already been rendered into a variable. To work around this, you need to overwrite the variable again by calling drupal_get_js() after your addition:
function yourThemeName_preprocess_page(&$variables) {
// Is this a node addition page for the specific content type?
// TODO: Adjust the content type to your needs
// NOTE: Will only work for the node add form - if you want your js to be
// included for edit forms as well, you need to enhance the check accordingly
if ('node' == arg(0) && 'add' == arg(1) && 'yourContentType' == arg(2)) {
// Add your js file as usual
drupal_add_js('path/to/your/file.js', 'theme');
// Ensure that the addition has any effect by repopulating the scripts variable
$variables['scripts'] = drupal_get_js();
}
}
NOTE: Use preprocess_page, not preprocess_node for this, as javascript inclusion should happen in the page template. Also, Kevins hint on the need to rebuild the theme registry still applies (+1).

Inside your module could you just do a:
function your_module_name_init() {
if(arg(0) == "some_name") {
drupal_add_js(drupal_get_path('module', 'your_module_name') . '/your_js_file_name.js');
}
}
Something like that?

After adding the drupal_add_js, did you clear the theme/site cache? That should work.

Related

How to provide template for particular view mode?

I want to theme the search result view mode with a template.
I had it in my head that naming a template file node--article--search-result.tpl.php would do the trick, but I'm obviously wrong about that.
I realise I can do node--article.tpl.php and within this check $view_mode, but this is awkward with all the other view modes that I don't want to template.
Ideas?
Adding theme hook suggestions in a node preprocess function should do the trick:
function YOURMODULE_preprocess_node(&$vars) {
if ($vars['node']->type == 'article' && $vars['view_mode'] == 'search_result') {
$vars['theme_hook_suggestions'][] = 'node__article__search_result';
}
}
After clear the cache you should be able to use node--article--search-result.tpl.php for your template file name.
Hope it will help you. Happy coding.

Load CSS and JS files only to a specific module Drupal

I’m developing a custom module, but need that only in the page where I use this module add some CSS and JavaScript files, I know how to do this from template.php using a condition to specific node, but I need do it more generic, that the condition can stay in .module file, or some similar solution.
I have a little code in my .module file:
function mymodule_init() {
$url = request_uri();
$path = drupal_get_path('module', 'module_name');
if ($url == 'xxxx') {
drupal_add_js($path . '/js/animations.js');
}
}
This work very well, but I’m limiting, because I need know the URL where the module will used.
Rather than checking for the URL, you should add your drupal_add_js when you initialize the code your module will be using on a given page.
For example, if you are adding a block, you could add it to hook_block_view. If you're adding a theme template, you could do it in hook_preprocess for that theme template.

custom html in drupal subtheme

I'm just starting out with drupal and need some custom html for an intricate menu system. My plan is to override the html-generating functions in template.php.
My theme name is "Drupal subtheme" and the navbar I would like to target has the machine name "menu-usm-navbar-small". What should I name the functions that overrides the default html-printouts?
I think I have tried every possible combination of these. Some examples of what I've tried:
function drupal_subtheme_menu_link($variables) {
return "foo";
}
function drupal_subtheme__menu_usm_navbar_small($variables) {
return "foo";
}
If you want to place custom HTML inside, why do you need to do it trough Drupal's functions?
Let's say, that you want to insert your code into page.tpl.php (most likely) - just open that file, edit it, add your code there.
Since you are overriding some theme - copy that file from original theme, and then edit it (don't forget to clear the cache).

custom page-xxxx.tpl.php doesnt works

I have page named page--news.tpl.php, which i created for my news page. But after i cleared my cache, page still not using, and drupal use the original page.tpl.php. Any ideas how to solve it?
An alternate way of doing it, is through preprocess hook with few lines of code.
Here's how it goes
function <module_name>_preprocess_page(&$variables) {
if (isset($variables['node'])) {
$variables['theme_hook_suggestions'][] = 'page__'.$variables['node']->type;
}
}
Suppose you have a node type as "news" then tpl should look like 'page--news.tpl.php' and above code will handle the rest.

Drupal7: Trying to theme a specific page using a preprocess function, but...I get a blank screen instead

I've just discovered that if you want to alter a specific page (or group of pages) all you need is to add templates file to the core templates. For instance, I need to theme my /helloword page using a page--helloworld.tpl.php and node--helloworld.tpl.php template files.
Now all I get is a blank screen so I tried to write a preprocess function that adds support for custom theme files like:
<?php
/**
* Adding or modifying variables before page render.
*/
function phptemplate_preprocess_page(&$vars) {
// Page change based on node->type
// Add a new page-TYPE template to the list of templates used
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use doublehyphens in Drupal7.
$vars['template_files'][] = 'page--'. str_replace('_', '-', $vars['node']->type);
}
}
?>
I see no syntax error but I still get a blank screen. Still no luck
Is someone able to figure out what's wrong in the code/routine?
Drupal7 + Omega Sub-Theme
Kind Regards
I think there's a tiny bit of confusion here: a template file named node--type.tpl.php will automatically be called for any node which has the type type...you don't need to add the template suggestions in yourself.
There is one caveat to this, you have to copy the original node.tpl.php to your theme folder and clear your caches otherwise Drupal won't pick it up.
Also you don't want to use the phptemplate_ prefix...rather you want your function to be called MYTHEMENAME_preprocess_page.
Your code to add the page template based on the node type looks spot on, see if you still have the problem after you change your function name and clear the caches.
Hope that helps :)

Resources