Drupal 7: Hiding labels with empty fields when viewing a node - drupal

How do I hide labels that have empty fields when viewing the actual node of a certain content type?
I'd really appreciate anyone's help, thanks for your time.

Another way you could achieve this is by using a custom template file that would apply to all nodes of that content type.
Make sure that node.tpl.php exists in your sites/all/themes/[mytheme] directory first. This template must exist before other custom templates can be called.
Make a copy of your node.tpl.php and name it node--[contenttype].tpl.php (without the brackets).
If you have the Devel module enabled, you can throw a dpm($content); into the file to find out the name of the field you are trying to hide. Or you could look at the content type itself.
Once you have the name of the field, you can now insert this code before the print render($content); statement:
if (empty($content['my_field'])) {
unset($content['my_field']);
}
Clear the cache, and your field will only appear if there is a value stored.

By default, the labels of empty fields are hidden, maybe there's still a 'non breaking space' or some other leftover in the field?
You have to check the difference between an existing node where the problem occurs and a new node where you don't touch the particular field.
Set unwanted labels display hidden in nl/admin/structure/types/manage/selected_content_type/display

I would like to correct first answer. In node.tpl.php we should check #markup instead of field array:
if (empty($content['field_vac_req'][0]['#markup'])) {
unset($content['field_vac_req']);
}
instead of
if (empty($content['my_field'])) ...

If the content type has a lot of fields looping this worked for me:
foreach($content AS $key => $values) {
if (!empty($content[$key][0]['#markup'])) {
print render($content[$key]);
}
}

Related

How can you hack an existing drupal module and add an entity reference?

I am using imagefield marker and would like the description boxes to load nodes.
Please do not use preprocess. That is for the theming layer.
Use the Field API.
If it's for a particular field on a node edit form, use a form alter to change the array of the form element.
If you wanna modify field behaviour you can use the preprocess field hook in your template.php
template_preprocess_field(&$vars, $hook){
if($vars['element']['#field_name'] == 'myfield')
$variables['loaded_node'] = drupal_render(node_view(node_load($nid)));
}
And then create the theme file field--field-name.tpl.php and print the variable from there.
Hope that helps your question is kinda vague on what you want to do.

Creating a link field in a custom content type

I have several custom content types and in one specific one I need to offer two fields, one for the href for a link, and one for the text of a link this way I can build and style it with minimal user involvement of the HTML/CSS. I also have a custom node.tpl for this content type. My problem is that drupal throws divs around each field I create that aren't in my template file for this content type (node-custom.tpl) and I can't put the href for a link with divs around it inside google.co.uk</div>"> See my problem. Maybe I'm doing this all wrong so any other ideas are welcome.
Please note I'm trying to create this site with minimum involvement of HTML/CSS access for the user. I'm aware I could hand code the link in the field.
The easiest way to do this would be to use a preprocess function in your template.php file and build the link up manually:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type = 'my_type') {
$uri = $node->field_name_of_link_field[LANGUAGE_NONE][0]['value'];
$text = $node->field_name_of_display_text_field[LANGUAGE_NONE][0]['value'];
$vars['my_link'] = l($text, $uri); // Using Drupal's l() function to render a link
}
}
Then in your node template file you'll have access to the variable $my_link which you can output anywhere, and will contain the correct HTML for the link. Finally, go to the "Manage Display" page for your content type and set the display of the two fields you no longer need to output to 'Hidden'.
There are other ways so if that's no good let me know
EDIT
Just to add, I think the easiest way to do this would actually be to install the Link module and use the provided field type instead of the two other fields you're currently using.

Get the view name in the page.tpl.php file

