Problem/Motivation

I'm using the JSON of Views Datasource as a source for an AngularJS app. I'm just basically printing out a list of nodes (default behaviour) Each node has a tag field (taxonomy reference) which can refer to multiple terms. The end result looks something like this (lists 1 node, title & tags field):

{
  "nodes" : [
    {
      "node" : {
        "title" : "TEST",
        "Tags" : "lorem, ipsum"
      }
    }
  ]
}

The problem is that I would like the value of the"Tags" property to be an array instead of a comma separated string since my Angular app needs to loop through those tags a certain point in the flow. I could parse the string into an object myself on the front end side, but then I would be trying to fix something which isn't happening on the backend side.

Desired output:

{
  "nodes" : [
    {
      "node" : {
        "title" : "TEST",
        "Tags" : ["lorem", "ipsum"]
      }
    }
  ]
}

I'm currently using the standard taxonomy term views field to display the tags. I've tried to use the "Taxonomy all terms" views field handler, but that yields the same result. I've toyed with aggregation, relationships, etc. but then I would get duplicate rows (2 entries, 1 per tag) which isn't what I want.

Is their a way which doesn't involve writing a lot of code or a custom views taxonomy handler to do this?

Thanks!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ziobudda’s picture

Hi, have you found a solution for this ?

M.

joehudson’s picture

I created a custom field formatter to take the tid of the taxonomy ref field from views and generate the desired array. I then had to modify the views_json module to allow that array through to the json_encode call.

I attach a patch file for the modified views_json code, and a zip of my simple module with a custom formatter.

The patch also fixes a bug with the bitmask and the character encoding option not being picked up.

yannickoo’s picture

Status: Active » Needs work

Thanks for the patch but could you please convert the tabs to spaces and remove the trailing spaces? Also a variable like $isjsonready should be named like $json_ready (no camel-case) :)

anthonylindsay’s picture

Status: Needs work » Closed (outdated)

So understandably enough, with all the code changes over the last two years, that patch applies about as cleanly as last month's dish-cloth dipped in mud.

So I'm going to close this due to inactivity. @joehudson: if you wanna re-roll your extensive patch for the current dev I'm happy to give it another go.

Albert Volkman’s picture

I attempted to bring this patch up to date, but it doesn't seem to work.

B.Friddy’s picture

Hi Albert, just wondering if you happen to know where you think things need to altered to make your patch work? I'm very interested in having this implemented as well.

Thanks

B.Friddy’s picture

This isn't ideal, but I'm manipulating the output after the fact and saving it to a new .json file

Here is my code if anyone is interested.

$url = 'feed.json';
$jsonString = file_get_contents($url);
$data = json_decode($jsonString, true);

foreach($data['places'] as &$value)
{
//Create array from tag ids
$value['tags'] = explode(",",$value['tags']);
//Trim the whitespace
$value['tags'] = array_map('trim',$value['tags']);
}

//We don't want to escape slashes (for my particular purposes)
$newJsonString = json_encode($data,JSON_UNESCAPED_SLASHES);
file_put_contents('updated.json', $newJsonString);