Drupal 7:如何将更改的值从预处理函数获取回渲染函数?



我正在尝试制作一个简单的面板模块。我有一个带有文本字段的表单,我可以输入值,然后该值通过渲染函数打印到 .tpl 文件,例如:

function my_module_panel_render($subtype, $conf, $args, $contexts) {
  $block = new stdClass();
  $block->content = [
    '#theme' => 'my_tpl',
    '#config' => $conf,
  ];
  return $block;
}

然后在 .tpl 上:

<?php print $config['name_field']; ?>

这工作正常。

但我想稍微改变一下值。我了解到我需要一个hook_preprocess_theme()函数,我已经添加了它。

但是,我该如何实际更改值呢?然后如何将更改后的值返回到$conf?

做类似的事情

$conf['name_field'] = $conf['name_field'] . $some_other_stuff;

似乎不起作用。

有人知道我能做什么吗?

好的,我没有放心的是$conf$variables数组的子数组,hook_preprocess_theme()将其作为参数,如我的hook_theme所示:

function my_module_theme() {
  return [
    'my_module' => [
      'template' => 'theme/my_theme',
      'render element' => 'element',
      'variables' => [
        'config' => NULL,
      ],
    ],
  ];
}

因此,我的hook_preprocess_theme()函数现在如下所示:

function my_module_preprocess_my_theme(&$vars) {
  $conf = $vars['config'];
  $vars['config']['name_field'] = $conf['name_field'] . $some_other_stuff;
}

最新更新