Drupal 7 - 将字段描述放在字段上方

时间:2011-02-12 16:14:28

标签: drupal drupal-7 drupal-theming

默认情况下,在Drupal 7中,字段描述显示在字段下方。无论如何将它们移到场地上方?

在Drupal 6中,您可以将以下代码粘贴到template.php中以移动描述。但是,代码在Drupal 7中不起作用:

/**
 * Place CCK Options above field .
 */

function ThemeNAME_form_element($element, $value) {
  $output  = ' <div class="form-item"';
  if(!empty($element['#id'])) {
    $output .= ' id="'. $element['#id'] .'-wrapper"';
  }  
  $output .= ">\n";
  $required = !empty($element['#required']) ? '<span class="form-required" title="'.t('This field is required.').'">*</span>' : '';

  if (!empty($element['#title'])) {
    $title = $element['#title'];
    if (!empty($element['#id'])) {
      $output .= ' <label for="'. $element['#id'] .'">'. t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label> \n";
    }
    else {
      $output .= ' <label>'. t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
    }   
  }

  if (!empty($element['#description'])) {
    $output .= ' <div class="description">' . $element['#description'] ."</div> \n";
  }

  $output .= " $value\n";
  $output .= " </div> \n";
  return $output;
}

4 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,并将其添加到我主题的template.php文件中。

/**
* Replacement for theme_webform_element() to enable descriptions to come BEFORE the field to be filled out.
*/
function danland_webform_element($variables) {
  $element = $variables['element'];
  $value = $variables['element']['#children'];

  $wrapper_classes = array(
    'form-item',
  );
  $output = '<div class="' . implode(' ', $wrapper_classes) . '" id="' . $element['#id'] . '-wrapper">' . "\n";
  $required = !empty($element['#required']) ? '<span class="form-required" title="' . t('This field is required.') . '">*</span>' : '';

  if (!empty($element['#title'])) {
    $title = $element['#title'];
    $output .= ' <label for="' . $element['#id'] . '">' . t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) . "</label>\n";
  }

  if (!empty($element['#description'])) {
    $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
  }

  $output .= '<div id="' . $element['#id'] . '">' . $value . '</div>' . "\n";

  $output .= "</div>\n";

  return $output;
}

别忘了清除缓存!

答案 1 :(得分:2)

https://drupal.org/project/label_help也应该这样做。希望有所帮助

答案 2 :(得分:0)

您可以为要更改的特定字段执行主题覆盖,也可以对所有字段执行更一般的覆盖。阅读本文:

http://api.drupal.org/api/drupal/modules--field--field.module/function/theme_field/7

你根本不应该乱用template.php来做这件事。

答案 3 :(得分:0)

Rumblewand的答案,条件是防止收音机/复选框被抛入输入上方的div。 (可能是更有效的方法。)

    function theme_form_element($variables) {

      $element = $variables['element'];
      $value = $variables['element']['#children'];

      $wrapper_classes = array(
        'form-item'
      );

      $output = '<div class="' . implode(' ', $wrapper_classes) . '" id="' . $element['#id'] . '-wrapper">' . "\n";

      $required = !empty($element['#required']) ? '<span class="form-required" title="' . t('This field is required.') . '">*</span>' : '';

        //Separate treatment for radio buttons & checkboxes
        if (($element['#type'] == 'radio') || ($element['#type'] == 'checkbox')) {
          //vs outputting input in its own div separate from label
          $output .=  $value . "\n";  

          if (!empty($element['#description'])) {
            $output .= '<span class="description">' . $element['#description'] . "</span>\n";
          }

          if (!empty($element['#title'])) {
            $title = $element['#title'];
            $output .= '<label class="option" for="' . $element['#id'] . '">' . t('!title !required', array('!title' => filter_xss_admin($title), '!required' => $required)) . "</label>\n";
          }

      } else {

          if (!empty($element['#title'])) {
            $title = $element['#title'];
            $output .= ' <label for="' . $element['#id'] . '">' . t('!title !required', array('!title' => filter_xss_admin($title), '!required' => $required)) . "</label>\n";
          }

          if (!empty($element['#description'])) {
            $output .= '<div class="description">' . $element['#description'] . "</div>\n";
          }

          $output .= '<div id="' . $element['#id'] . '">' . $value . '</div>' . "\n";  

      }

      $output .= "</div>\n";

      return $output;

    }