Drupal Features include Theme - drupal

Is it possible to include a theme in a Drupal Feature? if so how?

Not at the moment, unfortunately. Features basically consist of things that can be cleanly exported out of and imported into Drupal via various event hooks. Themes are an entirely different animal.
Theoretically, if you want to override some markup in your Feature (custom tpl.php files for your own content type for example), you could include the custom tpl.php file and use theme-related hooks in the Feature's module file to let Drupal know that the templates are in your module's directory.

In addition to Eaton's answer. If you need to override an existing template (a .tpl.php file) provided by another module you can use hook_theme_registry_alter in YOUR_FEATURE.module:
function YOUR_FEATURE_registry_alter($theme_registry) {
$originalpath = array_shift($theme_registry['TEMPLATE']['theme paths']);
$featurepath = drupal_get_path('module', 'YOUR_FEATURE') .'/themes');
array_unshift($theme_registry['TEMPLATE']['theme paths'], $originalpath, $featurepath);
}
In order for this to work, your feature should have a weight greater than the one of the module providing the overrided template. So in YOUR_FEATURE.install you will have something like
function YOUR_FEATURE_install() {
db_query("UPDATE {system} SET weight = 10 WHERE name = 'YOUR_FEATURE'");
}

Related

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).

hook_preprocess_page() does not seem to use the suggested template file

I am suggesting a template file in the hook_preprocess_page() implementation done from a module, but the suggested template file doesn't seem to be used.
The template file is page--terminal-template.tpl.php, which is in the directory containing the module, and this is the implementation of hook_preprocess_page().
function terminal_preprocess_page(&$variables) {
if (arg(0) == "terminal") {
$variables['theme_hook_suggestions'][] = "page__terminal_template";
}
}
Could anyone please help me?
Preprocess and process functions can be implemented by modules. In fact, the documentation for theme() lists them when it shows in which order those functions are called.
The fact is that Drupal looks for the suggested template files in the theme directory. You have these alternatives:
Put the template files your module is suggesting in the directory containing the theme currently used
Follow what reported in Load view template on module activation to load the template files from the module directory
Suggest the template files you want to use in a preprocess function implemented by a theme
Following what reported in the other question, you would be able to use the template file found in the module directory. The only problem is that you would be using a generic template that could be different from the default page template used from the currently enabled theme.
If you are adding template files for the currently enabled theme, you should call drupal_theme_rebuild() to make Drupal rescan the directory containing the template files, after you added the new template file to the theme.
Actually, this hook can also be called from theme's template.php file along with module's hook.
Please refer Drupal 7 documentation here.
Say if your active theme is MY_THEME, then the code should be:
function MY_THEME_preprocess_page(&$variables) {
if (arg(0) == "terminal") {
$variables['theme_hook_suggestions'][] = "page__terminal_template";
}
}
And the template suggestions will work.
Edit: This functionality can also be implemented with Modules using hooks.

Rewrite default Drupal view programmatically

Say we have a defaul view (i.e hardcoded), provided by Views module , for example "taxonomy/term/%"
Now, I'd like to make some modification to that view programmatically, through an installation profile
Normally I use Features module for such work, but Features does not support default views.
Please advise how to do that.
Thanks!
Use hook_views_default_views_alter
function MODULE_views_default_views_alter(&$views) {
if (isset($views['taxonomy_term'])) {
$views['taxonomy_term']->set_display('default');
$views['taxonomy_term']->display_handler->set_option('title', 'Categories');
}
}
You should use the Views theme information. There is a link you can use to find out what you should name your views (Its called "Theme Information") copy the name of the particular part of the view you would like to hardcode and paste it as a new file in your template's directory. You can use a folder (I usually name it views) to separate these files from others in the template. You'll need to refresh your cache to see the changes once you've created the new template file(s).
Yes, use hook_views_default_views_alter()
Here's a good example:
enter link description here

Why is Drupal not aware of my template file?

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?

How do you modify the fields output in Drupal's RSS Feeds

Trying to modify the RSS feeds created by Views module in Drupal.
Since there are no 'theme_' hooks for the RSS feeds (rightfully as XML is theme-less), I need an alternate way to modify the fields that are output into the RSS, preferably using template.php if possible.
http://api.drupal.org/api/function/format_rss_item/6 looks promising as that is where each row is created, but it doesn't
node_feed() is what collects the nodes, creates the additional fields, and then calls format_rss_item().
Specifically, we need to remove the dc:creator element from the $extra array created in node_feed()
If you go into a specific content type, you can set the RSS display fields as per this post:
http://redfinsolutions.com/redfin-blog/show-hide-fields-views-generated-drupal-rss-feed
I am adding another answer, as I have recently had to do this and managed to do it without modifying data in the theme layer.
You can add a preprocess function to your view. It is a bit of work though.
There are some guidelines here, but they are a bit confusing. Here is my summary of how to do this.
First make sure your modules weight is > views
Secondly copy the template you would like to add a preprocessor to to your modules directory. Rename it to be something in the list of templates in theming information.
Then edit hook theme like this (but change to use the existing view that you need to override.
function mymodule_theme($existing, $type, $theme, $path) {
// Copy the exsisting views theme from the existing array.
$override = $existing['views_view_row_rss'];
// Add my preprocessor to the list of preprocess functions
$override['preprocess functions'][] = 'mymodule_myfunction';
// Point to a template so that this view will be used.
$override['template'] = 'my_more_specific_template_name';
$override['path'] = drupal_get_path('module', 'path');
unset($override['file']);
// Return your theme handler.
return array('my_more_specific_template_name' => $override);
}
You should then be able to write your preprocess code in the function mymodule_myfunction.
In the view, if you click on "style information" this will show you the template files used to create the feed. You can copy the template so that it is overridden for your view and remove dc:creator from the $item_elements array.
This is not particularly nice as you are modifying data in the theme layer, but it will do what you want.
I'd suggest using the Views Node Feed module to do this. It will let you completely write the XML that is output by Views for feeds.

Resources