3

有谁知道在编辑节点后使用默认主题以编程方式从模块渲染视图的方法?

我基本上是在尝试创建视图的静态 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);
}

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

我尝试了几件事无济于事:

变量集

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';
}
4

2 回答 2

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();
于 2013-12-09T17:11:30.280 回答
0

这是一些基于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();
}
于 2019-05-29T10:15:18.297 回答