有人知道如何在编辑节点后使用默认主题以编程方式呈现来自模块的视图吗?
我基本上是在尝试创建视图的静态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';
}发布于 2013-12-09 17:11:30
我在找类似的东西。我发现了一些代码(请参阅修补程序#3 https://drupal.org/node/1813350),但是它无助于我们的短代码控制模块的实现。希望它对你有用,或者帮助你朝着正确的方向前进。
这是我们从修补程序派生出来的尝试:
$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();发布于 2019-05-29 10:15:18
下面是一些基于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();
}https://stackoverflow.com/questions/19560360
复制相似问题