class Color

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

Provides a form element for choosing a color.

Properties:

@property $default_value Default value, in a format like #ffffff.

Example usage:

$form['color'] = [
  '#type' => 'color',
  '#title' => $this->t('Color'),
  '#default_value' => '#ffffff',
];

Attributes

#[FormElement('color')]

Hierarchy

Expanded class hierarchy of Color

24 string references to 'Color'
AjaxFormsTestSimpleForm::buildForm in core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestSimpleForm.php
Form constructor.
claro_preprocess_input in core/themes/claro/claro.theme
Implements template_preprocess_HOOK() for input.
Color::preRenderColor in core/lib/Drupal/Core/Render/Element/Color.php
Prepares a #type 'color' render element for input.html.twig.
ContentEntityNonRevisionableFieldTest::testMultiColumnNonRevisionableBaseField in core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php
Tests multi column non revisionable base field for revisionable entity.
core.data_types.schema.yml in core/config/schema/core.data_types.schema.yml
core/config/schema/core.data_types.schema.yml

... See full list

3 #type uses of Color
FormTestColorForm::buildForm in core/modules/system/tests/modules/form_test/src/Form/FormTestColorForm.php
Form constructor.
FormTestDisabledElementsForm::buildForm in core/modules/system/tests/modules/form_test/src/Form/FormTestDisabledElementsForm.php
Form constructor.
IconExtractorSettingsFormTest::settingsFormDataProvider in core/tests/Drupal/Tests/Core/Theme/Icon/IconExtractorSettingsFormTest.php
Provide data for testGenerateSettingsForm.

File

core/lib/Drupal/Core/Render/Element/Color.php, line 27

Namespace

Drupal\Core\Render\Element
View source
class Color extends FormElementBase {
  
  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    return [
      '#input' => TRUE,
      '#process' => [
        [
          static::class,
          'processAjaxForm',
        ],
      ],
      '#element_validate' => [
        [
          static::class,
          'validateColor',
        ],
      ],
      '#pre_render' => [
        [
          static::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 https://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) {
        $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;
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
Color::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
Color::preRenderColor public static function Prepares a #type 'color' render element for input.html.twig.
Color::validateColor public static function Form element validation handler for #type 'color'.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
FormElementBase::processAutocomplete public static function Adds autocomplete functionality to elements. 1
FormElementBase::processPattern public static function Render API callback: Handles the #pattern form element property. 1
FormElementBase::validatePattern public static function Render API callback: Handles the #pattern form element property.. 1
FormElementBase::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementInterface::valueCallback 17
MessengerTrait::$messenger protected property The messenger. 25
MessengerTrait::messenger public function Gets the messenger. 25
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin ID.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin ID of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable Deprecated public function Determines if the plugin is configurable.
RenderElementBase::$renderParent protected property The parent element.
RenderElementBase::$renderParentName protected property The parent key.
RenderElementBase::$storage protected property The storage.
RenderElementBase::addChild public function Adds a child render element. Overrides ElementInterface::addChild
RenderElementBase::changeType public function Change the type of the element. Overrides ElementInterface::changeType
RenderElementBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
RenderElementBase::createChild public function Creates a render object and attaches it to the current render object. Overrides ElementInterface::createChild
RenderElementBase::elementInfoManager protected function Returns the element info manager.
RenderElementBase::getChild public function Gets a child. Overrides ElementInterface::getChild
RenderElementBase::getChildren public function Returns child elements. Overrides ElementInterface::getChildren
RenderElementBase::initializeInternalStorage public function Initialize storage. Overrides ElementInterface::initializeInternalStorage
RenderElementBase::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript. 2
RenderElementBase::preRenderGroup public static function Adds members of this group as actual elements for rendering. 2
RenderElementBase::processAjaxForm public static function Form element processing handler for the #ajax form property. 3
RenderElementBase::processGroup public static function Arranges elements into groups. 2
RenderElementBase::removeChild public function Removes a child. Overrides ElementInterface::removeChild
RenderElementBase::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes 2
RenderElementBase::setType protected function Set type on initialize. 1
RenderElementBase::toRenderable public function Returns a render array. Overrides ElementInterface::toRenderable
RenderElementBase::__construct public function Constructs a new render element object. Overrides PluginBase::__construct 6
RenderElementBase::__get public function Magic method: gets a property value.
RenderElementBase::__isset public function Magic method: checks if a property value is set.
RenderElementBase::__set public function Magic method: Sets a property value.
RenderElementBase::__sleep public function Overrides DependencySerializationTrait::__sleep
RenderElementBase::__unset public function Magic method: unsets a property value.
RenderElementBase::__wakeup public function Overrides DependencySerializationTrait::__wakeup
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

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