有人知道在编辑节点后使用默认主题以编程方式从模块呈现视图的方法吗?
我基本上是在尝试创建视图的静态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);
}
代码有效,但由于显而易见的原因,它使用管理主题呈现视图。
我尝试了几件事无济于事:
variable_set
function MODULENAME_node_update($node) {
variable_set('admin_theme', 'DEFAULT THEME HERE');
[...]
variable_set('admin_theme', 'ADMIN THEME HERE');
}
这个钩子可能不是切换主题的正确位置,因为它被调用太迟了。
全球$ 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
}
这适用于视图正确呈现但仍使用管理主题。
hook_custom_theme
function MODULENAME_custom_theme(){
return 'DEFAULT THEME HERE';
}
答案 0 :(得分:1)
我正在寻找类似的东西。我发现了一些代码(参见补丁#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();
答案 1 :(得分:0)
以下是一些基于answer by lmeurs的自记录代码:
/**
* Switch to or from an alternative theme in the middle of a request.
*
* This is useful if you need to render something (like a node) in a different
* theme without changing the theme of the entire page. An example use case is
* when you need to render something for a front end user from an admin page.
*
* Usage example:
* my_module_switch_theme('bartik');
* $node = node_load(1);
* $renderable = node_view($node);
* $rendered = render($renderable);
* my_module_switch_theme();
*
* @param string|null $to
* The name of the theme to switch to. If NULL, it switches back to the
* original theme.
*/
function my_module_switch_theme(string $to = NULL) {
// Backup the original theme.
static $original_theme;
if (empty($original_theme)) {
$original_theme = drupal_static('menu_get_custom_theme');
}
// Get a reference to the current theme value.
$custom_theme = &drupal_static('menu_get_custom_theme');
// Perform the switch.
$custom_theme = $to ?? $original_theme;
unset($GLOBALS['theme']);
drupal_theme_initialize();
}