0

I'm trying to override the Drupal Login form. I have been able to unset and modify pieces of it, but the last thing I am struggling with is the name attribute on the submit button. Currently, the button looks like this:

 <button class="btn btn-default form-submit" id="edit-submit" name="op" value="Log in" type="submit">Log in</button>

I am trying to get rid of the name attribute, but I don't see it listed as a array element when I dpm($form). How do I alter this

EDIT: I almost figured it out. I found that the below can be used to alter the name attribute, but I cant remove it, even if I set it equal to NULL. When set to null, it defaults to 'op':

$form['actions']['submit']['#attributes']['name'] = 'submit';
0

It's possible that "op" is a default that gets applied if there's nothing already there.

Try:

$form['#submit']['#attributes']['name'] = 'thing-you-want-to-call-it';
  • I tried that, but it didn't change the name attribute value. – user1015214 Dec 4 '14 at 14:18
  • I was able to target submit a little bit differently than you sugggested, but still not the full solution. See edit above. – user1015214 Dec 4 '14 at 14:28
0

Ok, I finally found a solution, but if someone has a better one, please let me know.

I found that I wasn't able to modify the name attribute from the hook form alter, so I went higher up. I first tried altering the button with a preprocess function, which didn't work. I was only able to do it by overriding the actual theme_button. Code below (note: I'm using a bootstrap subtheme):

/**
* Overrides theme_button().
*/
function mytheme_button($variables) {
  $element = $variables['element'];
  $label = $element['#value'];
  element_set_attributes($element, array('id', 'name', 'value', 'type'));

  // If a button type class isn't present then add in default.
  $button_classes = array(
    'btn-default',
    'btn-primary',
    'btn-success',
    'btn-info',
    'btn-warning',
    'btn-danger',
    'btn-link',
  );
  $class_intersection = array_intersect($button_classes, $element['#attributes']['class']);
  if (empty($class_intersection)) {
    $element['#attributes']['class'][] = 'btn-default';
  }

  // Add in the button type class.
  $element['#attributes']['class'][] = 'form-' . $element['#button_type'];

  if($element['#type'] === 'submit') {
    unset($element['#attributes']['name']);
  }
  dpm($element);
  // This line break adds inherent margin between multiple buttons.
  return '<button' . drupal_attributes($element['#attributes']) . '>' . $label ."</button>\n";
}

function mytheme_preprocess_button($variables) {
    if($variables['element']['#type'] === 'submit') {
      unset($variables['element']['#name']);
 }
 dpm($variables);  
}

I found that I was able to remove the name attribute with this code in the mytheme_button

unset($element['#attributes']['name']);

but from inside the mytheme_preprocess_button, I was able to remove the #name attribute from the array with this

unset($variables['element']['#name']);

but it didn't actually remove the name from the form. In the preprocess, 'name' is not a sub element under '#attributes'.

Does this make sense? I would think the most Drupal solution would be to use the preprocess, if I can.

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.