在 Drupal 中以收音机为主题的正确方法?

Blu*_*ird 1 radio-button drupal-6 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'];
  }
}
Run Code Online (Sandbox Code Playgroud)

主题注册一切正常。不幸的是,我没有得到任何结果。我只能看到表单标签(Radios 的标签)。请问有什么想法吗?

编辑:我设法解决了它。谢谢大家!

Dav*_*tey 5

对于 Drupal 7,您必须深入挖掘才能解决此问题。

首先MY_THEME_form_element()在你的template.php 中创建,复制所有的代码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']) {
Run Code Online (Sandbox Code Playgroud)

从上面我们将单选按钮的渲染传递给函数form_element_label()so createMY_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";
}
Run Code Online (Sandbox Code Playgroud)

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

最后,您可以连接到theme_radios以向呈现单选按钮列表的方式添加其他格式。

这个答案是在这个链接上找到的它描述了使用复选框的解决方案。

注意您也可以在格式化任何元素时应用它。