Skip Navigation

[Resolved] Use a View to generate dynamic options for a generic field

This thread is resolved. Here is a description of the problem and solution.

Problem: I am trying to use a View to generate the options for a custom generic hidden field, but the options are written out onto the page instead of being interpreted as form field options.

Solution: Ensure the output of your View does not include any extraneous characters or hidden markup. Follow the instructions on this ticket for additional details about code formatting:
https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms

In your View's Loop Output:

<wpv-loop>
      [wpv-item index=1]
        {"value":"[wpv-post-id]","label":"[wpv-post-title]"}
      [wpv-item index=other]
      ,{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>

In your View's filters:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
 
function prefix_clean_view_output( $out, $id ) {
  if ( $id == '12345' ) { //Please adjust to your Views ID
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}
This support ticket is created 7 years, 1 month ago. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by John Johansen 7 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#583050
Toolset_ticket_postfields_20171029_002.PNG
Toolset_ticket_postfields_20171029_001.PNG

I am trying to: Populate a select dropdown list using a View

Link to a page where the issue can be seen:

I expected to see:
The select dropdown options that are based on the View results
Instead, I got:
The select form element does not work (only the label is displayed).
If I insert the view shortcode somewhere on the page outside of the form, the view results in JSON format are displayed.

I am wondering whether the view shortcode 'name' parameter value is correct. That is, should the value be the 'name' of the view or should it be the 'slug' of the view? However, I have been able to display the results on the page but outside of the CRED form, so the name/slug value must be working somehow.

#583139

Hi, there are two critical steps you must follow to use a View as the source of options for a generic field.
1. Structure your View's Loop Output correctly. The first item must not include a comma, but all other items must include a leading comma:

<wpv-loop>
      [wpv-item index=1]
        {"value":"[wpv-post-id]","label":"[wpv-post-title]"}
      [wpv-item index=other]
      ,{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>

Obviously you may need to adjust the value and label as needed to match your desired output.

2. Filter the View's output to include only the raw text. If you're not filtering the View's output, it will include extra markup that will break the generic field options. Add the following code to your child theme's functions.php file:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
  if ( $id == '12345' ) { //Please adjust to your Views ID
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

Adjust the 12345 to match your View's numeric ID.

Let me know if this isn't working and I'll take another look.

#583151

Christian:
Thanks for your response.

Before I update my code, I would like to clarify some things:

1)
The code for my view was taken directly from an example on another trouble ticket (and my code resembles the code provided in response to other related tickets).

2)
The other tickets did not mention any requirement to add code to the function.php file.
Do you know why?

3)
One of my screenshots shows the output of my current view (without any new code added to my function.php file).
Based on my visual review of said output, it appears to be structurally valid. Further, before submitting this ticket, I copied said output into the options section of a CRED generic select field, and it worked correctly.
Please see the top section of the page screenshot and examine the JSON-format output displayed there. If said output is structurally invalid, please specify.

4)
The screenshot of my code indicates that the shortcode used outside the CRED form and the shortcode used inside the generic select field are identical. Is this correct? Or, is there a specific requirement regarding the shortcode details used inside the CRED generic select field options?

So, while I appreciate the feedback provided so far (and I will probably implement the change to the function.php file), I'm not sure that it addresses the primary issue I'm having. That is, the view output appears to be correct when rendered outside the CRED form but renders nothing when included inside a CRED generic select field.

Thoughts?

#583353

1) The code for my view was taken directly from an example on another trouble ticket (and my code resembles the code provided in response to other related tickets).
Okay, understood.

2) The other tickets did not mention any requirement to add code to the function.php file.
Do you know why?

Not offhand, no. Assuming the other tickets are using Views to generate the options for a generic field, then the functions.php file changes are necessary to format the output correctly. I usually use this ticket as a reference:
https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms

3) One of my screenshots shows the output of my current view (without any new code added to my function.php file).
Based on my visual review of said output, it appears to be structurally valid.

Sure, but visual review of the output in your browser is not entirely relevant here. HTML markup will be added to the source output that is not visible in the rendered output. There will be line breaks and spaces, wrapper div elements, HTML comments, and so forth, that will not be visible unless you look at the DOM using the browser inspector or a similar tool. Those elements will break the structure required for your generic field options.

Further, before submitting this ticket, I copied said output into the options section of a CRED generic select field, and it worked correctly
This reinforces what I'm trying to explain, actually. Copy + pasting the output could result in functional code because the hidden markup I was mentioning before is not copied. Only the visible text is copied, and that's likely to result in a valid structure. I can see how this is confusing if you're not familiar with the technical aspects of HTML.

Try it out and let me know how it goes, or if you have more questions about why it's required to add this formatting change in functions.php I'll be glad to go into more detail.

#583559

Christian:
Your solution worked perfectly. So, thanks for that.

That said, in the ticket that you used as a reference, the OP made a suggestion for a feature that would eliminate the need to add code to the function.php file. I strongly support this suggestion, mainly because it would eliminate the need to modify the functions.php file for each new view that is used to render this type of specialized output.