Conditional - Hide element based on API

This is somewhat linked to this post: DC Looper API - How to access value from key that has dot syntax - please see the login detail in the Secure Note in that post.

How would I use the Conditional function to hide an element if there is no record in the API fetched data.

With ACF - I can create a condition where it can check a key and see if its empty or not.
Whereas with an API JSON file, I don’t know how to make the condition work as the API has no data/key whatsoever.

For example: (the looper provider uses the “data” as the Data Key in the object

“data”: [
{
“Courses”: {
“Course_Name”: “course 1”,
“ID”: “118644000003209497”,
},
“ID”: “118644000004210488”,
“Course_Serial_Number_Autofill”: “100”,
“Start_Date”: “03 July 2024 09:30”,
“End_Date”: “03 July 2024 14:30”,
“Event_Cancelled”: “false”
},
{
“Courses”: {
“Course_Name”: “course 2”,
“ID”: “118644000003209497”,
},
“ID”: “118644000004210492”,
“Course_Serial_Number_Autofill”: “200”,
“Start_Date”: “03 July 2024 09:30”,
“End_Date”: “03 July 2024 11:30”,
“Event_Cancelled”: “false”
},
{
“Courses”: {
“Course_Name”: “course 3”,
“ID”: “118644000001252015”,
},
“ID”: “118644000004210474”,
“Course_Serial_Number_Autofill”: “300”,
“Start_Date”: “05 July 2024 10:00”,
“End_Date”: “05 July 2024 12:00”,
“Event_Cancelled”: “false”
}

Works
Text element 1(“course is available”): Condition = 100 is {{dc:looper:field key=“Course_Serial_Number_Autofill”}} = matches with ‘course 1’ so it shows the text element saying: “course is available”. if no matching course, this element is hidden.

DOESNT WORK
Text element 2(“course not available”): Condition = 400 is not {{dc:looper:field key=“Course_Serial_Number_Autofill”}} = doesnt match with any course, so shouldnt it show the text element? I also tried “not in” but that doesnt work too.

I think it’s because there are no record whatsoever to compare the conditions to. So if there are no record, how is it going to compare?
But if that is true - how can I show a message that says ‘course not available’ if there are no record from the pulled API data?

The logic is: if there are matching courses, text element 1 should show. if there is no matching courses, text element 2 should show.

Maybe there is a function on Cornerstone that I don’t know. Please point me to how I can solve this.

Thank you!

Hi @bobbyninetoes,

Thanks for reaching out.
You are matching with the looper values only, so if the value or values are not present you can’t match it within the condition.

Thanks

Hi,

Yes, this is what I thought - but is there a way to hide it if no data is present whatsoever?

Hello @bobbyninetoes,

You will insert two Text elements.

TEXT 1 displays “Course is Available” with the condition:

100 is {{dc:looper:field key="Course_Serial_Number_Autofill"}} 

OR 

200 is {{dc:looper:field key="Course_Serial_Number_Autofill"}}

OR

300 is {{dc:looper:field key="Course_Serial_Number_Autofill"}}

while the TEXT 2 element displays “Course not Available” will have the condition:

100 is not {{dc:looper:field key="Course_Serial_Number_Autofill"}} AND

200 is not {{dc:looper:field key="Course_Serial_Number_Autofill"}} AND

300 is not {{dc:looper:field key="Course_Serial_Number_Autofill"}}

If this does not work, please provide us with your WP details so we can check.

Kindly let us know how it goes.

Hello,

The first example works - because there is a data in the “Course_Serial_Number_Autofill” key for the Condition to compare with.

The second example doesnt work - because the “Course_Serial_Number_Autofill” doesn’t exist.

Hey @bobbyninetoes,

If the condition for text 1 works for you, you can adjust the code given by my colleague. Here’s the updated one:

TEXT 1 displays “Course is Available” with the condition:

100 is {{dc:looper:field key="Course_Serial_Number_Autofill"}} 

OR 

200 is {{dc:looper:field key="Course_Serial_Number_Autofill"}}

OR

300 is {{dc:looper:field key="Course_Serial_Number_Autofill"}}

while the TEXT 2 element displays “Course not Available” will have the condition:

100 is not {{dc:looper:field key="Course_Serial_Number_Autofill"}} OR

200 is not {{dc:looper:field key="Course_Serial_Number_Autofill"}} OR

300 is not {{dc:looper:field key="Course_Serial_Number_Autofill"}}

Hope that helps.

Hi,

Thank you for your suggestion - I see the changes that you made is only to change ‘AND’ to ‘OR’…which unfortunately doesn’t work as it’s pretty much the same.

I think I may be explaining it in a confusing way, but the issue is not the logic. YOUR logic is correct and I am thinking the same thing. But the issue is that the logic, the condition, has no data to compare to, so it won’t work at all.

This is my real Condition: {{dc:acf:field key=“Course_Serial_Number”}} is not {{dc:looper:field key=“Course_Serial_Number_Autofill”}}

If ‘Course’ 400 doesn’t exist at all, the “Course_Serial_Number” has nothing compare to (Course_Serial_Number_Autofill). it work work at all.

example:
{{dc:acf:field key=“Course_Serial_Number”}}(Course code on website using ACF) is not {{dc:looper:field key=“Course_Serial_Number_Autofill”}}(no data from API because it doesn’t exist) = condition logic fails.

The Condition output is basically like this: "400 is not "
it’s completely blank, nothing to compare to.

Can you see the issue? does it make sense now?

Hello @bobbyninetoes,

You may use this one:

{{dc:looper:field key="Course_Serial_Number_Autofill"}} is [leave this blank]

And then you add another condition if needed.

Hope this helps.

this will not work. there is no ‘Course_Serial_Number_Autofill’ to compare to to check if it’s empty or not. please understand my previous message.

What page on your site are you using the conditional on?

course page - the template for it. and you will see the part for ‘Open Course available dates:’

In your case you needed to start looping before you could utilize the condition. I arrange it like so to make use of your conditional. The same is needed for “Open Course Dates” element as you aren’t looping and need to to be able to use the looper data. Conditions happen before Looper consuming so to utilize looper data you need to first consume then you can use that data in a condition. Let us know if this helps and have a great day.

image

hmmm… i dont completely understand what you are saying.

Here is the photo:

I want the whole section Open Course Dates to disappear if there are no available dates. Only show this section if there available dates from the API, and then loop through the dates.

I don’t know what you did, but this is section still shows on other courses that has no dates.
image

The more I think about I’m not sure we could do that currently without a custom integration, a new feature, or adding a different type of API request. Since you need to loop through your API data to be able to determine if your ACF meta value is there, we don’t really have a way to both check a condition and only ever display an element once when in a looper. If we were checking that condition in looper data “Open Course available date:” it would display multiple times. I can take the feature request in, a “Display Once” condition. Through One we could also get more involved in this site if you’d like.

From what I gathered you are creating this API right? Is there anyway in that platform you are using to be able to create a request based on that ACF value? As in you could pass in ?acf-value=THIS_POST and only get results for the Course you want to display? That could remove the need for all these conditions and probably make this simpler overall.

Let me know if you have any other questions.

Have a great weekend.

Hi,

Thank you for taking the time to check the site - the “Display Once” would be nice, but we will also check what you said on the second paragraph.

We are using Zoho Creator to connect to our database there. This is the Custom API documentation: https://help.zoho.com/portal/en/kb/creator/developer-guide/microservices/custom-api/articles/understand-custom-apis

Could you check there if it mentions anything about what you said on paragraph 2 of your message above?

Thank you!

I see plenty of references to it, but not sure how it would factor in to how you filter data on your end. Look for “Request Parameters” in that doc. Also look at “Request Methods”. You would pass in that data from the “Request” section in Cornerstone.

image

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.