ImageButton.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Core\Render\Element;
  5. /**
  6. * Provides a form element for a submit button with an image.
  7. *
  8. * @FormElement("image_button")
  9. */
  10. class ImageButton extends Submit {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function getInfo() {
  15. $info = parent::getInfo();
  16. unset($info['name']);
  17. return [
  18. '#return_value' => TRUE,
  19. '#has_garbage_value' => TRUE,
  20. '#src' => NULL,
  21. '#theme_wrappers' => ['input__image_button'],
  22. ] + $info;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  28. if ($input !== FALSE) {
  29. if (!empty($input)) {
  30. // If we're dealing with Mozilla or Opera, we're lucky. It will
  31. // return a proper value, and we can get on with things.
  32. return $element['#return_value'];
  33. }
  34. else {
  35. // Unfortunately, in IE we never get back a proper value for THIS
  36. // form element. Instead, we get back two split values: one for the
  37. // X and one for the Y coordinates on which the user clicked the
  38. // button. We'll find this element in the #post data, and search
  39. // in the same spot for its name, with '_x'.
  40. $input = $form_state->getUserInput();
  41. foreach (explode('[', $element['#name']) as $element_name) {
  42. // chop off the ] that may exist.
  43. if (substr($element_name, -1) == ']') {
  44. $element_name = substr($element_name, 0, -1);
  45. }
  46. if (!isset($input[$element_name])) {
  47. if (isset($input[$element_name . '_x'])) {
  48. return $element['#return_value'];
  49. }
  50. return NULL;
  51. }
  52. $input = $input[$element_name];
  53. }
  54. return $element['#return_value'];
  55. }
  56. }
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public static function preRenderButton($element) {
  62. $element['#attributes']['type'] = 'image';
  63. Element::setAttributes($element, ['id', 'name', 'value']);
  64. $element['#attributes']['src'] = file_url_transform_relative(file_create_url($element['#src']));
  65. if (!empty($element['#title'])) {
  66. $element['#attributes']['alt'] = $element['#title'];
  67. $element['#attributes']['title'] = $element['#title'];
  68. }
  69. $element['#attributes']['class'][] = 'image-button';
  70. if (!empty($element['#button_type'])) {
  71. $element['#attributes']['class'][] = 'image-button--' . $element['#button_type'];
  72. }
  73. $element['#attributes']['class'][] = 'js-form-submit';
  74. $element['#attributes']['class'][] = 'form-submit';
  75. if (!empty($element['#attributes']['disabled'])) {
  76. $element['#attributes']['class'][] = 'is-disabled';
  77. }
  78. return $element;
  79. }
  80. }