Php 将静态数据附加到drupal视图

Php 将静态数据附加到drupal视图,php,ajax,drupal,drupal-views,Php,Ajax,Drupal,Drupal Views,我需要在drupal中通过ajax将块内容附加到视图结果。我怎样才能做到这一点呢?对于Drupal 7,我相信您可以这样做。其中一些可能是不必要的。我已经有一段时间没有设置这个了,但它对我来说很有用 在template.php文件中添加以下内容: function _phptemplate_variables($hook, $vars) { switch ($hook) { case 'page': // If the page was requested with th

我需要在drupal中通过ajax将块内容附加到视图结果。我怎样才能做到这一点呢?

对于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;
  }
}
在templates文件夹中创建一个模板文件,并将其命名为
page json.tpl.php

<?php
if($messages) {
  $content = $messages.$content;
}
echo drupal_to_js($content);
?>
请参阅这篇关于Drupal.settings.views的文章:

以下是我获得信息的来源:

在drupal7上,您可以使用上下文来实现这一点。是的,在drupal7上,我已经尝试了一些视图挂钩,但没有成功。
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'
        });
    }
});