欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal form text input desction order 我可以移动标签和字段之间的表单字段的描述吗?改变表单描述 input 等的顺序 有大用

以下四步是我做的 有大用
先是
1) $export = kprint($form['field'],TRUE);
print 
$export;
看看它的theme, 比如一个可能的值为
 content_multiple_values
2) form_alter 一下    即  改写 (超越)一下theme
$form['field']['#theme'] = 'custom_field_fangshi_values';
然后 我们 进行主题注册 

3) function shipingzhongcustom_theme(){
   return array(
      'custom_field_fangshi_values' => array(
         'arguments' => array('element' => NULL),
      ),
   );
}
4) 定义 custom_field_fangshi_values 函数 ,也就是把默认的 theme_content_multiple_values 这个函数复制过来修改就可以了 


我有一个表单与输入字段,如这一个:
'sex' => array(
    '#type' => 'radios',
    '#title' => t('Sex'),
    '#options' => array('m' => t('M'), 'f' => t('F')),
    '#description' => t("We use this to write Mr. or Mrs. in the letters we send you.")
)

默认情况下,首先打印标签(“Sex”),然后是单选按钮,然后是描述。我想打印出标签,紧接着的描述,并使单选按钮出现最后。我该如何实现呢?我知道如何加载样式表,但在这种情况下,它将是很好的html以正确的顺序出现:<label /><div class="description" /><input />而不是<label /><input /><div class="description" />

编辑:Anil Sagar提出的解决方案工作,但是引入了一个错误。事实上,我使用他的解决方案,它阻止我给一个复选框字段的描述。换句话说,如果我写

$form['myCheckbox'] = array(
  '#type' => 'checkbox',
  '#title' => t('My checkbox'),
  '#description' => t("My description")
);

该描述从不使它到HTML。有谁知道如何解决这个问题?
 

正确答案 
您需要重写
主题
template.php中的theme_form_element以更改表单字段描述的顺序。请参阅下面的代码,它将更改所有字段的顺序。

如果你只想改变一个字段,然后通过检查$ element数组只为你的字段添加一个if条件!

function yourtheme_form_element($variables) {
  $element = &$variables['element'];
  // This is also used in the installer, pre-database setup.
  $t = get_t();

  // This function is invoked as theme wrapper, but the rendered form element
  // may not necessarily have been processed by form_builder().
  $element += array(
    '#title_display' => 'before',
  );

  // Add element #id for #type 'item'.
  if (isset($element['#markup']) && !empty($element['#id'])) {
    $attributes['id'] = $element['#id'];
  }
  // Add element's #type and #name as class to aid with JS/CSS selectors.
  $attributes['class'] = array('form-item');
  if (!empty($element['#type'])) {
    $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  }
  if (!empty($element['#name'])) {
    $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  }
  // Add a class for disabled elements to facilitate cross-browser styling.
  if (!empty($element['#attributes']['disabled'])) {
    $attributes['class'][] = 'form-disabled';
  }
  $output = '<div' . drupal_attributes($attributes) . '>' . "\n";

  // If #title is not set, we don't display any label or required marker.
  if (!isset($element['#title'])) {
    $element['#title_display'] = 'none';
  }
  $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
   $element_description = "";
  if (!empty($element['#description'])) {
    $element_description = '<div class="description">' . $element['#description'] . "</div>\n";
  }
  switch ($element['#title_display']) {
    case 'before':
    case 'invisible':
      $output .= ' ' . theme('form_element_label', $variables);
      $output .= $element_description;
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;

    case 'after':
      $output .= ' ' . $prefix . $element['#children'] . $suffix;
      $output .= ' ' . theme('form_element_label', $variables) . "\n";

      break;

    case 'none':
    case 'attribute':
      // Output no label and no required marker, only the children.
      $output .= $element_description;
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;
  }
  $output .= "</div>\n";

  return $output;
}

其他方式和简单的方法来解决这个问题是使用jQuery来改变位置。将帮助你!

分享改进这个答案

   
显然,这个方法(覆盖theme_form_element)有一个讨厌的副作用。我不能再添加对复选框的描述($form['a'] = array('#type'=>'checkbox','#description'=>'hello');:描述从来没有使它到html)。评论时间70年01月01日原作者 :  Shawn
   
是否有人能够重现在我以前的评论中提到的问题? 评论时间70 年01月01日原作者:  Shawn
   
好吧,我想出来了。这个问题是通过$output .= $element_description;在结束时添加case 'after':-  Shawn在 10月22日在15:31
   
这个代码还有另一个问题,详细这里:drupal.stackexchange.com/questions/48918 / ...评论时间70 年01月01日原作者Shawn 评论时间70 年01月01日原作者: 
 
 

您可以使用label_help模块来解决这个问题,这是专门为此目的而设计的。

在字段设置中,它将引入一个称为“标签帮助消息”的字段,即“帮助文本插入到标签下方和输入表单元素上方”。这样,如果您想要在字段上方,下方或两者都有说明,则可以为每个字段选择。

分享改进这个答案
 

我发现这个非常有帮助的博客文章来解决这个问题:http : //www.whenwhowhere.com/tech-blog/move-description-field-below-label-drupal-7-form

基本上,在hook_form_alter,你把标题和描述在#prefix的元素,然后取消设置标题和描述,所以他们不会出现两次...我试过它,它的工作完美。

