4

I am loading node content to a specific div with Ajax. Is it possible to load a fully themed and rendered node (themed via page--node--1.tpl.php) with Ajax?

I have a custom module controlling my Ajax functions. From what I understand, the code below should be returning a fully themed node - but it is not doing so:

function ajax_reader_init() {
    drupal_add_js('misc/jquery.form.js');
    drupal_add_library('system','drupal.ajax');
}

/**
 * Implements hook_menu()
 */
function ajax_reader_menu() {
    $items['ajax-reader'] = array (
        'page callback' => 'ajax_link_response',
        'access callback' => 'user_access',
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK
    );
    return $items;
}

function ajax_link_response($type = 'ajax', $nid = 0) {
    $output = _ajax_reader_load_noder($nid);
    if ($type == 'ajax'){
        $commands = array();
        $commands[] = ajax_command_replace('.main', '<div class="main">'.$output.'</div>');
        $page = array(
            '#type' => 'ajax',
            '#commands' => $commands
        );
        ajax_deliver($page);
    } else {
        $output = '<div class="main">'.$output.'</div>';
        return $output;
    }
}

function _ajax_reader_load_noder($nid = 0) {
    $node = node_load($nid);
    if($node) {
        $vnode = node_view($nid);
        return theme("node", $vnode);
    }
}

1 Answer 1

3

There is a contributed module Ajaxify Drupal with JQuery Ajax available for this. You can load any pages, nodes or anything to a div by using ajax without loosing css and ajax behaviour of loading content.

Ajax links API module help you to load any content to a specified DIV or ajaxify whole drupal site, using JQuery Ajax. This module will also load JS for Ajax-loaded content by using Drupal.attachBehaviors(). In case you dont want to load js or css, you can alter tpl for path/node type.

2
  • The module you suggested seems to be working, however the node being loaded is not themed via the matching tpl file. Any ideas?
    – Warren
    Commented Dec 19, 2013 at 11:42
  • I didn't get you. you can use node-contenttype.tpl.php to theme a particular type of node.
    – Cool
    Commented Dec 19, 2013 at 12:05

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.