You are here

public static function ImageButton::valueCallback in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/Element/ImageButton.php \Drupal\Core\Render\Element\ImageButton::valueCallback()
  2. 9 core/lib/Drupal/Core/Render/Element/ImageButton.php \Drupal\Core\Render\Element\ImageButton::valueCallback()

Determines how user input is mapped to an element's #value property.

Parameters

array $element: An associative array containing the properties of the element.

mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

mixed The value to assign to the element.

Overrides FormElement::valueCallback

File

core/lib/Drupal/Core/Render/Element/ImageButton.php, line 33

Class

ImageButton
Provides a form element for a submit button with an image.

Namespace

Drupal\Core\Render\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  if ($input !== FALSE) {
    if (!empty($input)) {

      // If we're dealing with Mozilla or Opera, we're lucky. It will
      // return a proper value, and we can get on with things.
      return $element['#return_value'];
    }
    else {

      // Unfortunately, in IE we never get back a proper value for THIS
      // form element. Instead, we get back two split values: one for the
      // X and one for the Y coordinates on which the user clicked the
      // button. We'll find this element in the #post data, and search
      // in the same spot for its name, with '_x'.
      $input = $form_state
        ->getUserInput();
      foreach (explode('[', $element['#name']) as $element_name) {

        // chop off the ] that may exist.
        if (substr($element_name, -1) == ']') {
          $element_name = substr($element_name, 0, -1);
        }
        if (!isset($input[$element_name])) {
          if (isset($input[$element_name . '_x'])) {
            return $element['#return_value'];
          }
          return NULL;
        }
        $input = $input[$element_name];
      }
      return $element['#return_value'];
    }
  }
}