分享改进这个答案
 
   
所有期望标题和描述的模块,并尝试使用它们的形式改变,将开始失败。这可以和应该在主题级别,而不是你在这里张贴的方式。 -  Mołot 11月20日在'14 21:21
   
好吧然后就隐藏标题和描述用CSS然后



I have a form with input fields such as this one:

'sex' => array(
    '#type' => 'radios',
    '#title' => t('Sex'),
    '#options' => array('m' => t('M'), 'f' => t('F')),
    '#description' => t("We use this to write Mr. or Mrs. in the letters we send you.")
)

By default, this prints the label first ('Sex'), then the radio buttons, and then the description. I would like to print out the label followed immediately by the description and have the radio buttons appear last. How can I achieve this? I know how to load a stylesheet, but in this case it would be nice for the html to appear in the correct order: <label /><div class="description" /><input /> rather than <label /><input /><div class="description" />

Edit: The solution proposed by Anil Sagar works, but introduces a bug. Indeed, what I use his solution, it prevents me from giving a description to a checkbox field. In other words, if I write

$form['myCheckbox'] = array(
  '#type' => 'checkbox',
  '#title' => t('My checkbox'),
  '#description' => t("My description")
);

that description never makes it to the HTML. Does anyone know how to fix this?

shareimprove this question
 

3 Answers 正确答案 

You need to override theme_form_element in your theme template.php to change the order of the form field description. See below code which will change order for all fields.

If you would like to change for only one field, then add a if condition by inspecting $element array only for your field !

function yourtheme_form_element($variables) {
  $element = &$variables['element'];
  // This is also used in the installer, pre-database setup.
  $t = get_t();

  // This function is invoked as theme wrapper, but the rendered form element
  // may not necessarily have been processed by form_builder().
  $element += array(
    '#title_display' => 'before',
  );

  // Add element #id for #type 'item'.
  if (isset($element['#markup']) && !empty($element['#id'])) {
    $attributes['id'] = $element['#id'];
  }
  // Add element's #type and #name as class to aid with JS/CSS selectors.
  $attributes['class'] = array('form-item');
  if (!empty($element['#type'])) {
    $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  }
  if (!empty($element['#name'])) {
    $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  }
  // Add a class for disabled elements to facilitate cross-browser styling.
  if (!empty($element['#attributes']['disabled'])) {
    $attributes['class'][] = 'form-disabled';
  }
  $output = '<div' . drupal_attributes($attributes) . '>' . "\n";

  // If #title is not set, we don't display any label or required marker.
  if (!isset($element['#title'])) {
    $element['#title_display'] = 'none';
  }
  $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
   $element_description = "";
  if (!empty($element['#description'])) {
    $element_description = '<div class="description">' . $element['#description'] . "</div>\n";
  }
  switch ($element['#title_display']) {
    case 'before':
    case 'invisible':
      $output .= ' ' . theme('form_element_label', $variables);
      $output .= $element_description;
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;

    case 'after':
      $output .= ' ' . $prefix . $element['#children'] . $suffix;
      $output .= ' ' . theme('form_element_label', $variables) . "\n";

      break;

    case 'none':
    case 'attribute':
      // Output no label and no required marker, only the children.
      $output .= $element_description;
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;
  }
  $output .= "</div>\n";

  return $output;
}

Other way & simple way to solve this problem is use jQuery to change the position. This will help you !

shareimprove this answer
 
   
Apparently, this method (overriding theme_form_element) has a nasty side effect. I can no longer add a description on a checkbox ($form['a'] = array('#type'=>'checkbox','#description'=>'hello');: The description never makes it to the html). – Shawn Oct 16 '12 at 21:22
   
Is anyone able to reproduce the problem mentionned in my previous comment? – Shawn Oct 19 '12 at 17:31
   
Ok, I figured it out. The problem is avoided by adding $output .= $element_description; at the end of case 'after': – Shawn Oct 22 '12 at 15:31
   
There is another problem with this code, detailed here: drupal.stackexchange.com/questions/48918/… – Shawn Oct 26 '12 at 18:40

You can solve this with the label_help module, which is made specifically for this purpose.

In the field settings, it will introduce one more field called "Label help message", which is "Help text to insert below the label and above the input form element." This way, you can choose for each field if you want a description above the field, below it, or both.

shareimprove this answer
 

I found this very helpful blog post to fix this problem : http://www.whenwhowhere.com/tech-blog/move-description-field-below-label-drupal-7-form

Basically, in hook_form_alter, you put the title and the description in the #prefix of the element, then unset the title and the description so they don't appear twice ... I tried it and it works perfectly.

shareimprove this answer
 
   
And all modules that expect title and description to be there, and try to use them in their form alters, will start to fail. This can and should be done at theme level, not the way you are posting here. – Mołot Nov 20 '14 at 21:21
   

   
显然,这个方法(覆盖theme_form_element)有一个讨厌的副作用。我不能再添加对复选框的描述($form['a'] = array('#type'=>'checkbox','#description'=>'hello');:描述从来没有使它到html)。评论时间70年01月01日原作者 :  Shawn
   
是否有人能够重现在我以前的评论中提到的问题? 评论时间70 年01月01日原作者:  Shawn
   
好吧,我想出来了。这个问题是通过$output .= $element_description;在结束时添加case 'after':-  Shawn在 10月22日在15:31
   
这个代码还有另一个问题,详细这里:drupal.stackexchange.com/questions/48918 / ...评论时间70 年01月01日原作者Shawn 评论时间70 年01月01日原作者: 
普通分类: