Drupal:如何只返回内容而不返回整个布局?
我正在drupal中使用thickbox模块。我使用的类型是通过thickbox的AJAX请求,我传递URL以仅获取“内容” 我怎样才能只显示返回的内容而不显示主链接和其他东西,比如drupal中的边栏?现在,thickbox还返回主链接 这是我正在传递的URL: 我只需要从return\u me函数中获取“returned content”。 例如,我的代码是:Drupal:如何只返回内容而不返回整个布局?,drupal,Drupal,我正在drupal中使用thickbox模块。我使用的类型是通过thickbox的AJAX请求,我传递URL以仅获取“内容” 我怎样才能只显示返回的内容而不显示主链接和其他东西,比如drupal中的边栏?现在,thickbox还返回主链接 这是我正在传递的URL: 我只需要从return\u me函数中获取“returned content”。 例如,我的代码是: <?php function cec_mypage_menu($may_cache) { $items = arra
<?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 />';
}
?>
我该怎么做
提前谢谢
干杯,
标记尝试使用echo/print
对于Ajax请求,我也有同样的问题,我只是打印内容,而不是返回内容
function return_me() {
print 'Only return this text and nothing more. No primary links, other layouts and stuff.<br />';
}
函数返回_me(){
打印“仅返回此文本,无其他内容。无主链接、其他布局和内容。
”;
}
尝试使用echo/print
对于Ajax请求,我也有同样的问题,我只是打印内容,而不是返回内容
function return_me() {
print 'Only return this text and nothing more. No primary links, other layouts and stuff.<br />';
}
函数返回_me(){
打印“仅返回此文本,无其他内容。无主链接、其他布局和内容。
”;
}
我认为,如果您进行打印,那么die()可以阻止Drupal添加任何其他内容。我认为如果您进行打印,那么die()可以阻止Drupal添加任何其他内容。要做类似的事情,我创建了一个小模块:
<?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);
}
?>
内容的url类似于:
为了做类似的事情,我创建了一个asmall模块:
<?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);
}
?>
内容的url类似于:
谢谢
对于drupal 7,我必须改变:
(……)
/*/*
/*
在管理面板中启用模块之前,我必须创建一个“ajax节点”目录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=ajaxnode/get/node/59访问内容,谢谢
对于drupal 7,我必须改变:
(……)
/*/*
/*
在管理面板中启用模块之前,我必须创建一个“ajax节点”目录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=ajaxnode/get/node/59来访问内容,我想您需要这个来使用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;
}
我想你需要这个来使用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;
}
默认情况下,菜单钩子函数将内容返回到drupal主题层进行主题化。如果您希望停止此过程并仅返回函数的输出,则可以回显内容,然后停止drupal过程
echo '<p>what I want to print</p>';
die();
然后在javascript中使用$.getJson或类似的方法来解析json,并将其放在需要的页面上
另外,如果在添加内容后需要附加drupal javascript行为,请确保在放置内容后调用“drupal.attachBehaviors”。默认情况下,菜单钩子函数会将内容返回到drupal主题层进行主题化。如果您希望停止此过程并仅返回函数的输出,则可以回显内容,然后停止drupal过程
echo '<p>what I want to print</p>';
die();
然后在javascript中使用$.getJson或类似的方法来解析json,并将其放在需要的页面上
另外,如果您需要在添加内容后附加drupal javascript行为,请确保在放置内容后调用“drupal.attachBehaviors”。Hehe这是我在drupal论坛上的问题,但无论如何还是要感谢:)正如Graham在下面所说,您需要死亡()以防止主题层被应用。我没有添加死亡()但它的工作正常如预期。Hhmm也许我会测试更多:)不需要die()来阻止主题布局。但如果您确实执行了die(),则会阻止任何关闭挂钩(包括hook_exit()实现)运行;而不是死();呵呵,这是我在Drupal论坛上提出的问题,不过还是要谢谢你:)正如格雷厄姆在下面说的,你需要死亡()来防止主题层被应用。我没有添加死亡()但是它可以正常工作。Hhmm也许我会测试更多:)不需要die()来阻止主题布局。但如果您确实执行了die(),则会阻止任何关闭挂钩(包括hook_exit()实现)运行;而不是死();