1

When a field is required, an asterisk is added inside the <label> tag in a <span> tag. for some reasons, I need to move the <span> tag outside the <label> tag, beside the <label> tag.

How can I achieve this?

1 Answer 1

3

You can achieve this goal via implementing theme_form_element_label.

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

e.g.

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

3
  • 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
    – user23288
    Commented Jun 15, 2015 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
    Commented Jun 15, 2015 at 10:51
  • What HTML structure you want to achieve ?
    – Rupesh
    Commented Jun 15, 2015 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.