Skip Navigation

[Resolved] field data manipuation in view output

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.

This topic contains 1 reply, has 2 voices.

Last updated by Christopher Amirian 1 year, 6 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2623919

Hi guys !

We need to do some data manipulation from a field called "aaa" when outputting through a view.
Similar to how we used to do it many years ago in the drupal views interface using the rewrite function.

As an example the data in the field might be:
"Cat Dog Horse 113:114:118"
And we need to output like this:
"cat_dog_horse_113/114/118"

So we need to have the data in the field output with these changes:
- make all the content lowercase
- replace any instance of a space with an underscore
- replace any instance of a colon character : with a forward slash character /

At present the view loop item code is this:
[types field="aaa"][/types]
and it just outputs the raw data:
"Cat Dog Horse 113:114:118"

What do we need to do to make the view data output like this ??
"cat_dog_horse_113/114/118"

cheers !
Daniel

#2624347

Christopher Amirian
Supporter

Languages: English (English )

Hi Daniel,

You will need to create a custom shortcode that does the changes you want. You can add the custom code using the method below:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

The code should be something like this:

function modify_shortcode_output() {
    // Get the output of the existing shortcode
    $output = do_shortcode('[types field="aaa"]');

    // Convert the output to lowercase
    $output = strtolower($output);

    // Replace spaces with underscores
    $output = str_replace(' ', '_', $output);

    // Replace colons with forward slashes
    $output = str_replace(':', '/', $output);

    // Return the modified output
    return $output;
}
add_shortcode('modified_types', 'modify_shortcode_output');

Now inside the view you can use this shortcode that will show the data as you wish:

[modified_types]

Please consider that the code given is just a starting point and the rest should be developed by you as it will be considered as custom development.

Thanks.

#2625491

Chris - you are a hero - thankyou so much for solving this issue !
You guys are absolutely brilliant, the flexibility of the views combined with this simple php code means we can have complete control of our data and output literally almost anything.
Thankyou thankyou thankyou !!!