Normally in Drupal if a field is required, the Asterisk sign would render inside the <label> tag in a <span>. for some reasons I'd like to move the span outside the <label> and beside the label tag.



You can achieve this goal via implementing theme_form_element_label.

I hope you already know how to implement or override theme.


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

  // If title and required marker are both empty, output no label.
  if ((!isset($element ['#title']) || $element ['#title'] === '') && empty($element ['#required'])) {
    return '';

  // If the element is required, a required marker is appended to the label.
  $required = !empty($element ['#required']) ? theme('form_required_marker', array('element' => $element)) : '';

  $title = filter_xss_admin($element ['#title']);

  $attributes = array();
  // Style the label as class option to display inline with the element.
  if ($element ['#title_display'] == 'after') {
    $attributes ['class'] = 'option';
  // Show label only to screen readers to avoid disruption in visual flows.
  elseif ($element ['#title_display'] == 'invisible') {
    $attributes ['class'] = 'element-invisible';

  if (!empty($element ['#id'])) {
    $attributes ['for'] = $element ['#id'];

  // The leading whitespace helps visually separate fields from inline labels.
  return ' <label' . drupal_attributes($attributes) . '>' . $t('!title', array('!title' => $title,)) . "</label>" . $t('!required', array('!required' => $required)) . "\n";

Put this function on template.php

  • Unfortunately I don't know how to achieve that, I know that I have to use such override but don't have enough knowledge in php – Gus Jun 15 '15 at 10:37
  • 1
    If you know the full answer Ruspesh, do you mind to help him out, I actually would like to know the answer as well. – Sohail Jun 15 '15 at 10:51
  • What HTML structure you want to achieve ? – Rupesh Jun 15 '15 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.