3 namespace Drupal\Core\Render\Element;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
9 * Provides a form element for a submit button with an image.
11 * @FormElement("image_button")
13 class ImageButton extends Submit {
18 public function getInfo() {
19 $info = parent::getInfo();
23 '#return_value' => TRUE,
24 '#has_garbage_value' => TRUE,
26 '#theme_wrappers' => ['input__image_button'],
33 public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
34 if ($input !== FALSE) {
36 // If we're dealing with Mozilla or Opera, we're lucky. It will
37 // return a proper value, and we can get on with things.
38 return $element['#return_value'];
41 // Unfortunately, in IE we never get back a proper value for THIS
42 // form element. Instead, we get back two split values: one for the
43 // X and one for the Y coordinates on which the user clicked the
44 // button. We'll find this element in the #post data, and search
45 // in the same spot for its name, with '_x'.
46 $input = $form_state->getUserInput();
47 foreach (explode('[', $element['#name']) as $element_name) {
48 // chop off the ] that may exist.
49 if (substr($element_name, -1) == ']') {
50 $element_name = substr($element_name, 0, -1);
53 if (!isset($input[$element_name])) {
54 if (isset($input[$element_name . '_x'])) {
55 return $element['#return_value'];
59 $input = $input[$element_name];
61 return $element['#return_value'];
69 public static function preRenderButton($element) {
70 $element['#attributes']['type'] = 'image';
71 Element::setAttributes($element, ['id', 'name', 'value']);
73 $element['#attributes']['src'] = file_url_transform_relative(file_create_url($element['#src']));
74 if (!empty($element['#title'])) {
75 $element['#attributes']['alt'] = $element['#title'];
76 $element['#attributes']['title'] = $element['#title'];
79 $element['#attributes']['class'][] = 'image-button';
80 if (!empty($element['#button_type'])) {
81 $element['#attributes']['class'][] = 'image-button--' . $element['#button_type'];
83 $element['#attributes']['class'][] = 'js-form-submit';
84 $element['#attributes']['class'][] = 'form-submit';
86 if (!empty($element['#attributes']['disabled'])) {
87 $element['#attributes']['class'][] = 'is-disabled';