Drupal:使用前端主题从管理员呈现 View

标签 drupal drupal-7 drupal-modules drupal-views drupal-theming

有谁知道在编辑节点后使用默认主题以编程方式从模块呈现 View 的方法?

我基本上是在尝试创建 View 的静态 html 页面。

我在自定义模块中有以下代码:

function MODULENAME_node_update($node) {
  unset($node->is_new);
  unset($node->original);    
  entity_get_controller('node')->resetCache(array($node->nid));
  $view = views_get_view('references');
  $view->set_display('block');
  $output = $view->render();
  file_put_contents('references.html', $output);
}

该代码有效,但出于显而易见的原因,它使用管理主题呈现 View 。

我试了好几种方法都无济于事:

变量集

function MODULENAME_node_update($node) {
  variable_set('admin_theme', 'DEFAULT THEME HERE');
  [...]
  variable_set('admin_theme', 'ADMIN THEME HERE');
}

这个钩子(Hook)可能不是切换主题的正确位置,因为它调用得太晚了。

全局 $custom_theme

function MODULENAME_node_update($node) {
  global $custom_theme;
  $custom_theme = 'DEFAULT THEME HERE';
  [...]
  $custom_theme = 'ADMIN THEME HERE';
}

自定义菜单项

function MODULE_NAME_menu(){
  $items = array();

  $items['outputview'] = array(
    'title' => 'Test',
    'type' => MENU_CALLBACK,
    'page callback' => 'MODULE_NAME_output_view',
    'access callback' => TRUE,
    'theme callback' => 'DEFAULT THEME HERE'
  );

  return $items;
}

function MODULE_NAME_output_view() {
  $view = views_get_view('references');
  $view->set_display('block');
  $output = $view->render();
  file_put_contents('references.html', $output);
}

function MODULE_NAME_node_update($node) {
    unset($node->is_new);
    unset($node->original);
    entity_get_controller('node')->resetCache(array($node->nid));
    menu_execute_active_handler('outputview', FALSE); // or via curl
}

这在 View 正确呈现但仍使用管理主题时有效。

hook_custom_theme

function MODULENAME_custom_theme(){
  return 'DEFAULT THEME HERE';
}

最佳答案

我正在寻找类似的东西。我找到了一些这样做的代码(参见补丁 #3 https://drupal.org/node/1813350),但它对我们实现 Shortcode contrib 模块没有帮助。希望它对您有用或帮助您朝着正确的方向前进。

这是我们从补丁衍生出来的尝试:

$custom_theme_bu = drupal_static('menu_get_custom_theme');
$custom_theme = &drupal_static('menu_get_custom_theme');

$custom_theme = variable_get('theme_default', 'bartik');
unset($GLOBALS['theme']);
drupal_theme_initialize();

$embed_view = views_embed_view('YOUR_VIEW_ID', 'YOUR_VIEW_DISPLAY_ID');

$custom_theme = $custom_theme_bu;
unset($GLOBALS['theme']);
drupal_theme_initialize();

关于Drupal:使用前端主题从管理员呈现 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19560360/

相关文章:

css - Drupal 站点上的 IE 问题 CCS div

javascript - 如何将 AdWords 转化跟踪代码添加到 drupal 中的特定表单

css - 个性化 Bootstrap 主题?

php - Drupal 7默认用户注册表单如何实现多步

javascript - Drupal - 轻松将自定义代码转换为 drupal 模块?

Drupal, View : how to avoid duplicated?

css - Drupal 7 中的 Twitter Bootstrap 列未在 Safari 中排列

git - Drupal 从哪里获取模块的开发版本日期?

json - 如何将 Drupal 服务默认响应格式更改为 JSON?

php - Facebook 对 Drupal 的评论(所有节点上的评论相同)