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.

3
  • 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 Commented Aug 26, 2015 at 20:26
  • Hi, Erik. If you are using the Field Group module (or other) please indicate this in your post. Commented Aug 27, 2015 at 6:08
  • I am using Field Group, edited the post to reflect that. Commented Aug 28, 2015 at 1:24

1 Answer 1

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';
9
  • +1 for Devel module
    – leymannx
    Commented Aug 28, 2015 at 10:08
  • Can I do the same to print something based off of the value of one specific field in the group? Commented Aug 31, 2015 at 23:46
  • Please, provide example Commented Sep 1, 2015 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' Commented Sep 4, 2015 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. Commented Sep 4, 2015 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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