Select a template through the admin area? - drupal

Up to now, I've always hard coded what page template a certain page should use, either based on the URL, or what type of node it is.
What would be really useful is if there was a way to select which tpl file to use, right there in the node edit form. This would allow the user to flick between different page layouts at will.
Does anyone know a good way to approach this problem, or a flat out solution to it?

ThemeKey will let you load a theme based on a path or other criteria. You can use other methods like utilize preprocesser functions of template.php, and hook it in with hook_form_alter and come up with a way to switch files.

I ended up adding a new vocabulary for template files (the VID for this is 2 in my case) , and then rolled this into the page preprocessor in my template.php:
function phptemplate_preprocess_page(&$vars) {
if (count($vars[node]->taxonomy)>0)
foreach ($vars[node]->taxonomy as $term)
$template = $term->vid == 2 ? $term->name : NULL;
if ($template) $vars['template_files'][] = "template-".preg_replace("/[^a-zA-Z0-9s]/", "", strtolower($template));
}
Now if I have a node in a taxonomy term called: A Green Page! it will look for template-agreenpage.tpl.php as a template file.

I was wanting this functionality as well, so I made a module to do this very thing for node templates. You can find it here: http://drupal.org/project/template-picker

Related

Place an Edit Button on the view node page in Drupal 7

I don't use the Drupal Tabs because they interfere with my CSS but I need the functionality of the Edit tab to be on that screen so that a user can edit the node after reviewing it.
Any ideas on how to do this? Functions? tpl placement? Thanks!
You can do this in a custom module as follows.
In yourcustommodule.module you implement hook_preprocess_node(). In there you check if the user has permissions to edit the node and you set the edit link.
function yourcustommodule_preprocess_node(&$vars) {
if (node_access("update", $vars['node']) === TRUE) {
$vars['edit_link']['#markup'] = l(t('Edit'), 'node/' . $vars['nid'] . '/edit');
}
}
In the node.tpl.php template in the theme you print the edit link if it is available.
<?php if (isset($edit_link)) : ?>
<p><?php print render($edit_link); ?></p>
<?php endif; ?>
If you do not have a node.tpl.php template in your theme folder than copy the one from modules/node.
If you're using the Views Format "Fields", one of the fields that you can add is "Edit Link." It's pretty flexible; it will tell you what text to display in the link. That's probably the preferred option.
If you're not using the "Fields" format, it gets trickier, especially since you're already interfering with some basic drupal styling. I'd need more information about your View and your skill set to recommend a method that doesn't cause more problems.
As a sidenote: I learned Drupal theming from the outside in, and used to use CSS that would interfere with the underlying drupal mechanics like tabs and contextual links. I've moved away from that; I find very few cases where I need to interfere with native styling-- and for those I can use custom .tpl's to get around.
EDIT: Ah. If you're not using views, a custom page .tpl is probably the best way to go. If you're not familiar, the structure for any node edit link is '/node/<NID>/edit' (for clean URL's) or '/?q=node/<NID>/edit' for old-style URL's. Depending on how your path aliases are set up, '/<url-alias>/edit' may work as well but the previous ones are more reliable.
This link on drupal.org gives a few options.
I think u can write a theme file(.tpl) for u specific case and theme page in whichever way u want

Assigning existing display template to Drupal view

How can I assign an existing template file (template file of an another view) to Drupal view.
I already have a template views-view--search-issue.tpl.php for search_issue view. Is there any way to use the same template for another view archive_issue? Or is it necessary that I have to create a new template for that one?
Then you might want to implement some preprocess hook that lets you add some suggestions. Something like this (it's not tested and maybe you'll need a different hook but to get an idea):
function phptemplate_preprocess_views_view (&$vars) {
$view = $vars['view'];
if ($view->name == 'archive_issue') {
$vars['template_files'] = 'views-view--search-issue';
}
}
But like Aniruddhsinh said, the easiest way is to just copy paste the code you need in the appropriate template. Maybe you're feeling that you're violating the DRY manta (Don't Repeat Yourself), but in this case it's better than to break the pattern for views templates. Just go with Aniruddhsinh solution.
Source: Suggested template files for views
Go to your archive_issue view and select the template file name. Create a template file with the same name from archive_issue and copy the content from views-view--search-issue.tpl.php which is for your search_issue.Paste it into this archive's template file. Clear the cache because of template changes and you will get the same template as it is in search_issue.

Displaying a Drupal content type field outside of a node

I have created a custom field on a content type in Drupal 7 which I need to display outside of the node, in a separate area on the page.
Similar to how the $title variable works (in which you can place this where you like in the page.tpl.php file) I would like to be able to create another variable called $subtitle which would call the data from the current node and allow me to print out the variable in an area on the page.tpl.php file.
I've seen a view examples seeming to use views and blocks to accomplish this task, but that seems a bit excessive and wondered if there was an easier way.
There is an easier way, you do need to bear in mind though that not every page is a node page, and not every node page will be of the right content type so you should be selective. Just add this to your theme's template.php file:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type == 'my_type') {
$vars['subtitle'] = $node->field_my_field[LANGUAGE_NONE][0]['value'];
}
}
Then in page.tpl.php you should do something like this:
if (isset($subtitle)) :
echo $subtitle;
endif;
Make sure you clear your caches (at admin/config/development/performance) once you've implemented the hook in template.php or Drupal won't pick it up.

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

What is the best way to add a content/view to a node

I am developing a module to display video. I have created a view so-called navigation for the user to select a video from a list.
Now I want o add this navigation to every node with type = 'video'. I don't know whether I should create a template for it ( then I have to put the template file in theme folder which is not so good ) or use some kind of hooks ( I haven't figured out which one to use ) ?
I tried to install http://drupal.org/project/views_attach, however the view only appeared in the content (after Title) which is not what I really want. I want it to be on top of the title.
Please help. I'm using drupal 6
Thanks in advance.
Sounds like you'd just create the navigation block any number of ways, and tell the block to only show on video node types using php in the display rules. Maybe menu_block module can help you here.
You can render the view anywhere you want in your node template with views_embed_view.
Create a template specific for your video node type: node-video.tpl.php and insert the view where you want it.
If you don't want to do it via template files, the Panels module can be used to have different layouts per node type. Once you install it, create a variant under the node_view panel and restric that variant to be selected for nodes of type 'video'
I finally found how to implement a view as block (simply select block as a content view) and I use this code below to display a block in a certain node type
<?php
$match = FALSE;
$types = array('video'=>1);
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$node = node_load(arg(1));
$match = isset($types[$node->type]);
}
return $match
}
?>

Resources