Skip Navigation

[Resolved] Raw Views Output

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

Problem:
Is it possible to populate a 3rd Party Plugin Chart with data from Views?

In other words, how can I obtain a raw output (values only) from a Views Loop?

Solution:
It is not possible without custom code, and that custom code has potential to ruin many feature in Views.

Hence this is a not supported or suggested way of using Toolset Views.

If you still want to proceed (on your own risk), here are possible solutions:
https://toolset.com/forums/topic/use-chart-with-view-shortcode/#post-554187
https://toolset.com/forums/topic/use-chart-with-view-shortcode/#post-555027

This support ticket is created 7 years, 3 months 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.

Our next available supporter will start replying to tickets in about 1.92 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 11 replies, has 2 voices.

Last updated by ahmedA-7 7 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#554172

I try to use this chart for post count
hidden link

its work inside view only whith this shortcode [wpv-found-count]

and i want post count by custom field values

there is any way to provid data to chart by view short code like [wpv-view name="my field" fieldvalue="Survey"] ??

or inside view create shortcode somthing like this : [wpv-found-count fieldvalue="Survey"] , [wpv-found-count fieldvalue="Comment"]

generally I want provid chart data in javascript via view [shortcode]

#554187

No, this is a not supported and forbbed appraoch to use Views.

1. WordPress generally does not allow ShortCodes to be used as HTML, CSS or JS values
2. Toolset adds some magic to make that possible here and there (nested ShortCodes, and so on)
3. Still, we are bound to the platform we produce the Plugin for, and that is WordPress
4. Views, bottom line, is a Data Renderer and not a Data Provider.

We wrap every View output in a certain, default HTML.
That HTML will destroy your chart, since the value Views returns is not a "raw" value.

There is one possible workaround, but that is on your own risk, and there are limits to it.

1. Sometimes we need to populate values in as example Select Boxes (JSON) or of 2rd Party ShortCodes like “Chart Generators”.
This is also your case here.

2. This snippet allows you to output a given Views Loop without default HTML wrapped around the results.

3. The following snippet produces a clean View output, or as clean as we can:

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

function prefix_clean_view_output( $out, $id ) {
if ( $id == '375' ) {
$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 );
}
}
return $out;
}

4. Use a Views Loop similar to the one in the screenshot

5. Step by step:

- It hooks early in the View output.
- Priority 5 is mandatory as we already do some other cleaning at priority 10.
- It only affects a View with an ID of 375, adjust acordingly if needed.
- It only affects Views with actual results: if the View matches no result, the same “No items found” or whatever you have between the wpv-no-items-found shortcode applies.
- It returns only what is between the HTML comments <!– wpv-loop-start –> and <!– wpv-loop-end –> , excluding them. Only content between those HTML comments is returned, as is.
- Notice that, with this applied to a given View ID:
-- Pagination, specially AJAX pagination, will not work.
-- Parametric search, specially AJAXed parametric search, will not work.
-- Other future features will not work either.

So this is to be used if and only if you know what you are doing and you are going to use the View output as source for anything else.

Also notice that this only clears the View structure.

Building as example JSON inside a View output is wrong because we can not address quoting problems.

Please use this with caution, considering the side effects and responsibly.

==> We have a feature filed for a View Data Provider, but that is not yet defined and has no ETA or green light from the developers at all.
I will add your voice to the Internal issue tracker.

Thanks!

#554346

Great ! That's what I really need ???? , okay what if I need apply more than one view ID ?

How can modify function? Can I do like this?

function prefix_clean_view_output( $out, $id ) {
if ( $id == '375'&&'406' )

and also how write view short code if there is space in (field value) like this : [wpv-view name="viewname" fieldvalue="SD Survey"]

because if there is space in field value the chart not working.

#554349

Overlook this one :

'' and also how write view short code if there is space in (field value) like this : [wpv-view name="viewname" fieldvalue="SD Survey"]

because if there is space in field value the chart not working. ''

sorry I'm forgot put " " between value . I already solved this one.

#554372

- After more testing I found one problem ,

" [wpv-no-items-found] " not between the HTML comments <!– wpv-loop-start –> and <!– wpv-loop-end –>

If there is no items found its not returns 0 see my view code :


[wpv-layout-start]
	[wpv-items-found]
<!-- wpv-loop-start -->
		<wpv-loop>

		</wpv-loop>

[wpv-found-count]

<!-- wpv-loop-end -->

[wpv-no-items-found]
<strong>[wpml-string context="wpv-views"]0[/wpml-string]</strong>
[/wpv-no-items-found]

	[/wpv-items-found]
[wpv-layout-end]

#555027

As said, Ahmed, this is Custom code, and additionally, it is not advised, nor supported to use Views or generally, Toolset, like this.

We will not assist any issue surging from it.

You can apply the same code to several Views by using an Array.
Like:

if (in_array($view_id, array( "3910", "1992", "1927", "673", "1994", "2000", "2004", "2008", "3198", "3197", "3196", "3195", "3463", "3535" ) ) )  {

This is PHP, not Toolset API.

Then, for the issue with the "Empty" View, we also have an adjusted snippet versionfor that, but again, we do not advise, support or encourage to use it:

function prefix_clean_view_output( $out, $id ) {
    if ( $id == '71' ) { //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;
}

I have not tested that code.

Another idea is to use the same code as I provided earlier again, but this time address something like '<!-- wpv-nothing-found-start -->' and '<!-- wpv-nothing-found-end -->'
Those two HTML tags need of course to be inserted to the View loop ad the proper place.

The function will then find the tags and strip whatever is inbetween.

I feel unconfortable giving you this solutions, as they will not be assisted or debugged here.

Please be careful with them.

Thank you.

#555127

I did like this :

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
function prefix_clean_view_output( $out, $id ) {
	if (in_array($id, array( "1280", "1369" ) ) )  {
        $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;
}

it's work fine but if result is not found it's return Empty not number 0
I want return number 0

#555138

As I mentioned, this is not suggested, not supported Custom manipulation of Toolset.

I cannot assist this further, I am sorry.

I have given all possible examples on how to clean out the View - and here I also mention, this has consequences on the Views that are not outputting anything:
https://toolset.com/forums/topic/use-chart-with-view-shortcode/#post-554187

Here I mention how to solve that (2 possible approaches):
https://toolset.com/forums/topic/use-chart-with-view-shortcode/#post-555027

It is not allowed to me to support this further:
https://toolset.com/toolset-support-policy/

Views is not built for this sort of customization either, unfortunately.

I suggest, to use Custom PHP or to await updates related a View that returns clean output - but as said, that is not yet in the pipelines.

I apologise deeply that I cannot assist this further.

#555172

Thank you Beda I really appreciate your efforts, I will try use Custom PHP for this, and I will clear this filter from my theme funcuton.php So as not to affect the "View" work.

#555199

Today after updated plugin I found some changes:

"Pagination enabled with manual transition and AJAX" not working

"Datepicker" not working in Post forms

I think there is a problem in AJAX

This happened after the update

I useing Versions :

Toolset Views: Version 2.4.1
Toolset Types: Version 2.2.14
Toolset Layouts: Version 2.0.2
CRED Toolset: Version 1.9.1
Access Toolset: Version 2.4.2

#555231

I am sorry to hear this

Could you report this in a new ticket?

I am not working right now. And we need Each ticket to be "on topic"

If you report it to a new ticket a supporter will be with you shortly.

Thank you!

#555248

Thank you Mr. Beda I really appreciate your efforts