为 Drupal 7 页面回调自定义 page.tpl.php 以通过 AJAX 提供表单时遇到问题
我正在创建一个自定义模块。我想实现一个可以用作块并在查询对话框中使用的表单(使用ajax请求加载)。我已经创建了块没有问题。我还创建了页面回调函数,没问题。
因此,我可以在块中加载表单,然后拉出 jquery 对话框,它将通过 ajax 请求获取表单。但是,它加载表单和包含页眉和页脚等的完整页面模板......
我想要做的是在获取 ajax 调用时使用不同的页面模板,以便我得到的只是表单。但是,我在网上看到的所有内容似乎都是 drupal 6 和 7 之间的混合,我还没有得到任何工作。
这是我最近的尝试:
在菜单挂钩中
$items['mymodule/get_form_ajax'] = array(
'title' => 'Ajax Form',
'type' => MENU_CALLBACK,
'page callback' => 'get_form_ajax',
'access arguments' => array('access content'),
);
然后我有回调:
function get_form_ajax() {
$form = drupal_get_form('request_form');
$build['items'] = array(
'#theme' => 'request_form',
'#items' => $form,
);
return $build;
}
然后我以这种方式注册主题:
function mymodule_theme($existing, $type, $theme, $path){
return array(
'request_form' => array(
'template' => 'request_form',
'render element' => 'form',
)
);
}
无论如何,这完全不起作用。它只是在对话框中呈现一个空页面。任何帮助将不胜感激。
I am creating a custom module. I want to implement a form that can be used as a block, and used in a query dialog (loaded with an ajax request). I have created the block no problem. I have also created the page callback function no problem.
So, I can load the form in a block and I can pull up the jquery dialog and it will get the form with an ajax request. But, It loads the form and the full page template complete with header and footer etc....
What I want to do is use a different page template when getting the ajax call so that all I get is the form. But, everything I've seen online seems to be a mix between drupal 6 and 7 and I haven't gotten anything to work.
here's my latest try:
in menu hook
$items['mymodule/get_form_ajax'] = array(
'title' => 'Ajax Form',
'type' => MENU_CALLBACK,
'page callback' => 'get_form_ajax',
'access arguments' => array('access content'),
);
Then I have the callback:
function get_form_ajax() {
$form = drupal_get_form('request_form');
$build['items'] = array(
'#theme' => 'request_form',
'#items' => $form,
);
return $build;
}
And then I have the theme registered this way:
function mymodule_theme($existing, $type, $theme, $path){
return array(
'request_form' => array(
'template' => 'request_form',
'render element' => 'form',
)
);
}
Anyway, this totally doesn't work. It just renders an empty page in the dialog. Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您正在进行 AJAX 调用,因此无需让页面正常构建,您只需直接打印渲染的表单并调用
drupal_exit()
:这将为您提供一个漂亮干净的表单,从正在加载的页面继承样式,并且仍然保持表单令牌/缓存完好无损。
Since you're making an AJAX call there's no need to let the page build as normal, you can simply print the rendered form out directly and call
drupal_exit()
:This will give you a nice clean form that inherits the styles from the page it's being loaded into, and still keeps the form tokens/cache in-tact.