how to override front page node tpl - drupal - drupal

How to override drupal front page node.tpl file ? I tried various
node--front.tpl.php
page--node--front.tpl.php
page--front--node.tpl.php
but its not working.
What will be the file name to override home page node ? (I am working in drupal 7)

You can add this function to theme template.php
function customethemename_preprocess_node ( &$vars ) {
if ($vars["is_front"]) {
$vars["theme_hook_suggestions"][] = "node__front";
}
}
Then you can page page--front.tpl.php
It will solve the problem

It should be page--front.tpl.php
Also, be sure that you have the precursors in the hierarchy for your theme (e.g. page.tpl.php)

I would recommend solving this by setting a specific node of content to be the front page.
http://www.inmotionhosting.com/support/edu/drupal-7/homepage/change-front-page
Then I would use a specific node ID template.
node--[insert id here].tpl.php i.e. node--1.tpl.php
You need to do two things before this will work:
Make sure you have a copy of the original node.tpl.php file in your theme folder (the overridden template file will not be picked up otherwise).
Clear Drupal's cache

No need to develop manually a front page, just creaate a frontpage by views or another and set it the as the front page in :
www.yoursite.com/?q=admin/config/site-information

Following steps resolved the issue for me to create custom front page in Drupal 7.
Create a file: page--front.tpl.php. Notice two hyphens instead of
one.
Clear the cache: Configuration >> Development >> Performance>>
Clear all caches

I think the best solution is using frontpage nid
function YOURTHEME_preprocess_node ( &$vars ) {
list(, $frontpage_nid) = explode('/', drupal_get_normal_path(variable_get('site_frontpage', 'node')));
if ($vars['node']->nid == $frontpage_nid) {
$vars['theme_hook_suggestions'][] = "node__frontpage";
}
}
Because
if ($vars["is_front"]) {
$vars["theme_hook_suggestions"][] = "node__front";
}
add the theme suggestion for all nodes in frontpage, not only for frontpage node

Related

Drupal basic page doesn’t seem to use page.tpl.php

Title says it really. Basic pages created in Drupal don’t seem to use the page.tpl.php file as a template.
If I edit the html.tpl.php file, those changes apply to every page, and it causes errors when I load a basic page.
I have also tried to copy the page.tpl.php file and name it page—basic-page.tpl.php to no avail.
Any idea what’s going on?
Also, how do I go about changing the page_top variable to include more content?
Lastly, the default page.tpl.php file has $page variables and things like $page_top and the like.
How would I call the title from the page only and the body text of a page only?
I’m using Drupal 7 and a custom sub theme.
The aforementioned files are in the template folder of the theme and I did clear caches when I added them.
Add $conf['theme_debug'] = TRUE; in settings.php and clear cache, reload page and check view source.
It will display the template file suggestions.
page.tpl.php file common for all pages. Just print anything to the tpl and run any node of basic page as well as other content type page and check if its working or not. If page.tpl.php not working for basic page only, then check your template.php file.
For print a page title just need to use following code:
<?php print $title; ?>
For print body text you need to use following:
<?php print render($page['content']); ?>
This may depend on the theme you are using. But I guess you are actually meaning page--page.tpl.php (double dashes). Which will be taken into account after you added the following snippet to your theme's template.php. Replace MYTHEME with your theme's machine name.
function MYTHEME_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; // (double underscores)
}
}
See Template (theme hook) suggestions, where I also got above snippet from.
active theme debug for inspecting the template source and you get a different suggestions to user it (just avoid using node/nid).
commend drush to enable theme debug drush vset theme_debug 1

Drupal 6 - Custom page.tpl

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.

Drupal - Replace the home page

Hi
Anyway to stop people browsing the Drupal home page and redirect to my specific html page?
Thanks you
For Drupal 7 you need to use page--front.tpl.php
If your theme doesn't have a page.tpl.php that you can copy, then copy it from your base theme if you are using one or:
modules/system/page.tpl.php
This should be placed in your custom theme folder in (assuming this is not a multisite):
sites/all/themes/my_theme
I tend to structure my themes as follows:
my_theme.info
templates/html.tpl.php
templates/page/page--front.tpl.php
templates/node/node.tpl.php
templates/block/block.tpl.php
css/style.css
But it doesn't really matter where it is, it will be picked up after a cache clear.
If you have specific HTML page that it is on the server and doesn't generated from Drupal, the most easy way is to use Drupal goto.
How to do it? open your template.php and search for page_preprocess function it should look like that:
YOURTHEME_preprocess(&$variables, $hook) {
if( drupal_is_front_page()) {
drupal_goto('yoursite.com/yourpage.html')
}
}
change YOURTHEME to your theme name and clear the cache.
The real Question why should you use static HTML file for your homepage? I would create some article or view for homepage and change it as I like with theming... It is much easier than any other alternative.

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?

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.

Resources