Drupal 7 - Поместите описания полей над полем - PullRequest
2 голосов
/ 12 февраля 2011

По умолчанию в 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 ]

6 голосов
/ 05 апреля 2011

У меня возникла та же проблема, и я выполнил ее, добавив ее в файл 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;
}

Не забудьте очистить кеш!

2 голосов
/ 05 ноября 2013

https://drupal.org/project/label_help также должны помочь. Надеюсь, это поможет

0 голосов
/ 08 марта 2013

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;

    }
0 голосов
/ 01 марта 2011

Вы можете сделать переопределение темы для определенного поля, которое вы хотите изменить, или более общее переопределение для всех полей. Читать это:

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

Вам вообще не нужно связываться с template.php.

...