CMS Development

RDouglas
Member

Empty group field count/length confusion?

SOLVE

I have created a module with the following nested field :

{
... other fields...
},
{
  "type": "group",
  "name": "sub_links",
  "label": "Second Level Links",
  "expanded": true,
  "occurrence" : {
    "min" : null,
    "max" : null,
    "sorting_label_field" : null,
    "default" : 0
  },
  "children": [
    ...
  ]
}

 

In my hubl template I'd like to see if there have been any sub_links added and if so, iterate on them. My issue however is that the hubl tag code (object is the instance that would have the sub_links field):

{{object.sub_links|length}}

This always seems to display a length of "4" anytime I would expect it to be "0" (empty). If I add any items in the CMS, the above code would then show the correct count. For some reason an empty array is displaying a non-zero length. it's also interesting that "4" corresponds with the number of available fields on a single group item (fields on the sub_link item). Is there something wrong with the way I'm using length or setting up the group field? I just want to determine if the group field has contents or not.

0 Upvotes
1 Accepted solution
piersg
Solution
Key Advisor

Empty group field count/length confusion?

SOLVE

You can use the iterable expression test to check if something can be iterated over, see if that works instead of the length filter e.g. 

{% if object.sub_links is iterable %}
  {% for link in object.sub_links %}
  ..etc
{% endif %}

 

View solution in original post

4 Replies 4
RDouglas
Member

Empty group field count/length confusion?

SOLVE

Looks like "is iterable" actually did the trick, I just also had an incorrect assumption in my template code. Still confused why the length is inaccurate though...

 

You're correct, the 'sub_links' group is nested. The 'object' key is just an artifact of trying to consolidate my code for this post 😅 The actual code would be something like this:

 

{% for link in module.links %}
  {% set has_children = link.sub_links is iterable %}
  {% if has_children %}
    {% set children_class = 'item-has-children' %}
  {% endif %}
  ... template html code ...
{% endfor %}

 

You can see my mistake was that I was assuming 'has_children' would be destroyed for every iteration of the loop, but it looks like once it gets set it just stays there until explicitly unset. Just so happens that the first 'link' had set 'has_children' to true.

 

Anyway, thanks for the help!

0 Upvotes
piersg
Key Advisor

Empty group field count/length confusion?

SOLVE

The issue might be that you're missing an id. It can be the same as the name so 

"id" : "sub_links",
"name": "sub_links",
..

 

I'm also not sure why you're using object.sub_links instead of module.sub_links. Is the sub_links field group a nested inside another field group with the id of 'object'?

 

 

0 Upvotes
piersg
Solution
Key Advisor

Empty group field count/length confusion?

SOLVE

You can use the iterable expression test to check if something can be iterated over, see if that works instead of the length filter e.g. 

{% if object.sub_links is iterable %}
  {% for link in object.sub_links %}
  ..etc
{% endif %}

 

RDouglas
Member

Empty group field count/length confusion?

SOLVE

That doesn't work since it seems the problem is that the sub_links object is being created with an single empty instance (making sub_links iterable) even though the CMS content editor hasn't added anything. Is there an issue with how I've set up the group occurence field that would be causing this?

0 Upvotes