Skip Navigation

[Resolved] Code to conditionally hide labels for empty repeating field groups?

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 0 replies, has 1 voice.

Last updated by Djuna 1 week, 4 days ago.

Assisted by: Minesh.

Author
Posts
#2805958

Any chance you can have a look at this today, Minesh?

I've just made a fresh backup...

#2805962

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I can not able to access the wp-admin.

Can you please share details how I can access the wp-admin and what link I should use to login to admin?

I have set the next reply to private which means only you and I have access to it.

#2806195
Conditional Blocks - RFG + RP.jpg
Another Conditional Block Quirk.jpg

First image shows the problems I reported earlier for conditional blocks containing a Repeating Field Group or Related Posts, with the blocks I set up for demonstration today. This example shows results for hidden link

Second image shows a similar problem discovered last night. This conditional references a "normal" field. I don't have problems with conditions referencing other normal fields on this site. Only this one, where the conditional block holds shortcode that references a field that may or may not be empty.

Note the only good example for this new problem is the post linked above. For all other posts, the "other-video-link-1" field is empty.

While a conditional wrapper isn't needed for this shortcode, itself, (because the media player simply doesn't display if the shortcode doesn't find a video), the intent is to use this condition as part of an OR test for the 'Videos' heading. The heading should display IF my first YouTube Video field is not empty OR the 'other-video-link-1' is not empty. Because the second part of this condition breaks (ie., results in NO display, whether or not 'other-video-link-1' is empty), the whole condition breaks and I never get a 'Videos' heading, even if the first part of the OR test passes muster (as it does for most posts).

I don't have the OR test on the Videos heading now, but that's how I discovered the problem. In the current state I set the conditional on the shortcode block only for testing purposes and found this strange behavior. Even the debug results don't get out...

#2806207

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please check the following screenshot: hidden link

You can see how I've setup the conditional block within the view block.

I've added the conditional block and set it to display with condition "Number of items found in Views is greater than 1".

Can you please confirm it works as expected.

#2806208

Looks encouraging 🙂

I'll have a check soon.

#2806211

I see that the structure is different, and that's the main point, yes?

As for the condition it should be greater than 0 or greater than or equal to 1, correct?

Images showing the required structure would be very helpful in the documentation 🙂

#2806212

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see that the structure is different, and that's the main point, yes?
==>
Structure and the condition.

As for the condition it should be greater than 0 or greater than or equal to 1, correct?
==>
The condition should be greaten than 1. Normally, it should be greater than 0 but I need to test this and reproduce this and then I will escalate it once I confirm.

But for you, you can use this like this or another way is you can use conditional shortcode and add view's shortcode within it without block.

#2806215

Ah, okay, I never would have guessed greater than 1!

I'll test soon but am feeling great relieved already. Huge thanks!

#2806228

Yay! I've got the example for the Sound block working. Few posts have sound but the following post does, and the block appears using the structure you set up AND the condition of number of views > 0, as we'd expect.

hidden link

Now my struggle is reproducing your structure! When creating a view, the view output appears nested within it. Then, when I create a conditional block within the view it appears at the same hierarchical level as the view output. I've not yet succeeded in dragging the view output into the conditional block, to be contained within it. Is there a trick to this?

#2806230

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No - there is no other trick other than you have to drag the conditional block at your required place.

#2806414
Heading + Map Conditioned on 'Locations' on-off Switch.jpg
Conditional Blocks - RFG + RP.jpg

I found a workaround for conditionally displaying headings for Repeating Field Groups and Related Posts sections that may or may not have content, and will share an illustration of my fix here.

Basically, I added a radio button field for each RFT and RP section to serve as a manual "switch", at post-level, upon which a conditional block for only the heading of each RFT and RP section operates.

The view outputs of the RFT and RP have conditional display built in (thankfully, they take up zero screen real estate when empty), so it's really only the headings that need added conditionality.

This set up appears to keep Gutenberg a little happier and snappier, too. Rendering only a heading into a conditional block that checks a lightweight on/off field setting is understandably easier than rendering a heading plus an RFG or RP output, conditioned on a query counting views in said output. *Nor could I get consistent results for any structure with a conditional block based on RFG or RP view counts.*

