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.
Related
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).
A WordPress theme I am developing has an integrated custom post type called "albums" which utilizes a few custom templates (archive-albums.php, content-albums.php, etc.). What I want to do is transfer this functionality, along with the template files, into a plugin for the sake of portability.
I transferred the CPT code from the functions.php with success, but when I try to move the template files from the theme folder to the plugin folder, things fall apart. I feel like it should be simple to somehow register the templates so WordPress knows to load them.
Can my WordPress custom templates be in plugin folder or only theme folder?
Things are falling apart because when you move those files, you're violating WP's native template hierarchy. You'll need to explicitly declare the location of those files. Using the archive as an example, you could add something like this to functions.php (to tell WP to look elsewhere):
add_filter('template_include', 'include_album_template', 1);
function include_album_template($template_path) {
if(get_post_type() == 'albums') {
if(!is_single()) {
$theme_file = 'path-to-your-plugin-directory';
$template_path = $theme_file;
}
}
return $template_path;
}
Obviously you'd use your own path, and I wrote this hastily so you might want to refactor.
I have the same issue. I'm already using add_filter ('template_include', ...) problem is that I need to specify a file to return, and in this case being it,index.php. This raises an issue with the theme not running entirely as if installed via themes folder, because what I need is to have WP selecting the appropriate file to render without any conditional logic from my part. So if it is a post it will select the single.php and so on. Another problem raised with this method is that in header.php the call get_header (); ignores the local file header.php and loads the default theme installed file instead.
I'm using Drupal 6. I have some css that I want to apply only for a specific content-type. The content types machine readble name is "snap".
I copied the page.tpl.php and created another file called page-snap.tpl.php.
I have restarted apache and mysql and refresh the cache but when I look for the content type snap to use as the DIV I see nothing.
What am I doing wrong?
Thanks,
You can use $node->type to include some additional css...
if($node->type == 'snap') {
//inlcude your css file
}
Take a look at Theming a page by content type
Instructions:
Rename page-snap.tpl.php to page-node-snap.tpl.php.
Create a file called template.php in your theme folder
In template.php, add this:
<?php
function bluemarine_preprocess_page(&$variables) {
if ($variables['node']->type == "snap") {
$variables['template_files'][] = 'page-node-snap';
drupal_add_css(PATH TO CSS FILE); // Enter path to custom css here
}
}
Save the file and clear the cache (admin/settings/performance)
More about drupal_add_css()
If you want different theme for different content type you can try themekey module. Using this module you can configure theme for different content types.
this is a question how to override themable items in Drupal 6.
According to the book "Pro Drupal Development", we can override themable items in two ways:
overriding via Theme functions
overriding via Template files
So for example, in order to reformat the breadcrumb, I can:
via function theme_breadcrumb($breadcrumb)
via breadcrumb.tpl.php
But on my local testing server, the second approach (i.e. via template file) is not working! I see no breadcrumbs at all, while the first approach works fine.
Any idea how could this happen? any special settings I need to configure my Drupal?
thanks!
My custom theme "greyscale":
sites\all\themes\custom\greyscale:
- breadcrumb.tpl.php
- greyscale.info
- node.tpl.php
- page.tpl.php
- style.css
- template.php
relevant file contents:
* template.php:
function phptemplate_preprocess_breadcrumb(&$variables) {
$variables['breadcrumb_delimiter'] = '#';
}
breadcrumb.tpl.php:
Theme functions are setup to either use a template or a function to generate the markup, it will never use both as that's pointless.
For a theme function to use a template, it must be defined when you define it in hook_theme.
A template + preprocess function and a theme function really does the same thing: produce markup. It depends on the situation which method is best to use, that's why we have two. The good thing about templates, is that it allows themers to change the markup, without know much about PHP or Drupal.
Cache
Drupal caches all templates and theme functions defined in your theme, when you create new ones, you need to clear the cache, this can be done by:
Use drush
Clearing cache in admin/settings/performance
Use devel to clear it on each page load. Usable during development, biut will kill performance.
Switching theme back and forth will work too, but it really not the desired way to do it.
I personally always find it easier to alter breadcrumbs through template.php using hook_breadcrumb()
function mytheme_breadcrumb($breadcrumb) {
$sep = ' > ';
if (count($breadcrumb) > 0) {
return implode($breadcrumb, $sep) . $sep;
}
else {
return t("Home");
}
}
Any particular reason why you wish to use a .tpl.php file?
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.