2

I'm developing on the Views Datasource module to enable grouping by field (not field aggregation). I've now got the output format that I want, but I don't know what variable the Grouping Field setting is stored in. I want to check if a grouping field is selected, and then assign the field's name as an array key if it is.

views-views-json-style-simple.tpl.php:

// TODO Enclose everything in an if statement, conditional
//   on a grouping field being selected

$grouped = array();


foreach ($rows["nodes"] as $nodenum => $nodearray) {
  // TODO Replace "building" with variable representing user-
  //   selected field.
  // Variable names will obviously change too.
  $building = $nodearray["node"]["building"]; 
  foreach ($nodearray["node"] as $cprop => $cvalue) {
    // TODO Replace "building" with grouping field variable
    if ($cprop != "building") { // Ignore grouped field
      $grouped[$building][$cprop][$nodenum] = $cvalue;
    }
  }
}

$rows = $grouped; // Pass back to existing code

The above code takes the $rows output by views, and reformats the array right before it goes to _views_json_json_encode. The idea is to take the default output of Views Datasource:

{
  "nodes" : [
    {
      "node" : {
        "name" : "Energy Services Performance Contract (ESCO)",
        "geometry" : "48.7366, -122.486",
        "description" : "This project was initiated In order to advance Western’s sustainability goals and reduce our carbon footprint. The project will implement 38 separate facility improvement measures in various academic and auxiliary buildings identified by an energy audit of Western's campus.",
        "building" : "BH"
      }
    },
    {
      "node" : {
        "name" : "Energy Services Performance Contract (ESCO)",
        "geometry" : "48.7378, -122.486",
        "description" : "This project was initiated In order to advance Western’s sustainability goals and reduce our carbon footprint. The project will implement 38 separate facility improvement measures in various academic and auxiliary buildings identified by an energy audit of Western's campus.",
        "building" : "WL"
      }
    },
    {
      "node" : {
        "name" : "Wilson Becoming Skyscraper",
        "geometry" : "48.7378, -122.486",
        "description" : "Wilson will have 708 floors added to it, becoming the tallest building ever.",
        "building" : "WL"
      }
    }
  ]
}

and turn it into

{
  "BH" : {
    "name" : [
      "Energy Services Performance Contract (ESCO)"
    ],
    "geometry" : [
      "48.7366, -122.486"
    ],
    "description" : [
      "This project was initiated In order to advance Western’s sustainability goals and reduce our carbon footprint. The project will implement 38 separate facility improvement measures in various academic and auxiliary buildings identified by an energy audit of Western's campus."
    ]
  },
  "WL" : {
    "name" : {
      "1" : "Energy Services Performance Contract (ESCO)",
      "2" : "Wilson Becoming Skyscraper"
    },
    "geometry" : {
      "1" : "48.7378, -122.486",
      "2" : "48.7378, -122.486"
    },
    "description" : {
      "1" : "This project was initiated In order to advance Western’s sustainability goals and reduce our carbon footprint. The project will implement 38 separate facility improvement measures in various academic and auxiliary buildings identified by an energy audit of Western's campus.",
      "2" : "Wilson will have 708 floors added to it, becoming the tallest building ever.\n"
    }
  }
}
  • It depends on the stage you want to check for it. For example table descriptions cannot have it at all. Could you show minimal working example of code, with the place you need to know grouping marked with // TODO? – Mołot May 20 '13 at 14:35
0

My question assumed that Views Datasource includes parent view format options such as grouping fields, but it does not. Rather than call all of the parent fields from the Views defaults, I copied only the field definition from views_plugin_style.inc to views_plugin_style_json.inc, within the options_form function.

The second part of my question was how to get the value of that field back out. It exists in $options["grouping"][$i]["field"], where $i is the Grouping Field Number, -1.

So, the value entered into "Grouping Field Nr.1" is $options["grouping"][0]["field"].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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