1
votes

Bellow is the code for a custom module that parses json to ios devices. The thing is on my drupal page I've created a node hierarcy using "nodehierarcy" module. I've got two types of custom contents - phones and events. For each phone I have a list of events which I want to parse. The problem with the code below is that it's parsing all of the jobs instead of categorizing them per device. The json output url is: "xyz.com/job/json". But I want something like xyz.com/?q=node/4/json, node 4 representing a phone. Any ideas?

function job_menu() {
    $items =array();

    $items['job/json'] = array(
        'title' => 'Json callback',
        'page callback' => 'message_json_page',
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK,
    );
    return $items;   
}

function message_json_page(){
    $sql = "SELECT n.nid , n.title as name FROM {node} n 
            WHERE n.status = 1 and n.type = :type";
    $result = db_query($sql, array(':type'=>'job'))->fetchAll();
    $json_value = json_encode($result);
    print $json_value;
}
1

1 Answers

0
votes

Paths in Drupal go one of two ways...if you have clean URLs enabled then you'll access a page at something like mysite.com/node/4/json. If you don't have clean URLs on you'll access the same page at mysite.com?q=node/4/json (which as it happens is exactly what you're after).

That in mind, you would set up a menu item for node/*/json, and use some of the node hierarchy internal functions to get a list of the children nodes in your page callback:

function job_menu() {
  $items['node/%node/json'] = array(
    'title' => 'Json callback',
    'page callback' => 'message_json_page',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function message_json_page($node) {
  $children_links = _nodehierarchy_get_children_menu_links($node->nid, FALSE);

  $nids = array();
  foreach ($children_links as $child_link) {
    list(, $nid) = explode('/', $child_link['link_path']);
    $nids[] = $nid;
  }

  $sql = "SELECT n.nid , n.title as name FROM {node} n WHERE n.nid in (:nids) AND n.status = 1 and n.type = :type";
  $result = db_query($sql, array('nids' => $nids, ':type'=>'job'))->fetchAll();

  $json_value = json_encode($result);
  print $json_value;
}