Skip Navigation

[Gelöst] Using view with custom attribute as an option for generic select field

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
The user was using a view to generate values for a generic field. The view was crashing the field.

Solution:
It turns out that the user was nested shortcodes and used inside the generic field shortcode. This is 3 level of nested shortcodes, which is not possible. We can only nest 2 levels of shortcodes. To workaround it, you can replace the nested shortcodes by a custom shortcode that hides one level of nesting. Check the example here https://toolset.com/forums/topic/using-view-with-custom-attribute-as-an-option-for-generic-select-field/page/2/#post-2181729

This support ticket is created vor 3 Jahren, 2 Monaten. There's a good chance that you are reading advice that it now obsolete.

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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

Author
Artikel
#2174459

Tell us what you are trying to do?
- I have a view filtered by post id through an attribute. I want to have this attribute equal to another view that generates a single post id. When I display this view on the page I get the result with no problem. But when I insert this view into the generic select field as options it doesn't work, and neither options nor field is displayed.
Posts from both views connected with the relationship.

Example of the generic select field shortcode with a view:
[cred_generic_field type='select' field='generic-field-select']
{
"required":0,
"default":"",
"options":[ [wpv-view name="view-that-gives-results-in-json-format" postid="[wpv-view name="view-that-give-related-post-id"]"] ]
}
[/cred_generic_field]

So the view itself works as expected on the page or post. But it doesn't work an an option as well as it doesn't work when it just embedded into the post cred form: [wpv-view name="view-that-gives-results-in-json-format" postid="[wpv-view name="view-that-give-related-post-id"]"]

Generic select field displays options in these format:
[cred_generic_field type='select' field='generic-field-select']
{
"required":0,
"default":"",
"options":[ [wpv-view name="view-that-gives-results-in-json-format" postid="12345"] ]
}
[/cred_generic_field]

Is there any documentation that you are following?
- https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/
- https://toolset.com/forums/topic/create-edit-form-which-combines-user-fields-and-several-custom-post-fields/

Is there a similar example that we can see?
- I don't have any

What is the link to your site?
versteckter Link

#2175411

So the view itself works as expected on the page or post. But it doesn't work an an option as well as it doesn't work when it just embedded into the post cred form
Okay, it sounds like there are two nested Views. There could be a problem in one View, the other View, or both Views. What happens if you place the inner View "view-that-give-related-post-id" directly in the form, what is the result of only this View?

Test inner View.[wpv-view name="view-that-give-related-post-id"]End test.

Please copy + paste the page source for me to review, or provide a link where I can see the output on the front-end of your site.

#2175479

Hi Christian.
I embedded a nested view on the form again as I did it yesterday and now it works: I can see the view output in the form as well as its output in the select field options. I am sure I checked that the View cache, as well as page cache, was cleared. I don't understand what could happen since yesterday when I submitted the ticket as no changes were done to the view...
Is there any possibility that a cached view (Toolset cache) wouldn't be cleared for some time even after I clicked "Clear cache" button?

#2175481

ok, it is interesting... As soon as I removed embedded views to show you results (not embedded into select field options) select fields stopped working again, they are not displayed

#2175483

... I played a little with embedding views (not as select field options). When the inner view is embedded on the form, select fields with a nested view as options works well. As soon as I remove the inner view, nothing works.
Here is a link to the page: versteckter Link

#2175485
#2175487

Let me continue investigating this, it seems like my team leader had a workaround for the generic field options issues and it involved placing the View shortcode in the page somewhere else. I'll check my notes and let you know as soon as I can.

#2175489

Yep, looks similar.
But I thought this issue was resolved...
I also tried to embed this form on the page without other Toolset Blocks elements. And the issue remain the same

#2175501

Here is an example of the page with a cred form clone that doesn't have embedded views. And this page doesn't have any other Toolset Blocks. As you can see nested view doesn't work as options for select field. However, simple view as options would work. the issue is only with nested views.
versteckter Link

