6

我有一个模块,它使用hook_default_page_manager_pages(). 这很好。但现在我还想为包含的系统提供一个变体node/%node page。但我找不到任何提供变体的钩子。

我的问题是,我无法创建自己的页面来覆盖 node/%node,因为这已经由页面管理器模块本身提供,所以我可以创建接管正常节点视图的页面的唯一方法是提供一个变体(据我了解)。但是我怎样才能以编程方式做到这一点?我可以看到可以导出变体,因此我想也可以通过钩子提供它?

这有可能吗?

4

2 回答 2

9

我找到了我要找的东西。

要在代码中为使用页面管理器构建的页面提供变体,请在您的模块文件中调用 hook_ctools_plugin_api(),让页面管理器知道它应该监听您的模块:

/**
 * Implement hook_ctools_plugin_api().
 *
 * Tells ctools, page manager and panels, that we have a template ready
 */
function mtvideo_ctools_plugin_api($module, $api) {
  // @todo -- this example should explain how to put it in a different file.
  if ($module == 'panels_mini' && $api == 'panels_default') {
    return array('version' => 1);
  }
  if ($module == 'page_manager' && $api == 'pages_default') {
    return array('version' => 1);
  }
}

现在在模块根文件夹中创建一个名为 MODULE_NAME.pages_default.inc 的新文件。在此文件中,您现在可以包含以下函数:

hook_default_page_manager_pages()
/**
 * If you want to put an entire page including its variants in code.
 * With the export module from ctools, you can export your whole page to code.
 * Paste that into this function.
 * (Be aware that the export gives you $page, but you need to return an array,
 * So let the function return array('page name' => $page);
 */

和/或

hook_default_page_manager_handlers()
/**
 * This will provide a variant of an existing page, e.g. a variant of the system
 * page node/%node
 * Again, use the export function in Page Manager to export the needed code,
 * and paste that into the body of this function.
 * The export gives you $handler, but again you want to return an array, so use:
 * return array('handler name' => $handler);
 *
 * Notice, that if you export a complete page, it will include your variants.
 * So this function is only to provide variants of e.g. system pages or pages
 * added by other modules.
 */

我希望有一天这能帮助另一个需要帮助的人:o) 我唯一需要发现的是我的模块如何以编程方式在页面管理器中启用节点/%node 页面。如果有人有线索,请随时与我分享:)

于 2011-10-15T22:08:17.773 回答
0

抱歉,如果我误解了,但我认为有两种方法可以解决这个问题:

首先,您可以实现hook_menu_alter()覆盖路径的页面回调:

function mymodule_menu_alter(&$items) {
  $items['node/%node']['page callback'] = 'mymodule_node_page_callback';
}

function mymodule_node_page_callback($node) {
  // Build up the content and return
}

在这种情况下,您需要确保在system表格中您的模块在weight列中的值高于页面管理器模块(因此稍后将调用您的钩子,并且将拥有最终决定权)。

其次,您可以实现hook_node_view()并完全覆盖内容:

function hook_node_view($node, $view_mode, $langcode) {
  if ($view_mode == 'full') {
    $node->content = array();
    $node->content['title'] = array('#markup' => '<h1>' . $node->title . '</h1>';

    // Build up the rest of the content
  }
}

在这种情况下,您需要将内容构建为渲染数组(参见drupal_render()函数)。

希望有帮助

于 2011-10-14T21:02:25.607 回答