<input> submit element as <button> when value contains HTML

Public

<input> submit element as <button> when value contains HTML

Get raw version
php
  1. <?php
  2.  
  3. /**
  4.  * Returns HTML for a button form element when the value includes html.
  5.  *
  6.  * Overrides theme_button().
  7.  *
  8.  * @param $variables
  9.  * An associative array containing:
  10.  * - element: An associative array containing the properties of the element.
  11.  * Properties used: #attributes, #button_type, #name, #value.
  12.  *
  13.  * @return string
  14.  * Themed output string.
  15.  */
  16. function THEMENAME_button($variables) {
  17. $element = &$variables['element'];
  18. $label = $element['#value'];
  19.  
  20. // Only override the button styling if the value contains
  21. if(preg_match("/<[^<]+>/", $label, $m) == 0) {
  22. return theme_button($variables);
  23. }
  24.  
  25. $element['#attributes']['type'] = 'submit';
  26. element_set_attributes($element, array('id', 'name', 'value', 'type'));
  27.  
  28. $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
  29. if (!empty($element['#attributes']['disabled'])) {
  30. $element['#attributes']['class'][] = 'form-button-disabled';
  31. }
  32.  
  33. // Add in the button type class.
  34. $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
  35.  
  36. // This line break adds inherent margin between multiple buttons.
  37. return '<button' . drupal_attributes($element['#attributes']) . '>' . $label . "</button>\n";
  38. }

Comments

VVS's picture

Ex. only for #type = 'button':

  1. /**
  2.  * Override of theme_button().
  3.  *
  4.  * Render the button element as a button and the submit element as an input element.
  5.  */
  6. function mytheme_button($variables) {
  7. $element = $variables['element'];
  8. $element['#attributes']['type'] = 'submit';
  9.  
  10. element_set_attributes($element, array('id', 'name', 'value'));
  11.  
  12. $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
  13. if (!empty($element['#attributes']['disabled'])) {
  14. $element['#attributes']['class'][] = 'form-button-disabled';
  15. }
  16.  
  17. if (isset($element['#buttontype']) && $element['#buttontype'] == 'button') {
  18. $value = $element['#value'];
  19. unset($element['#attributes']['value']);
  20. return '<button' . drupal_attributes($element['#attributes']) . '>' . $value . '</button>';
  21. }
  22. else {
  23. return '<input' . drupal_attributes($element['#attributes']) . ' />';
  24. }
  25. }
  26. <php>