2

I'm using "views datasource" to output a view as JSON - for each views row it's creating a JSON object with the fields as string/value pairs - all those objects are in an array which is the value to a string specified by the "Root object name" in the view's format settings. It looks like this:

{
  "nodes":[
    {
      "nodeid":"1",
      "title":"foo"
    },
    {
      "nodeid":"2",
      "title":"bar"
    }
  ]
}

Now I need to add another string/value pair to the main JSON object so it looks like this:

{
  "nodes":[
    {
      "nodeid":"1",
      "title":"foo"
    },
    {
      "nodeid":"2",
      "title":"bar"
    }
  ],
  "total_nodes":"2",
  "some_other_stuff":"here"
}

In another question I found out how I could add elements to the view's results but now I need to add elements "next to" the view's results. I just don't know where I need to put my code. Can anyone point me to a hook that would do the trick?

5
  • If I understand correctly, you just have to use the same hook as in the other post, but just cange the structure of the new result array. I didn't test this, but chances are good that adding a new element to the $view->result object won't do any harm.
    – Countzero
    Commented Mar 29, 2012 at 14:28
  • It won't do any harm but it won't solve my problem either :) Everything I do to $view->result only affects the JSON array that is the value for "nodes" but instead I want to add a new string/value pair at the same level as "nodes" Commented Mar 29, 2012 at 15:04
  • So then you could implement something in the view template for the JSON display, but I don't know if there's one. The correct file to modify seems to be views-views-json-style-simple.tpl.php.
    – Countzero
    Commented Mar 29, 2012 at 15:19
  • Great...that's exactly the right place! You might want to post that as an answer :) Commented Mar 29, 2012 at 15:34
  • Done. See answer below.
    – Countzero
    Commented Mar 29, 2012 at 17:41

1 Answer 1

3

You could implement something in the view template for the JSON display.

The correct file to modify seems to be views-views-json-style-simple.tpl.php.

You have to copy it to your theme's template folder (usually template's root directory, or template subdirectory) and add the necessary data to the $json variable before the correct print $json statement.

3
  • 2
    Thank you very much! I ended up doing the changes in the preprocess function for that template file since I want to have this in a module and not a theme. The function is modulename_preprocess_views_views_json_style_simple(&$vars) Commented Mar 30, 2012 at 8:05
  • Glad you managed to make it work, and took time to complete the information. Have a nice day.
    – Countzero
    Commented Mar 30, 2012 at 8:11
  • Great solution !! for people interested, on modulename_preprocess_views_views_json_style_simple(&$vars) alter the key $vars['rows'], i'm using this with a search api view for adding facets filters
    – ropic
    Commented Mar 29, 2016 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.