Drupal 7: Select theme depending on content-type - drupal

Suppose I have www.mycoolsite.com, and want to have a sub-section like www.mycoolsite.com/hino (yes, a car-sale with a subsection holding trucks).
The ugly fact here is, actually, divided into 2:
I'm an almost total n00b Drupal developer.
The /hino subsection must have a different layout (a different theme).
Now I created (i.e. copy-pasted, renamed, and successfully activated) another theme I already had. Both themes are active right now.
Actually, 3 themes are active right now, counting the latter: Normal theme for Desktop devices, Normal theme for Mobile devices, and Hino theme (for Desktop devices yet; the requirement for Hino/Mobile will come later).
Currently, the selected theme is Normal/Desktop unless a mobile device connects, in which case the theme is automatically switched to Normal/Mobile. BUT that's thanks to the device detector.
What I need is to render a completely different theme (i.e. the new Hino/Desktop theme) when a request is done to a node which I think it has to belong to a new content type I must create.
So, my qestion here: How do I create a new content-type and specify another theme for it? e.g. "modelos" content-type should render the Normal theme, but "modelos-hino" (a new content-type with different fields) should render the Hino theme.
Notes:
Even the page.tpl wrapper (and the menus and menu elements inside) must be different. That's why I need this instead of just creating node--modelos-hino.tpl file.
Having a separate Drupal install for /hino is NOT an option.

Based on http://drupal.org/node/224333#custom_theme
You could create a custom module and use the hook_custom_theme to load the node, check its type and trigger your new theme from there. Something like:
<?php
/**
* Implements hook_custom_theme().
*/
function mymodule_custom_theme() {
if (arg(0)=='node' && is_int(arg(1))) {
$node = node_load(arg(1));
if ($node->type == 'modelos-hino') {
return 'Hino';
}
}
}
?>

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

How to customize Wordpress Theme before going live

