#1 October 24, 2012 5:07am

philp
Member
Registered: April 5, 2012
Posts: 25

Transforming field values with the parser

Hi,


I'm getting a little bit confused when trying to use the "Parser" options on a module view. I want to be able to transform the value of a JSON field (a text field which is of type `name`) but having trouble working out exactly how I should be defining the output value.


Are there any examples I can look at which might give some clues?

Offline

#2 October 24, 2012 11:43am

Farnsworth
Member
Registered: March 30, 2012
Posts: 13

Re: Transforming field values with the parser

I just had trouble with this yesterday. Great timing!

For a name input, by default you'll see something like this: {"last_name": "Doe","first_name": "John"} which is the `$value`.

To work with that in the parser function use json_decode($value, true). The true as the second parameter was the part throwing me off yesterday, as that's what converts the decoded json into an associative array.

Any data manipulation performed in the parser has to be re-assigned to `$value` to display in the module view.

So, to display "LastName, FirstName", the parser function would look something like this:

$new_value = json_decode($value, true);
$parsed_name = $new_value["last_name"] . ", " . $new_value["first_name"];
$value = $parsed_name;

Offline

#3 October 25, 2012 4:15am

philp
Member
Registered: April 5, 2012
Posts: 25

Re: Transforming field values with the parser

Thanks! I'd tried json_decode but hadn't include the true parameter.

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.