Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Render / Element / ImageButton.php
1 <?php
2
3 namespace Drupal\Core\Render\Element;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
7
8 /**
9  * Provides a form element for a submit button with an image.
10  *
11  * @FormElement("image_button")
12  */
13 class ImageButton extends Submit {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getInfo() {
19     $info = parent::getInfo();
20     unset($info['name']);
21
22     return [
23       '#return_value' => TRUE,
24       '#has_garbage_value' => TRUE,
25       '#src' => NULL,
26       '#theme_wrappers' => ['input__image_button'],
27     ] + $info;
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
34     if ($input !== FALSE) {
35       if (!empty($input)) {
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'];
39       }
40       else {
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);
51           }
52
53           if (!isset($input[$element_name])) {
54             if (isset($input[$element_name . '_x'])) {
55               return $element['#return_value'];
56             }
57             return NULL;
58           }
59           $input = $input[$element_name];
60         }
61         return $element['#return_value'];
62       }
63     }
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public static function preRenderButton($element) {
70     $element['#attributes']['type'] = 'image';
71     Element::setAttributes($element, ['id', 'name', 'value']);
72
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'];
77     }
78
79     $element['#attributes']['class'][] = 'image-button';
80     if (!empty($element['#button_type'])) {
81       $element['#attributes']['class'][] = 'image-button--' . $element['#button_type'];
82     }
83     $element['#attributes']['class'][] = 'js-form-submit';
84     $element['#attributes']['class'][] = 'form-submit';
85
86     if (!empty($element['#attributes']['disabled'])) {
87       $element['#attributes']['class'][] = 'is-disabled';
88     }
89
90     return $element;
91   }
92
93 }