Drupal 7 How to override page.tpl for specific content type? - drupal

I wanted to override page.tpl.php for specific content type.
I have tried these thing, nothing works for me.
page--article.tpl.php
page--node--article.tpl.php
page--node--type--article.tpl.php
page--node-type--article.tpl.php
page--type--article.tpl.php
But when I targetted specific node by number i.e. page--node--8.tpl.php it worked fine.
I think page--article.tpl.php should have worked, but I dont know why its not working.
Please tell me if I am naming it wrong. and how can I debug such things. I have heard I can use Devel module, but know nothing about it. A slight hint in right direction will be appreciated.
Thanks in advance

You have to make sure that your template can handle this ... I got a code snippet that worked for me here:
http://drupal.org/node/1089656#comment-4426790
<?php
function themeName_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
// If the node type is "blog_madness" the template suggestion will be "page--blog-madness.tpl.php".
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
}
}
?>

Have you remembered to clear the cache (under Administer > Site configuration > Performance) so that Drupal is 'aware' of your new file & uses this rather than the default.
See here on Clearing cached data
Also, you may need to add a preprocess hook (I haven't used D7 myself, just 5/6 — think this has changed slightly.) This post on Drupal Stack Exchange seems to give more details

I just ran into the same problem.
node--recipe.tpl.php
not
page--recipe.tpl.php
this is, of course, with a content type called 'recipe'.

ALWAYS CLEAR THE CACHE...
Go to /admin/config/development/performance and click the clear cache button. It should be the first thing you do if you've made alterations to the structure of the backend and they're not showing as expected.
Otherwise you almost had it right
page--content-type-name.tpl.php
will allow you to theme the content type template.

I know this is an old question but if others arrive here, the way I override content types in drupal 7 is like this
node--contenttype.tpl

You will need to overwrite the specific node template for the content type if it is anything different than a Basic Page. Please check this out:
http://api.drupal.org/node/19080
Under this page copy/paste the items that are in the node.tpl.php page and overwrite it on how you want. You can access the node specifically by $node
Your template for articles would be:
node--article.tpl.php
Once this is created make sure you clear cache to ensure this works.

So depending on what you are trying to accomplish, there might be some CSS tricks that could help you out.
For instance, to override any CSS rules for a specific node type just remember that your pages for that node type will have a body class of "node-type-[TYPE]" (ie: .node-type-article)
You can then override the CSS as follows:
body.node-type-article #selector {}

Step 1.create a content type for example: my content type is "fullwidthpage"
Step 2. copy page.tpl.php and rename to page--fullwidthpage.tpl in template folder.
Step 3. Add this code to template.php
function <strong>YOUR_THEME_NAME_HERE</strong>_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
// If the content type's machine name is "my_machine_name" the file
// name will be "page--my-machine-name.tpl.php".
$variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
}
}
Don't forgot to change YOUR_THEME_NAME_HERE

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

omega theme, file node--type.tpl.php

I'm using the theme of Omega 3. I would like to review the layout of certain fields inside my content type "video". I made so the file: node--video.tpl.php:
<? php print render ($ content ['body']); ?>
yet even this simple statement does not work. Does anyone know the reason? No review of the design of the node with omega 3?
for better debugging, try to install devel module and theme developer module.
Both can be your best friends till you solve this issue.
Hope this helps... Muhammad.
I understand why it did not work. Going up: structure -> content type -> manage display, the fields that I want to print with php hidden. Putting them visible both on the "teaser" that the "default" everything works as it should.

Hooking basic pages in drupal 7

I have few basic pages in drupal 7, named about, contacts. I tried to make page--about, and style it, but it doesnt work. Any advice on how to force hook basic pages?
In your themes template file you can get template suggestions. This was you can check there isnt anything weird going on. The following will dump out suggestions for templates.
You need to use the following:
function YOURTHEME_process_page(&$vars) {
var_dump($vars['theme_hook_suggestions']);
dsm($vars['theme_hook_suggestions']); //if you have Devel
}
You should install the Devel module, this helps alot with theming.
First make sure you're using the correct machine name for your content type. Go to content types and copy it from there.
Copy the node.tpl.php file from root/modules/nodes/ to your theme's templates directory.
Rename the node.tpl.php file to node--MACHINE_NAME.tpl.php.
Clear the cache in admin/performance/. This is important.

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