Yesterday I installed a new theme on Wordpress on my self-hosted website. I am aware of the feature that allows you to preview a theme and have used it to select a new Theme that I want to install.
Problem
I do not want to interrupt normal operations of my website, but this new theme requires a lot of customization before it is ready to go. How do I do this?
My Crappy Solution
Is the only way to go about it to run a virtual server on my desktop? This seems tedious, not to mention all the errors I usually get when switching to the "real" server when doing this.
A better way?
I've been searching on SO as well as the WordPress Forum for an answer as to how to do this, but have come up short. I would have thought this is a common question. Maybe I'm using the wrong search terms [themes, customization, before installing]???
Any help is greatly appreciated! Thanks!
Ok, since your question is a pretty good one and probably not a few people are going through the same process when they decide to update their site, I decided to give a try to the get_stylesheet and get_template filter hooks. It turns out that with a very small plugin, you can easily enforce a specific theme(well in this case any logged-in visitor, but you can change this to use any logic you want) according to a specific rule/s.
Here's the code that you need to put in a file in your plugins directory:
<?php
/*
Plugin Name: Switch Theme
Description: Switches the theme for logged-in visitors, while keeping the current theme for everyone else. !!!NOTE!!! Please back-up your database prior using this plugin - I can't guarantee that it will work with any theme, nor that it won't break your site's set-up - USE AT YOUR OWN RISK(I did a quick test and it seemed to be fine, but haven't done extensive testing).
You don't need to switch to the desired theme before that - you want to keep active the theme that you will display to your visitors - the one that you will see will be used programatically.
Before activating the plugin, change the line that says `private $admin_theme = '';` to `private $admin_theme = 'theme-directory-name';` where "theme-directory-name" is obviously the name of the directory in which the desired theme resides in.
*/
class MyThemeSwitcher {
private $admin_theme = '';
function MyThemeSwitcher() {
add_filter( 'stylesheet', array( &$this, 'get_stylesheet' ) );
add_filter( 'template', array( &$this, 'get_template' ) );
}
function get_stylesheet($stylesheet = '') {
if ( is_user_logged_in() && $this->admin_theme ) {
return $this->admin_theme;
}
return $stylesheet;
}
function get_template( $template ) {
if ( is_user_logged_in() && $this->admin_theme ) {
return $this->admin_theme;
}
return $template;
}
}
$theme_switcher = new MyThemeSwitcher();
So - first of all BACKUP YOUR DATABASE! I tested locally with Twenty Eleven being the default theme and a basic framework theme as my custom theme - the theme options and navigation menus were saved properly.
Then all you need to do is to update the file(change the line that says private $admin_theme = ''; to private $admin_theme = 'theme-slug'; where theme-slug is the name of the directory in which the theme you want to use is).
Also - you won't be able to change the Front page and Posts page options, without this affecting the live site, nor will you be able to change the any shared components that both themes use(Site name, Front Page, Posts page, Posts Per Page, etc options, content and so on).
So if you have no clue whether this solution is for you - well, it depends.
If both themes are not relatively complex, then most-likely you should be able to use this hack. If they are though maybe you should do a second installation of your website as others suggested - I think that a second installation in either a sub-domain or a sub-directory would be the best option for you(simply because moving a multisite database is more complex than moving a normal WP database).
I'd setup local apache server with a wordpress installed to customize and test a new theme. When you finished customizing it then you can upload the theme to your live site and activate it. If there are settings that you need to set in the dashboard then you probably will have to adjust them again. That's one way to test/customize a theme before putting it live.
You could create a network (make WordPress multisite with define('WP_ALLOW_MULTISITE', true);, see : http://codex.wordpress.org/Create_A_Network) and then create one sub-site, then turn it "off" with a Maintenance plugin so it is not accessible to users not logged in as admin, export your posts & data from main blog, import them in sub-blog with WordPress default importer, then apply your new theme to this sub-blog and work on it. When everything satisfies you, apply the theme to the main site and deactivate subsite.

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

Drupal administration theme doesn't apply to Blocks pages (admin/build/block)

A site I'm creating for a customer in D6 has various images overlaying parts of the main content area. It looks very pretty and they have to be there for the general effect.
The problem is, if you use this theme in the administration pages, the images get in the way of everything.
My solution was to create a custom admin theme, based on the default one, which has these image areas disabled in the output template files - page.tpl.php
The problem is that when you try and edit the blocks page, it uses the default theme and half the blocks admin settings are unclickable behind the images. I KNOW this is by design in Drupal, but it's annoying the hell out of me and is edging towards "bug" rather than "feature" in my mind. It also appears that there is no way of getting around it.
You can edit /modules/blocks/block.admin.inc to force Drupal to show the blocks page in the chosen admin theme. BUT whichever changes you then make will not be transferred to the default theme, as Drupal treats each theme separately and each theme can have different block layouts. :x
function block_admin_display($theme = NULL) {
global $custom_theme;
// If non-default theme configuration has been selected, set the custom theme.
// $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
// Display admin theme
$custom_theme = variable_get('admin_theme', '0');
// Fetch and sort blocks
$blocks = _block_rehash();
usort($blocks, '_block_compare');
return drupal_get_form('block_admin_display_form', $blocks, $theme);
}
Can anyone help? the only thing I can think of is to push the $content area well below the areas where the image appear and use blocks only for content display.
Thanks!
in template.php you can put
function YOURTHEME_preprocess_page(&$vars) {
if (implode('/', arg()) == 'admin/build/block'){
$vars['body_classes'] = $vars['body_classes'].' administer_block';
}
}
and you'll have a nice body class which you can use to hide those images using CSS.
If anyone's still having a problem with this, a bit similar to barraponto's solution above, if you are using the admin menu module, it adds a class (.admin-menu) to the body, which you can use to hide any overlaying divs etc that are getting in the way.
you can apply admin theme wherever you want using hook_init() in your custom module:
function yourmodule_init()
{
if ( some condition here like arg(0) == 'foobar'
or node_load(arg(1))->type == 'something' )
{
$GLOBALS['custom_theme'] = variable_get('admin_theme', '0');
drupal_add_css(drupal_get_path('module', 'system') .'/admin.css', 'module');
drupal_add_js(drupal_get_path('theme', 'myadmintheme').'/jscripts/adminjs.js');
}
}
EDIT: then (probably) you have to use form_alter against the block editing form to restore the target theme. in this way you don't have to hack the core.
Thanks for bringing up this topic! I was having the same problem, and it's annoying. Here's how to remedy it without a single line of code:
1) Switch the main theme to your administration theme.
2) Configure Blocks. This always affects the currently selected main theme.
3) Switch the main theme back to what it's supposed to be. Your admin theme will still reflect your changes.
could just use the block-admin..... tpl file from block module and theme it in your custom theme. I have done this way as admin theme module never overrides block admin even when you use custom path bit.
If you don't need your new theme while performing administration tasks, you can use a different theme while doing so.
Goto "Site Configuration" -> "Administration Theme". Here you can pick the theme to be used while doing admin. So your new theme is only used while users are viewing your site. And you can do admin tasks without the interference of all your images.

