You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Drupal 7中向Panel Pane输出数据?

解决Panel Pane无法传递数据到.tpl文件的问题

看起来你已经迈出了第一步,但要让数据成功传递到模板,还需要完善几个关键环节——毕竟CTools/Panel的自定义Pane需要完整的插件定义、变量传递和路径配置才行。我来一步步帮你排查和修复:

1. 确保你有完整的Pane插件定义文件

hook_ctools_plugin_directory只是告诉CTools去哪里找插件,但你还需要在指定的目录下创建Pane的插件定义文件(比如plugins/content_types/message_pane.inc)。这个文件要定义Pane的基本信息、渲染回调,以及关联模板:

<?php
/**
 * @file
 * Custom Panel Pane plugin definition.
 */

$plugin = array(
  'title' => t('Custom Message Pane'),
  'description' => t('Displays a configurable message.'),
  'single' => TRUE, // 标记为单个Pane类型
  'content_types' => array('message_pane'),
  'render callback' => 'message_pane_render', // 核心:处理数据并传递给模板的函数
  'template' => 'message-pane', // 关联你的模板文件(不带.tpl.php后缀)
  'edit form' => 'message_pane_edit_form', // 可选:添加编辑表单让用户自定义消息
  'category' => t('Custom'), // Pane在Panel界面中的分类
);

/**
 * 渲染回调:准备要传递给模板的数据
 */
function message_pane_render($subtype, $conf, $args, $contexts) {
  $block = new stdClass();

  // 这里就是你要传递给模板的变量!
  // 如果有编辑表单,就用$conf里的用户输入;否则直接写死测试
  $block->content['message'] = isset($conf['message']) ? $conf['message'] : t('Hello from your custom pane!');

  return $block;
}

/**
 * 可选:添加Pane的编辑表单(让用户在Panel配置里修改消息)
 */
function message_pane_edit_form($form, &$form_state) {
  $conf = $form_state['conf'];

  $form['message'] = array(
    '#type' => 'textfield',
    '#title' => t('Display Message'),
    '#default_value' => $conf['message'] ?? '',
    '#required' => TRUE,
  );

  return $form;
}

function message_pane_edit_form_submit($form, &$form_state) {
  $form_state['conf']['message'] = $form_state['values']['message'];
}

2. 修正你的模板文件

确保你的模板文件(比如message-pane.tpl.php)和插件定义里的template名称一致,并且变量名和渲染回调里的对应:

<?php
/**
 * @file
 * Template for custom message panel pane.
 */
?>
<div class="custom-message-pane">
  <?php print $message; ?>
</div>

3. 确认hook_ctools_plugin_directory路径正确

你的.module里的这个钩子需要准确返回插件所在的目录,比如如果插件文件在modules/message/plugins/content_types/下,钩子应该写成:

/**
 * Implements hook_ctools_plugin_directory().
 */
function message_ctools_plugin_directory($owner, $plugin_type) {
  // 针对CTools的content_types(也就是Panel Pane)
  if ($owner === 'ctools' && $plugin_type === 'content_types') {
    return 'plugins/' . $plugin_type;
  }
  // 保留你原来的styles相关判断(如果需要的话)
  if ($owner === 'panels' && $plugin_type === 'styles') {
    return 'plugins/' . $plugin_type;
  }
}

4. 可选但推荐:注册主题钩子

为了确保Drupal能正确识别你的模板,最好在.module里添加hook_theme:

/**
 * Implements hook_theme().
 */
function message_theme($existing, $type, $theme, $path) {
  return array(
    'message_pane' => array(
      'template' => 'message-pane',
      'path' => drupal_get_path('module', 'message') . '/templates', // 把模板放到templates文件夹更规范
      'variables' => array('message' => NULL), // 声明模板接受的变量
    ),
  );
}

5. 最后一步:清除Drupal缓存!

这一步绝对不能忘——Drupal不会自动识别新的插件和模板,必须通过配置页面清除所有缓存,或者用Drush命令:drush cache-clear all

做完这些步骤后,回到Panel的布局编辑界面,添加你的Custom Message Pane,应该就能看到模板输出的消息了!如果用了编辑表单,还能在Pane的配置里修改消息内容。

内容的提问来源于stack exchange,提问作者MeltingDog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:38:53