Drupal:如何只返回内容而不是整个布局?

时间:2009-09-22 03:46:33

标签: drupal

我在drupal中使用thickbox模块。我使用的类型是通过thickbox的AJAX请求,我传递的URL只获取'内容'。

如何在没有主链接的情况下显示返回的内容以及drupal中的侧边栏等其他内容?现在,thickbox还返回主链接。

这是我传递的网址: http://cec5/bhutan/?q=en/ceccr/subscribe/59&destination=og

我只需要从return_me函数中获取'返回的内容'。 例如,我的代码是:

<?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 />';
}
?>

我该怎么做?

提前致谢。

干杯, 标记

6 个答案:

答案 0 :(得分:3)

尝试使用echo / print。

对于Ajax请求,我遇到了同样的问题,我只是打印内容而不是返回它。

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

答案 1 :(得分:2)

我猜你需要这个用于AJAX使用。

以下是我完成任务的方法:

/**
 * 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;
}

答案 2 :(得分:0)

我认为如果你进行打印,这将有效,然后die()来阻止Drupal添加任何其他内容。

答案 3 :(得分:0)

谢谢!

对于drupal 7我不得不改变:

(...)

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 / *

/ * 我必须创建一个“ajax-node”目录sites / all / modules / mymodule,将前一个脚本放在ajax-node.module中,并在管理员中启用模块之前创建一个ajax-node.info文件(见下文)面板。

ajax-node.info:

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

* /

然后你可以通过http:/ example / q = ajax-node / get / node / 59

来访问内容

答案 4 :(得分:0)

默认情况下,menu_hook函数将内容返回到以主题为主题的drupal主题图层。如果你想停止这个过程并只返回函数的输出,你可以回显内容,然后停止drupal过程。

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

如果您尝试通过ajax调用返回某些内容,建议以json格式返回内容:

  drupal_json_output($data);
  drupal_exit();

然后在你的javascript中使用$ .getJson或其他一些来解析json并将它放在你想要的页面上。

此外,如果您在附加内容后需要附加drupal javascript行为,请务必在放置内容后调用“Drupal.attachBehaviors”。

答案 5 :(得分:-1)

为了做类似的事情,我创建了一个小模数:

<?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);
}

?>

内容的网址如下: http://www.example.com/q=en/ajax-node/get/node/59