Color.php

Same filename in this branch
  1. 9 core/modules/color/src/Plugin/migrate/destination/Color.php
  2. 9 core/modules/color/src/Plugin/migrate/source/d7/Color.php
  3. 9 core/lib/Drupal/Component/Utility/Color.php
Same filename in other branches
  1. 8.9.x core/modules/color/src/Plugin/migrate/destination/Color.php
  2. 8.9.x core/modules/color/src/Plugin/migrate/source/d7/Color.php
  3. 8.9.x core/lib/Drupal/Core/Render/Element/Color.php
  4. 8.9.x core/lib/Drupal/Component/Utility/Color.php
  5. 10 core/lib/Drupal/Core/Render/Element/Color.php
  6. 10 core/lib/Drupal/Component/Utility/Color.php
  7. 11.x core/lib/Drupal/Core/Render/Element/Color.php
  8. 11.x core/lib/Drupal/Component/Utility/Color.php

Namespace

Drupal\Core\Render\Element

File

core/lib/Drupal/Core/Render/Element/Color.php

View source
<?php

namespace Drupal\Core\Render\Element;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Component\Utility\Color as ColorUtility;

/**
 * Provides a form element for choosing a color.
 *
 * Properties:
 * - #default_value: Default value, in a format like #ffffff.
 *
 * Example usage:
 * @code
 * $form['color'] = array(
 *   '#type' => 'color',
 *   '#title' => $this->t('Color'),
 *   '#default_value' => '#ffffff',
 * );
 * @endcode
 *
 * @FormElement("color")
 */
class Color extends FormElement {
    
    /**
     * {@inheritdoc}
     */
    public function getInfo() {
        $class = static::class;
        return [
            '#input' => TRUE,
            '#process' => [
                [
                    $class,
                    'processAjaxForm',
                ],
            ],
            '#element_validate' => [
                [
                    $class,
                    'validateColor',
                ],
            ],
            '#pre_render' => [
                [
                    $class,
                    'preRenderColor',
                ],
            ],
            '#theme' => 'input__color',
            '#theme_wrappers' => [
                'form_element',
            ],
        ];
    }
    
    /**
     * Form element validation handler for #type 'color'.
     */
    public static function validateColor(&$element, FormStateInterface $form_state, &$complete_form) {
        $value = trim($element['#value']);
        // Default to black if no value is given.
        // @see http://www.w3.org/TR/html5/number-state.html#color-state
        if ($value === '') {
            $form_state->setValueForElement($element, '#000000');
        }
        else {
            // Try to parse the value and normalize it.
            try {
                $form_state->setValueForElement($element, ColorUtility::rgbToHex(ColorUtility::hexToRgb($value)));
            } catch (\InvalidArgumentException $e) {
                $form_state->setError($element, t('%name must be a valid color.', [
                    '%name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title'],
                ]));
            }
        }
    }
    
    /**
     * Prepares a #type 'color' render element for input.html.twig.
     *
     * @param array $element
     *   An associative array containing the properties of the element.
     *   Properties used: #title, #value, #description, #attributes.
     *
     * @return array
     *   The $element with prepared variables ready for input.html.twig.
     */
    public static function preRenderColor($element) {
        $element['#attributes']['type'] = 'color';
        Element::setAttributes($element, [
            'id',
            'name',
            'value',
        ]);
        static::setAttributes($element, [
            'form-color',
        ]);
        return $element;
    }

}

Classes

Title Deprecated Summary
Color Provides a form element for choosing a color.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.