2

I have a content type with 4 different field-groups utilizing the Field Group module. I want to test to see if those groups are empty and render them and some additional HTML if they are NOT empty, and not render them, and additional HTML, if all the fields in the group are empty.

I'm not sure of the syntax to test if field groups contents are NULL, any pointers there?

So far this still isn't working for me. I have a field group that I know is empty group_factory_images

When trying to use the code from the answer:

<?php if(!isset($node->group_factory_images))
print 'Yep, Empty';
?>

It just prints the "Yep, Empty" string.

  • At least you can install Devel module and in node.tpl use dpm($content); to see the structure of content. You can always change $content with what ever is accessible for that template. I should take a look to say more concrete things – ar7max Aug 26 '15 at 20:26
  • Hi, Erik. If you are using the Field Group module (or other) please indicate this in your post. – othermachines Aug 27 '15 at 6:08
  • I am using Field Group, edited the post to reflect that. – Erik Hanson Aug 28 '15 at 1:24
4

For example:

  1. I created content type
  2. In display settings for Full content display created group group_mygroup

If group elements are empty then group wil not be displaying. In this case you should check it:

//if node's group_mygroup does not exist
//in our case it means that all elements in group are empty
if(!isset($node->group_mygroup))
        print 'Group elements are empty';
print render($content);

As you can read in node template help part - block of comments:

 * Other variables:
 * - $node: Full node object. Contains data that may not be safe.

Now, what you can do to understand solution more clearly:

  1. Install Devel module

Helper functions for Drupal developers and inquisitive admins. This module can print a summary of all database queries for each page request at the bottom of each page. The summary includes how many times each query was executed on a page (shouldn't run same query multiple times), and how long each query took (short is good - use cache for complex queries).

  1. Somwhere in your template's code use dpm($node);

  2. Create content of your type and see how it is displaying. You should see same picture 1

  3. Click on header of appeared block and you can see structure of node object 2

If you want to check if some field doesnt exist - use same construction:

if(!isset($node->field_name))
print 'Field is empty';
  • +1 for Devel module – leymannx Aug 28 '15 at 10:08
  • Can I do the same to print something based off of the value of one specific field in the group? – Erik Hanson Aug 31 '15 at 23:46
  • Please, provide example – ar7max Sep 1 '15 at 4:03
  • So far this isn't working for me. I have a group of images "group_factory_images" that I know are empty, yet this isn't doing anything in the node: if(!isset($node->group_factory_images)) print 'Yep, Empty' – Erik Hanson Sep 4 '15 at 0:49
  • I installed Devel, interesting thing is that it only shows fields, not groups. With that being true is seems to me we can't actually do any testing on the group to see if it's has contents. – Erik Hanson Sep 4 '15 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.