marknt15
marknt15

Reputation: 5127

Drupal: How to return only content and not the whole layout?

I am using the thickbox module in drupal. The type I am using is the AJAX request via thickbox and I am passing the URL to get only the 'content'.

How can I show only the returned content without the primary links and other stuff like sidebar in drupal? Right now, the thickbox also returns the primary links.

This is the URL that I am passing: http://cec5/bhutan/?q=en/ceccr/subscribe/59&destination=og

I need only to get the 'returned content' from the return_me function. For example, my code is:

<?php
function cec_mypage_menu($may_cache) {
    $items = array();
    $items[] = array(
        'path' => 'cec_mypage',
        'title' => t('CEC My Page'),
        'access' => TRUE,
        'callback' => 'return_me',
        'type' => MENU_CALLBACK,
    );
    return $items;
}

function return_me() {
    return 'Only return this text and nothing more. No primary links, other layouts and stuff.<br />';
}
?>

How can I do that?

Thanks in advance.

Cheers, Mark

Upvotes: 1

Views: 8402

Answers (6)

oknate
oknate

Reputation: 1148

By default, menu_hook functions return the content to the drupal theme layer to be themed. If you wish to stop this process and just return the output of the functions, you can echo the content, then stop the drupal process.

echo '<p>what I want to print</p>';
die();

If you are trying to return some content through an ajax call, it's advisable to return the content in json form:

  drupal_json_output($data);
  drupal_exit();

Then in your javascript use $.getJson or some such to parse the json and place it on the page where you want it.

Also if you need to attach drupal javascript behaviors after appending your content, be sure to call "Drupal.attachBehaviors" after you place your content.

Upvotes: 0

Savas Vedova
Savas Vedova

Reputation: 5692

I guess you need this for AJAX use.

Here is what I do to accomplish the task:

/**
 * Implements hook_page_delivery_callback_alter().
 */
function YOUR_MODULE_page_delivery_callback_alter(&$callback) 
{
    // If is ajax simply echo the result. Otherwise pass to the default function.
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' &&
       in_array($callback, array('boost_deliver_html_page', 'drupal_deliver_html_page')))
        $callback = 'YOUR_MODULE_ajax_deliver';
}

/**
 * Print the content if we have an ajax request
 */
function YOUR_MODULE_ajax_deliver($result) 
{
    if (is_array($result))
        $result = json_encode($result); // or render() dependending on what you need.

    echo $result;
    return;
}

Upvotes: 2

drupalux
drupalux

Reputation: 9

thanks !

for drupal 7 i had to change:

(...)

function ajax_node_get_node($nid) {
     $n = node_load($nid, NULL, FALSE);
     print drupal_render(node_view($n,'full'));
    }

    function ajax_node_get_node_teaser($nid) {
     $n = node_load($nid, NULL, FALSE);
     print drupal_render(node_view($n, 'teaser'));
    }

/* http://api.drupal.org/api/drupal/modules--node--node.module/function/node_view /*

/* I had to create an "ajax-node" directory sites/all/modules/mymodule, put the previous script in ajax-node.module, and create a ajax-node.info file (see below) before enabling the module in the admin panel.

ajax-node.info:

description = "A module to fetch a node for ajax purposes."
core = "7.x"
version = "7.x-1.0 alpha"
package = "AJAX"

*/

Then you can reach the content with http:/example/q=ajax-node/get/node/59

Upvotes: 0

Tom
Tom

Reputation: 1381

To do something similar I created a asmall module:

<?php
/**
 * Implementation of hook_menu().
 */
function ajax_node_menu() {
  $items['ajax-node/get/node'] = array(
    'page callback' => 'ajax_node_get_node',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,

    );

  $items['ajax-node/get/node-teaser'] = array(
    'page callback' => 'ajax_node_get_node_teaser',
    'access arguments' => array('access content'),  
    );

  return $items;
}

function ajax_node_get_node($nid) {
 $n = node_load($nid, NULL, FALSE);
 print node_view($n);
}

function ajax_node_get_node_teaser($nid) {
 $n = node_load($nid, NULL, FALSE);
 print node_view($n, TRUE);
}

?>

The url for the content will be something like : http://www.example.com/q=en/ajax-node/get/node/59

Upvotes: -1

Graham
Graham

Reputation: 667

I think that this will work if you do a print, then die() to stop Drupal from adding anything else.

Upvotes: 0

IvanSF
IvanSF

Reputation: 301

try using echo/print.

For Ajax requests I have the same problem and I just print the content instead of returning it.

function return_me() {
    print 'Only return this text and nothing more. No primary links, other layouts and stuff.<br />';
}

Upvotes: 3

Related Questions