How do I display either body or file from content - drupal

I am a new in Drupal. I want your help for doing this please help
I have created one Content type with fields( Title, Body, File) and also added content through this content type. But Some of the content didn't have file filed and some of the content having the only file. In the view, I want to show both either body or file. if suppose file field is empty then it should display the body of the same content.
Thanks.

You could do this by creating a custom node template for particular content type. So if you have a content type 'news', for example, the you can create a node template for this content type and customized the front-end display of the content. See below how it would work.
As per the Drupal theme suggestions, create a node template, named, node--news.tpl.php (copying the existing node.tpl.php file) in your custom (active) theme directory under the templates folder. On this template, you will be able to access the $node object variable, containing complete node information including fields and data.
Suppose your fields are field_image and body then replace your template's code print render($content); with the following lines:
if(!empty($content['body']){
print render($content['body']);
} else {
print render($content['field_image']);
}
For more details: https://www.drupal.org/node/1323842
Hope this help!

Related

Drupal: page specific theming without node-id

I am adding a page with a node-id and earlier I was using "page--node--node-id.tpl.php" template to customize the node/page. However, I accidentally deleted that node, and now I am unable to create that node with particular nid node. I want to know how to customize the specific page as each page his unique title.
Use page--node.tpl.php for all the nodes instead of each template otherwise use
node--[node-type].tpl.php for specifc node content type template
There is another smart step to create content type template with your own specific name by following below code
function themeName_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
$vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);
}
}
In above case If node content type is "article" then the template suggestion will be "page--article.tpl.php".
Create template file page--nodetype.tpl.php in your theme directory.

Custom template file for displaying the inner blog contents

I am trying to create a custom layout for my blog listing and blog content display pages.
The following url will display all the latest blogs based on the conditions that I have set.
http://fqdn/general-blog
When I click on the 'Read more' button corresponding to a button, it will display the full blog contents in another page.
Sample format : http://fqdn/general-blog/tourist-details
I am able to modify the layout of the page corresponding to blog listing. For this, I have created a custom tpl file : page--general-blog.tpl.php.
What I want is to create a new template file for displaying the contents of individual blogs.
The individual blog content display page is not entering - page--general-blog.tpl.php file. It's always using the template - page.tpl.php.
I tried creating separate tpl files like :
page--general-blog--%.tpl.php
But, the changes in this template is not getting effect in the individual blog content display page.
How should I move forward. Has someone had any similar situations?
If your content type has name general-blog,you should name single blog entry template as node--general-blog.tpl.php. See
Theming nodes by content type for details.
If you need to theme whole page template depending on node type, you should add following code in your template.php:
function YOURTHEME_preprocess_page(&$variables) {
if (!empty($variables['node']) && !empty($variables['node']->type)) {
$variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
}
}
And then use page--node--general-blog.tpl.php file (see Page templates depending on node type for details).
Don't forget to clear the theme registry after creating new template file.

How to show the image of custom content type in drupal page.tpl.php?

I just created a custom content type in drupal7 and I want to manage the image of that content in my template. <?php print $content; ?> displays all the field of that content. But I just want to show the image only.How can I do so?
You must somehow get node id of node that contains that picture (hard-code it or something). Then use node_load() function to load whole node - image field will be one of available fields in loaded node object. Use print_r or something to check out available fields, so you can construct exact code, but it should be something like this:
$node = node_load($nid);
$picture_uri = $node->field_myimagefield['und'][0]['uri'];
// To get image path in specific image style you created:
$picture_style_path = image_style_url('style_name', picture_uri);
// To get original image path
$picture_original_path = file_create_url($picture_uri);
Then you can directly print picture path into img html tag...
But, are you sure that you are using right template file? If you want template just for your content type you should use node, not page template (which is wrapper for node template).

How can i edit the default drupal node view page

I am trying to edit the default page view of a node in Drupal and i found the file called node.tpl.php but even if i change it, the node page on the site isn't updated...
So how can i update the node view page so i can added some text, images etc.. ?
I guess that node_show function () { } is the one that displays the node but the code there doesn't give me enough detail on where the HTML is created so i can it
You need to copy the file to your theme's templates folder and clear the cache out before you can use this file. The original file should be at root/modules/node/
You can also set a template up for a specific content type by copying the default node.tpl.php file and renaming it to node--CONTENT_TYPE_MACHINE_NAME.tpl.php

new content type, with completely blank(body of node only) output theme files

I have a content type that is only used for scripts on my drupal site that should return json data. So the issue then becomes that I can't have any of the theming elements of the site in the output.
So I know I need blank (only the output variable) tpl files for the following:
html.tpl.php
page.tpl.php
region.tpl.php
block.tpl.php
field.tpl.php (the manual says this isn't used but its the only way I could find to remove field divs around the body of my page)
So my question is, how can I create all of the content specific files for this content type? I know its easy to do a theme override in template.php BUT I can only get it to work for html.tpl,page.tpl and thats it. Drupal seems to ignore any content specific functions from region down to field.
I noticed you tagged this drupal-7, which I haven't tried yet. But, in Drupal 6 I would accomplish this by adding a new page template to $vars['template_files'][] in the page preprocess function of template.php. Example:
$node_type = $vars['node']->type;
if ($node_type == '{your content type name here}') {
$vars['template_files'][] = "page-" . $node_type;
}
If your content type was called 'scripts', then Drupal will look for a page called page-scripts.tpl.php, which will contain only one line:
<?php print $content; ?>

Resources