Is there a configuration for removing the asterisks from required fields? Is it a style of some sort? If not, does anybody know which module/file/function in the core I can hack to remove the asterisks from all the required fields?

Comments

erdemkose’s picture

I think the simplest way to remove asterisks is making form-required CSS class hidden. Drupal wraps asterisks in a span tag. So adding

span.form-required {
  display: none;
}

to your theme's CSS file will remove asterisks visually.

:: I am not sure if display: none; is right, I am not so familiar with CSS.

--------------------------------------------------------------
http://erdemkose.com/
Drupal Turkiye

morganhua’s picture

visibility: hidden;

also works and keeps the spacing, but since the asterisk is always at the end of the field, "display: none" worked fine.

And no real coding required. Thanks.

NitroLiq’s picture

Oddly enough, when I tried this, visibility:hidden worked but display: none didn't. I had to add !important to get it to work.

span.form-required {
   display:none !important;
}
hgmichna’s picture

I'm pretty sure that there is no web admin setting for the red asterisks. They are a widely used de-facto standard, so my recommendation would be to leave them in place. Many users will recognize them for what they are.

If you still want to remove them, you may have to delve into PHP programming. Or perhaps there is a way to make them invisible in one of the CSS sheets.

Sorry for not having a definite answer. But I just saw that somebody posted a more concrete proposal while I wrote this.

Hans-Georg

bdragon’s picture

alf1230’s picture

What file is this in? Thanks.

keilo’s picture

To remove asterisks from the login form add the following code to your theme's style.css:

#user-login-form span.form-required {
  display: none;
}

This will only affect the login form. To remove asterisks from everywhere use:

span.form-required {
  display: none;
}
aronanda’s picture

Here it is in a D7 module with a specific NODE-TYPE:

drupal_add_css(
    'form#NODE-TYPE-node-form span.form-required { display: none !important; }', 
    array('group' => CSS_THEME, 'type' => 'inline')
);

And here it is for the user form:

drupal_add_css(
    'form#user-login span.form-required { display: none !important; }', 
    array('group' => CSS_THEME, 'type' => 'inline')
);
kkatusic’s picture

It's simple, you can use "theme_form_element" function:


function YOURTHEMENAME_form_element($element, $value) {
  $output  = '<div class="form-item"';
  if (!empty($element['#id'])) {
    $output .= ' id="' . $element['#id'] . '-wrapper"';
  }
  $output .= ">\n";
  
  //REPLACE THIS
  //$required = !empty($element['#required']) ? '<span class="form-required" title="' . t('This field is required.') . '">*</span>' : '';
//WITH THIS
  $required = !empty($element['#required']) ? '' : '';

  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";
    }
  }

  $output .= " $value\n";

  if (!empty($element['#description'])) {
    $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
  }

  $output .= "</div>\n";

  return $output;
}

nevets’s picture

Instead of

$required = !empty($element['#required']) ? '' : '';

I would suggest

$required = '';
iLLin’s picture

For those that want to remove the asterisks from some forms and not others I did something similar.

function THEME_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>' : '';

  // Remove the asterisk if needed.
  if(isset($element['#skip asterisk'])) {
    unset($element['#required']);
  }

  switch ($element['#title_display']) {
    case 'before':
    case 'invisible':
      $output .= ' ' . theme('form_element_label', $variables);
      $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 .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;
  }

  if (!empty($element['#description'])) {
    $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  }

  $output .= "</div>\n";

  return $output;
}

Notice this part

  // Remove the asterisk if needed.
  if(isset($element['#skip asterisk'])) {
    unset($element['#required']);
  }

Then you can simply add in a form alter

function MODULE_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
    case 'user_login_block':
      $form['name']['#skip asterisk'] = TRUE;
      $form['pass']['#skip asterisk'] = TRUE;
      break;
  }
}
nehajn’s picture

Remove Astrik and replace it with (required)on site forms.
function THEME_form_required_marker($variables) {
  // This is also used in the installer, pre-database setup.
  $t = get_t();
  $attributes = array(
    'class' => 'form-required',
    'title' => $t('This field is required.'),
  );
  return '<span' . drupal_attributes($attributes) . '>(required)</span>';
}
iampuma’s picture

For Drupal 7 you can overwrite THEME_form_element in your theme. All classes on the fields stay for styling purposes, Ex. required, error,..

function bizreview_form_element($variables) {
  $element = &$variables['element'];

  // 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>' : '';

  // ALTER: Remove every required asterisk field on the drupal form.
  $element['#required'] = FALSE;

  switch ($element['#title_display']) {
    case 'before':
    case 'invisible':
      $output .= ' ' . theme('form_element_label', $variables);
      $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 .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;
  }

  if (!empty($element['#description'])) {
    $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  }

  $output .= "</div>\n";

  return $output;
}