How can I get the current view name inside the page.tpl.php file?
Details:
If I want to apply special code inside page.tpl.php for a certain node I can use the node number
if($node->nid == 35){
//do something special for this node id
}
But The pages generated by the views module don't have a nid, They have a view name, How can I get this node name using php, I need to get something like this.
if(//node-name == "view1")
//do something special for this page generated by views module
I use a work around for now, I use current_path() but I need a more reliable solution because the path may change!
Thanks
When you edit the view, there is a part called 'Theme Information', there shows a list of possible template files for theming the view or its components.
I found the answer to my question (Drupal 7)..
In order to get the current view name in the page.tpl use:
$page["#views_contextual_links_info"]["views_ui"]["view_name"]
In order to get the name of the page inside the current view use:
$page["#views_contextual_links_info"]["views_ui"]["view_display_id"]

Conditional link to node within views?

I have two content types, book and chapter. Each chapter node contains a node reference of the book to which it belongs. I have created a view which displays the title of each chapter for a given book. Within the view, the title field has been configured to link to its node.
All works well. I am now interested in updating the view to not display a link to a chapter's node when the chapter's body is empty. Thus this view would display a list of chapter titles for a book and link only to those chapters that have a body.
Can this be accomplished using out of the box Views functionality? If not, how would something like this be accomplished?
Thanks.
I'd use the Views Custom Field module to implement your custom logic. It allows you to grab the data fetched by Views and manipulate it at will with PHP. Very handy.
I'm answering my own question because my response to ceejayoz is poorly formatted.
What I did to accomplish this was to first download and enable views_customfield. Second, I placed Title and Body fields within the view, both excluded from display. Third, within a Customfield: PHP code field I placed the following code:
<?php
if (strlen(trim($data->node_revisions_body)) == 0) {
return $data->node_title;
} else {
return l($data->node_title, drupal_get_path_alias('node/' . $data->nid));
}
?>
There's also this clever workaround which allows you to achieve this very easily:
Add 2 title fields one with link and one with no link, and make both
them exclude from display.
Add body field,
In No result behavior put title with no link replacement token to
it.
In Rewrite results behavior put title with link replacement token to
it.
Tick hide if empty.
Source

How do you remove the default title and body fields in a CCK generated Drupal content-type?

When you create a new content type in Drupal using the Content Creation Kit, you automatically get Title and Body fields in the generated form. Is there a way to remove them?
If you're not a developer (or you want to shortcut the development process), another possible solution is to utilize the auto_nodetitle module. Auto nodetitle will let you create rules for generating the title of the node. These can be programmatic rules, tokens that are replaced, or simply static text. Worth a look if nothing else.
To remove the body edit the type, expand "Submission form settings" and put in blank for body field label. For title you can rename it to another text field. If you really have no need for any text fields you can create a custom module, say called foo, and create function foo_form_alter() which replaces $form['title'] with a #value when $form['type']['#value'] is your node type.
No need to install anything:
when editing the content type, press "Edit"
(on the menu of Edit | Manage fields | Display fields )
click on the Submission form settings
on the Body field label:
Leave it blank, it would remove the Body field.
If you're not a developer (or you want
to shortcut the development process),
another possible solution is to
utilize the auto_nodetitle module.
Auto nodetitle will let you create
rules for generating the title of the
node. These can be programmatic rules,
tokens that are replaced, or simply
static text. Worth a look if nothing
else.
And to add on to William OConnor's solution...
The module is poorly documented unfortunately. It's really only effective if you use PHP with it in my opinion. Check off the "Evaluate PHP in Pattern" and type into the "Pattern for the title" field something like:
<?php echo $node->field_staff_email[0]['email']; ?>
or:
<?php echo $node->field_staff_name[0]['value'] . '-' . gmdate('YmdHis'); ?>
...where I had a field with an internal name of "field_staff_email" and was using the CCK Email module -- thus the 'email' type was used. Or, I had a field with an internal name of "field_staff_name" and was just an ordinary text field -- thus the 'value' type was used. The gmdate() call on the end is to ensure uniqueness because you may have two or more staff members named the same thing.
The way I discovered all this was by first experimenting with:
<?php print_r($node); ?>
...which of course gave crazy results, but at least I was able to parse the output and figure out how to use the $node object properly here.
Just note if you use either of these PHP routines, then you end up with the Content list in Drupal Admin showing entries exactly as you coded the PHP. This is why I didn't just use gmdate() alone because then it might be hard to find my record for editing.
Note also you might be able to use Base-36 conversion on gmdate() in order to reduce the size of the output because gmdate('YmdHis') is fairly long.
The initial answers are all good. Just as another idea for the title part... how about creating a custom template file for the cck node type. You would copy node.tpl.php to node-TYPE.tpl.php, and then edit the new file and remove where the title is rendered. (Dont forget to clear your cache).
Doing it this way means that every node still has a title, so for content management you aren't left with blank titles or anything like that.
HTH!

Resources