我需要通过drupal中的ajax将块内容附加到视图结果请求中。我怎样才能做到这一点?
答案 0 :(得分:0)
对于Drupal 7,我相信你可以这样做。其中一些可能没有必要。我设置它已经有一段时间了,但它对我有用......
在你的template.php文件中添加:
function _phptemplate_variables($hook, $vars) {
switch ($hook) {
case 'page':
// If the page was requested with the jQuery ajax functionalities, an HTTP header (X-Requested-With: XMLHttpRequest)
// will be sent to the server, making it possible to identify if we should serve the content as JSON
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XmlHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']) {
// Now that we know that the page was requested via remote scripting (AJAX) we can serve the content as JSON
// by telling Drupal to use a different template for the page (in this case page-json.tpl.php)
$vars['template_files'] = is_array($vars['template_files']) ? $vars['template_files'] : array();
$vars['template_files'][] = 'page-json';
}
break;
}
}
在模板文件夹中创建模板文件,并将其命名为page-json.tpl.php
:
<?php
if($messages) {
$content = $messages.$content;
}
echo drupal_to_js($content);
?>
然后在你的script.js文件中:
jQuery(document).ready(function($){
if(typeof Drupal.settings.views != "undefined")
{
var data = {};
// Add view settings to the data.
for (var key in Drupal.settings.views.ajaxViews[0]) {
data[key] = Drupal.settings.views.ajaxViews[0][key];
}
// Get the params from the hash.
if (location.hash) {
var q = decodeURIComponent(location.hash.substr(1));
var o = {'f':function(v){return unescape(v).replace(/\+/g,' ');}};
$.each(q.match(/^\??(.*)$/)[1].split('&'), function(i,p) {
p = p.split('=');
p[1] = o.f(p[1]);
data[p[0]] = data[p[0]]?((data[p[0]] instanceof Array)?(data[p[0]].push(p[1]),data[p[0]]):[data[p[0]],p[1]]):p[1];
});
}
$.ajax({
url: Drupal.settings.views.ajax_path,
type: 'GET',
data: data,
success: function(response) {
console.log(response);
// look into the log to see what results get back
},
error: function(xhr) {
},
dataType: 'json'
});
}
});
看到这篇关于Drupal.settings.views的帖子: Embed a View using AJAX
以下是我从中获取信息的来源: http://drupal.org/node/174008