开发者

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

How do I hide labels that have empty fields when vi开发者_StackOverflowewing 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]);
          }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