Same name and namespace in other branches
  1. 8.x-2.x src/Plugin/Field/FieldType/ColorFieldType.php \Drupal\color_field\Plugin\Field\FieldType\ColorFieldType::preSave() 1 comment

File

src/Plugin/Field/FieldType/ColorFieldType.php, line 132

Class

ColorFieldType
Plugin implementation of the 'color_type' field type.

Namespace

Drupal\color_field\Plugin\Field\FieldType

Code

public function preSave() {
    parent::preSave();
    if ($format = $this->getSetting('format')) {
        $color = $this->get('color')
            ->getValue();
        // Clean up data and format it.
        $color = trim($color);
        if (str_starts_with($color, '#')) {
            $color = substr($color, 1);
        }
        switch ($format) {
            case '#HEXHEX':
                $color = '#' . strtoupper($color);
                break;
            case 'HEXHEX':
                $color = strtoupper($color);
                break;
            case '#hexhex':
                $color = '#' . strtolower($color);
                break;
            case 'hexhex':
                $color = strtolower($color);
                break;
        }
        $this->set('color', $color);
    }
    if ($this->getSetting('opacity')) {
        return;
    }
    $this->set('opacity', NULL);
}