Get a node to show up underneath a View's menu item

So I've got a content type of "News" and then a View which shows a list of News nodes as a menu item.
Is there any way to highlight the News View in the menu tree when you're viewing a News node?
I'm sort of aware of why this doesn't work, and why doing this might be hacky, but there's got to be a way to have a "default" menu item or something that a node can show up under.
Actually, I found a module that does basically what the template.php answer above does, by allowing you to assign a default menu item by content type.
It's called Menu Trails. Here is an excerpt from its project page:
... adds some common-sense usability to Drupal's menu system
Menu Trails implements primary/secondary links which keep the current menu trail "active" or highlighted. A handy snippet ready to go into your template.php is included.
The module provides a means of broadly categorizing nodes (by type or taxonomy) as falling "under" a known menu item. These nodes are not added to the menu tree (keeping the menu admin system sane) but they will trigger the functionality above -- preserving navigation state for the user -- when viewed.
New for 6.0: Menu Trails can also set breadcrumbs for nodes, keeping them in sync with the trail.
New for 6.0: Menu Trails is now Organic Groups aware, so nodes can be designated to fall "under" the first group node they belong to.
New for 6.0: A token is exposed to pathauto (and other token-aware modules) allowing for the menu trail to be used in automatic path alias creation.
Assuming the answer to my above comment is "yes," this is something you can accomplish through the template.php file in your theme folder
The final solution will depend on your settings, but generally most of the action will happen inside phptemplate_preprocess_page
// Simplified, and untested in this form
function phptemplate_preprocess_page(&$vars) {
if (isset($vars['node'])) { // if we're looking at a node
$body_classes[] = (($vars['node']->type == 'News') || (arg(0) == 'News')) ? 'news_page' : ''; // Page is news page
}
else if (arg(0) == 'taxonomy') {
// ... Special handling for news taxonomy pages, if you want
} else {
$body_classes[] = "page-type-" . arg(0);
}
$body_classes = array_filter($body_classes); // Out! Out! Damn empty elements!
$vars['body_classes'] = implode(' ', $body_classes);
}
This will give the you the body.news_page style class to work with, and you can combine that with the style of your news menu block to make it appear how you like.
if Menutrails doesn't work (for me it didn't) you can do it yourself by something like:
$somenode = menu_get_item('node/5');
menu_set_item($somenode);
I have found you have more control if you do something like this in hook_nodeapi().
// this code worked for me
$somenode = menu_get_item();
$somenode['href'] = 'node/5';
menu_set_item(NULL, $somenode);
You may need to set the weight of your custom module to be heavier than core modules: at least 1

Resources