#2177863

Okay I'll need to get a login again, the credentials were deleted from your previous ticket when it was resolved, for your security. Private reply fields are available here.

#2177971

Okay I see, part of the problem here is the number of levels of nested Views in other Views' attributes. Beyond one or maaayybe two levels of nesting in attributes like this, the result is an ambiguous structure of nested quotation marks that the shortcode system cannot support. You would see the same problem with Types shortcodes nested beyond one or two levels of attributes. I would not expect this to work in any case because of the level of nesting involved:

[cred_generic_field type='select' field='generic-principal-investigator-select']
          {
          "required":1,
          "default":"",
          "options":[ [wpv-view name="principal-investigators-pi-as-options-for-post-a-trial" instpostid="[wpv-view name='institution-id-of-the-current-member-for-my-account' memberid='[wpv-view name="member-id-of-the-current-member-for-my-account"]']"] ]
          }
        [/cred_generic_field]

Another ticket where nested shortcodes have caused a problem in Forms:
https://toolset.com/forums/topic/pre-populating-fields-on-cred-form-not-showing-correctly/

What you really need here is one custom shortcode that produces the desired output and can be used in place of the nested View structure like this:

[cred_generic_field type='select' field='generic-principal-investigator-select']
          {
          "required":1,
          "default":"",
          "options":[ [your-custom-shortcode][/your-custom-shortcode] ]
          }
        [/cred_generic_field]

We have a PHP API for rendering Views you might find useful:
https://toolset.com/documentation/programmer-reference/views-api/

You can use the PHP API to get the results of a View by passing in arguments just like you would pass in shortcode arguments. Let me know if you have questions about that.

#2178029

Thank you, Christian, for your help. I will try to work with View API and probably Relationship API.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

#2179061

Hi Christian.
I managed to reduce the view nesting to one level only with a custom shortcode. The view works ok on pages.
I placed the view as options into the CRED post form. I keep having an issue that the view as options doesn't work preventing the select field from rendering at all.
But as soon as I add the same view out the generic field and simply place it in the form, all select fields start working. You can find this issue here: versteckter Link

#2179651

Hello there! Christian is not available. If you don't mind, I'll continue with you on this ticket.

However, I am a bit confused. I don't understand what do you mean by as soon as I add the same view out the generic field and simply place it in the form, all select fields start working
Can you add a screenshot, or tell me more details? Where do you place it? What fields are involved? What views?
This way, I can debug it and test it without needing to guess or inspect each view to understand your data model. I am sure you will understand.

#2181111

Hi Jamal. Ok, thank you.
When I add a view as options into the post CRED form like this (code below) it doesn't work and the select field is not showing up:
[cred_generic_field type='select' field='generic-field-select']
{
"required":0,
"default":"",
"options":[ [wpv-view name="view-that-gives-results-in-json-format" postid="[wpv-view name="view-that-give-related-post-id"]"] ]
}
[/cred_generic_field]

but,
when I additionally add the same view somewhere on the form (not as an option in the generic field), for example above the generic field, suddenly the select field with a view as an option start to work. Here is an example of what I am talking about:

[wpv-view name="view-that-gives-results-in-json-format" postid="[wpv-view name="view-that-give-related-post-id"]"]

[cred_generic_field type='select' field='generic-field-select']
{
"required":0,
"default":"",
"options":[ [wpv-view name="view-that-gives-results-in-json-format" postid="[wpv-view name="view-that-give-related-post-id"]"] ]
}
[/cred_generic_field]

----
Just some history: I had a previous ticket related to user CRED forms where Christian helped me (https://toolset.com/forums/topic/create-edit-form-which-combines-user-fields-and-several-custom-post-fields/). There was a similar issue with a user form when you add a view as an option to the select generic field and the field stops showing up. Christian and your support team found out that adding a view again somewhere on the form makes the select field work. Later this issue was fixed in new plugin versions. Maybe this issue was fixed for user forms but not for post forms..(?)