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

发布于 2024-10-17 01:06:58 字数 1257 浏览 2 评论 0原文

默认情况下,在 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;
}

By default in Drupal 7, field descriptions appear below the field. Is there anyway to move them above the field?

In Drupal 6, you could paste the following code in template.php to move the descriptions. However, the code does not work in 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枯寂 2024-10-24 01:06:58

我遇到了同样的问题,并通过将其添加到主题的 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;
}

不要忘记清除缓存!

I had the same problem and accomplished this by adding it to my theme's template.php file.

/**
* 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;
}

Don't forget to clear your cache!

各空 2024-10-24 01:06:58

https://drupal.org/project/label_help 也应该可以解决问题。希望有帮助

https://drupal.org/project/label_help should also do the trick. Hope that helps

没企图 2024-10-24 01:06:58

您可以对要更改的特定字段进行主题覆盖,或者对所有字段进行更通用的覆盖。阅读此内容:

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

您根本不需要搞乱 template.php 来执行此操作。

You can do a theme override for the specific field you want to change or a more general override for all fields. Read this:

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

You shouldn't have to mess with template.php at all to do this.

一场春暖 2024-10-24 01:06:58

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;

    }

Rumblewand's answer, with a conditional that prevents radio/checkboxes from also being thrown into a div above the input. (May be more efficient ways to do this.)

    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;

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文