You are here

public static function ImageEffectsColor::valueCallback in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Element/ImageEffectsColor.php \Drupal\image_effects\Element\ImageEffectsColor::valueCallback()
  2. 8.2 src/Element/ImageEffectsColor.php \Drupal\image_effects\Element\ImageEffectsColor::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

src/Element/ImageEffectsColor.php, line 39

Class

ImageEffectsColor
Implements a form element to enable capturing color information.

Namespace

Drupal\image_effects\Element

Code

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

    // Make sure element properties are set.
    $element['#allow_null'] = isset($element['#allow_null']) ? $element['#allow_null'] : FALSE;
    $element['#allow_opacity'] = isset($element['#allow_opacity']) ? $element['#allow_opacity'] : FALSE;

    // Normalize returned element values to a RGBA hex value.
    $val = '';
    if ($element['#allow_null'] && !empty($input['container']['transparent'])) {
      return '';
    }
    elseif ($element['#allow_null'] || $element['#allow_opacity']) {
      $val = Unicode::strtoupper($input['container']['hex']);
    }
    else {
      $val = Unicode::strtoupper($input['hex']);
    }
    if ($val[0] != '#') {
      $val = '#' . $val;
    }
    if ($element['#allow_opacity']) {
      $val .= ColorUtility::opacityToAlpha($input['container']['opacity']);
    }
    return $val;
  }
  return '';
}