"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Render/Element/Color.php" (26 Nov 2020, 2393 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Color.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.0.8_vs_9.1.0-rc1.

    1 <?php
    2 
    3 namespace Drupal\Core\Render\Element;
    4 
    5 use Drupal\Core\Form\FormStateInterface;
    6 use Drupal\Core\Render\Element;
    7 use Drupal\Component\Utility\Color as ColorUtility;
    8 
    9 /**
   10  * Provides a form element for choosing a color.
   11  *
   12  * Properties:
   13  * - #default_value: Default value, in a format like #ffffff.
   14  *
   15  * Example usage:
   16  * @code
   17  * $form['color'] = array(
   18  *   '#type' => 'color',
   19  *   '#title' => $this->t('Color'),
   20  *   '#default_value' => '#ffffff',
   21  * );
   22  * @endcode
   23  *
   24  * @FormElement("color")
   25  */
   26 class Color extends FormElement {
   27 
   28   /**
   29    * {@inheritdoc}
   30    */
   31   public function getInfo() {
   32     $class = get_class($this);
   33     return [
   34       '#input' => TRUE,
   35       '#process' => [
   36         [$class, 'processAjaxForm'],
   37       ],
   38       '#element_validate' => [
   39         [$class, 'validateColor'],
   40       ],
   41       '#pre_render' => [
   42         [$class, 'preRenderColor'],
   43       ],
   44       '#theme' => 'input__color',
   45       '#theme_wrappers' => ['form_element'],
   46     ];
   47   }
   48 
   49   /**
   50    * Form element validation handler for #type 'color'.
   51    */
   52   public static function validateColor(&$element, FormStateInterface $form_state, &$complete_form) {
   53     $value = trim($element['#value']);
   54 
   55     // Default to black if no value is given.
   56     // @see http://www.w3.org/TR/html5/number-state.html#color-state
   57     if ($value === '') {
   58       $form_state->setValueForElement($element, '#000000');
   59     }
   60     else {
   61       // Try to parse the value and normalize it.
   62       try {
   63         $form_state->setValueForElement($element, ColorUtility::rgbToHex(ColorUtility::hexToRgb($value)));
   64       }
   65       catch (\InvalidArgumentException $e) {
   66         $form_state->setError($element, t('%name must be a valid color.', ['%name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title']]));
   67       }
   68     }
   69   }
   70 
   71   /**
   72    * Prepares a #type 'color' render element for input.html.twig.
   73    *
   74    * @param array $element
   75    *   An associative array containing the properties of the element.
   76    *   Properties used: #title, #value, #description, #attributes.
   77    *
   78    * @return array
   79    *   The $element with prepared variables ready for input.html.twig.
   80    */
   81   public static function preRenderColor($element) {
   82     $element['#attributes']['type'] = 'color';
   83     Element::setAttributes($element, ['id', 'name', 'value']);
   84     static::setAttributes($element, ['form-color']);
   85 
   86     return $element;
   87   }
   88 
   89 }