2

I tried to realize a site using ajax to show its content following this guide.

The result is a module with following code:

<?php
    function simple_ajax_menu() {
        $items['simple_ajax/%/%node'] = array(
                'access arguments' => array('access content'),
                'page callback' => 'simple_ajax_callback',
                'page arguments' => array(1,2),
                'type' => MENU_CALLBACK,
        );
        return $items;
    }

    function simple_ajax_callback($js, $node) {
        // If the nojs didn't get changed to ajax, the user has no javascript.
        // Send them to the regular node page instead.
        if ($js == 'nojs') {
            drupal_goto('node/' . $node->nid);
        }

        // Get the render array for the node.
        $build = node_view($node);

        // Render the node into html.
        $html = render($build);

        // Prepare an ajax command to insert the node html into our ajax wrapper.
        $commands = array();
        $commands[] = ajax_command_html('#block-system-main', $html);

        // Render the commands into JSON and print them.
        print ajax_render($commands);
        exit;  // Exit so Drupal doesn't have a chance to build a whole page.
    }

This shows the correct node at the correct position but does not use the node template as it would when requesting /node/[nid]. I have set up a page for "/node/%node" with a variant "Product" that is shown when requesting a node and want this to be shown when called with ajax as well.

So far I tried setting the view mode to the Product variant.

        // Get the render array for the node.
        $build = node_view($node, "Product");

Without any effect.

Any suggestions what I am missing? Thanks in advance!

0

Your Answer

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

Browse other questions tagged or ask your own question.