While this solution requires more fields than I anticipated, and more manual control at post-level, use of radio button switches eases the pain because:

1) We can set the radio button default to hide or display sections based on the most common condition for that section (has content, or doesn't have content). In many cases, relatively few buttons require manual setting.

2) Buttons are quick and painless to click, if need be.

And sometimes manual control at post-level is nice.

Besides RFT and RP sections, I've applied this solution to a couple more cases on my current project:

1) A 'Videos' section that includes YouTube fields and "Other Video" fields (that use shortcode in my template to pull up a media player). As I illustrated yesterday in this thread, I found really strange behavior for a conditional block checking if my 'other-video-1' field was empty or not -- no display at all, including debug results. A manual switch for the video heading just works.

2) A 'Locations' section that includes a view of location 'cards', with search fields, followed by a map that references the location vew. Now both the header and the map are conditionally displayed based on my manually tweakable 'Locations' switch, allowing the whole section be hidden for a small set of posts that don't list locations.

I've put in more time than I'd like to admit asking conditional blocks to display, or not, based on view counts for Repeating Field Groups and Related Posts, and getting unworkable results. At least 4/5 of that time was lost due to nasty Gutenberg lag mixing views and conditions. Good chance there are hard-coded solutions to this challenge, but for block-editing I believe this approach is best. I'd look forward to being corrected 🙂

#2806435

Personally, my issue is resolved, but I'll leave this open for the moment in case there are better ideas than my manual on/off switch.

Sadly, the test on my Sound section yesterday showed the heading and a blank view output for posts with 0 sound entries, both with conditions set to "Items found in views > 1" and "items found in views > 0", not the desired result of hiding the section heading if no views are found.

#2806437

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That is why I shared the following solution:
- https://toolset.com/forums/topic/code-to-conditionally-hide-labels-for-empty-repeating-field-groups/page/2/#post-2806207

I've added the conditional block and set it to display with condition "Number of items found in Views is greater than 1".

So you will have to use the condition:
- "Number of items found in Views is greater than 1"

Then you will find it working for both empty and not-empty repeating field group.

#2806469

Yay! I got it working like that now. Thank you, Minesh!!

Some difficulties have been:

a) A sticky browser cache, I presume, that led me to see a heading over no views for the "Views > 1" test section for some posts.

b) Being unable to reproduce this structure:

View
>Conditional
>>Heading
>>View Output

No matter long I held a block over a _newly created Conditional Block inside a View_, nothing would go in. Same problem in the list editor and the block editor. And if trying to jam a View Output into a Conditional Block in the list editor, it would tend to slip off, delete itself, then recreate itself with an empty View Loop. Frustrating!

Hence I developed my workaround.

Now I just discovered the required trick, on my system, for getting anything inside a Conditional Block INSIDE a view:

1) Create the Conditional Block OUTSIDE the View.
2) Put a block inside the Conditional Block (e.g., the section Heading).
3) Slide the Conditional Block into the View.

If the Conditional Block is "primed" with contents before going into a View, I can drag the View Output (or anything else) into the CB. Otherwise, for an empty CB inside a View, nothing will go in!

c) My initial difficulty was that I expected I could use this structure:

Conditional
>Heading
>View
>>View Output

I think I expected this as it follows the structure for "normal" fields (non-repeating and not a list of related-posts)

Conditional
>Heading
>Dynamic Block pointing at a "normal" Field

It would be helpful to add an image showing the required structure to documentation.

d) I never would have found my way to the "Views > 1" condition. Because that's so non-intuitive, and because I presume this is a frequently needed conditional test, it would be nice to offer a plain English option in the CB dialog that skirts the weird math. Maybe:

Parent View Block Returns No View Outputs

Upon selection, the right side of the dialog is grayed out. Or, better yet, overlaid with an image showing the required hierarchy... View > CB > View Output.

Anyway, thank you for your patience. Hopefully my troubles and your solution help others down the line.

#2806546

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Really glad to know that workaround I shared help you to resolve your issue.

You are welcome to mark resolve this ticket.