在Drupal中使用主题无线电的正确方法?

时间:2012-01-01 06:04:56

标签: drupal-6 radio-button drupal-theming

我尝试在Drupal模块中使用无线电,覆盖我的主题函数中的以下代码。对于实验,我没有改变以下函数中的任何内容,只是迁移到我的模块中,其名称不同于原始api函数。

function theme_radios($element) {
  $class = 'form-radios';
  if (isset($element['#attributes']['class'])) {
    $class .= ' ' . $element['#attributes']['class'];
  }
  $element['#children'] = '<div class="' . $class . '">' . (!empty($element['#children']) ? $element['#children'] : '') . '</div>';
  if ($element['#title'] || $element['#description']) {
    unset($element['#id']);
    return theme('form_element', $element, $element['#children']);
  }
  else {
    return $element['#children'];
  }
}

主题注册和所有罚款。不幸的是,我得不到任何结果。我只能看到表格标签(Radios的标签)。好吗?

编辑:我成功解决了这个问题。谢谢大家!

2 个答案:

答案 0 :(得分:4)

对于Drupal 7 ,你需要深入挖掘一下来解决这个问题。

首先在 template.php 中创建MY_THEME_form_element(),复制theme_form_element()中的所有代码 - 您可以从api.drupal.org获取该代码。添加条件以检查元素是否是switch($element['#element_display')语句之前的单选按钮。然后创建一个新函数来处理不同的单选按钮。

if ($element['#type'] == 'radio') {
  $variables['rendered_element'] = ' ' . $prefix . $element['#children'] . $suffix . "\n";
  $output .= theme('form_element_label', $variables);
} else {
  switch ($element['#title_display']) {

从上面我们将单选按钮的渲染传递给函数form_element_label(),因此创建MY_THEME_form_element_label()form_element_label()的内容。编辑这个新函数,用以下代码替换return语句:

if (!empty($variables['rendered_element'])) {
    return ' <label' . drupal_attributes($attributes) . '>' . $variables['rendered_element'] . $t('!title !required', array('!title' => $title, '!required' => $required)) . "</label>\n";
} else {
    return ' <label' . drupal_attributes($attributes) . '>' . $t('!title !required', array('!title' => $title, '!required' => $required)) . "</label>\n";
}

这会给你一个单选按钮放在它的标签内,而不是放在它的下面或上面。

最后,您可以加入 theme_radios ,以便为呈现单选按钮列表的方式添加其他格式。

this link.找到了这个答案。它使用复选框描述了一个解决方案。

NB 您也可以在格式化任何元素时应用此功能。

答案 1 :(得分:-1)

问题解决了。我发布了一篇关于如何做的博客文章。任何人在类似问题上需要帮助都可以参考链接。 http://www.encodez.com/blog/how-to-theme-radio-drupal-1.html

注意:此解决方案适用于Drupal 6.