0

I want functionality like this: when user clicks a node link in a calendar view, the details of the node should be loaded tu another region with AJAX. How to achieve this?

1 Answer 1

1

You can take help of Drupal hook_menu with Json object output and a Drupal JS beahvior to handle click event.

Idea : In the click event call an ajax function to your menu callback that gets you a JSON object of required content. Load the content in your desired DOM element.

code for hook_menu; considering your module name 'mymodule'

function mymodule_menu() {
  $items['mymodule/%node/load-content'] = array(
      'page callback' => 'mymodule_ajax_node_load_content',
      'page arguments' => array(1),
      'access arguments' => array('access content'),
  );
  return $items;
}

code for Page callback :

function mymodule_ajax_node_load_content($node) {
  // The variable holds complete node object for you.

  $data = array();

  $data['body'] = $node->body['0']['value']; 
  // Consider providing any other node details if required.

  $output = array('status' => 'ok', 'data' => $data);

  return drupal_json_output($output);
}

JS code for attaching a behavior to Drupal,

Drupal.behaviors.nodeLinkClick = {
  attach: function(context, settings) {
    // sort by click.
    $("a.node-link").click(function(e){
      e.preventDefault();
      $.ajax({
        url: Drupal.settings.basePath + 'mymodule/' + nid + '/load-content',
        type: 'GET',
        data: data,
        dataType: 'json',
        success: function(response) {
          $(".dom_to_load_content").html(response.data.body);
        },
        error: function() {
          // show error in any DOM.
        },
      });
    });
  }
}

NOTE : The above code not tested; a small snippet cut-out from my project. Hope this helps you. Let me know if you still facing any problem.

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.