Same name and namespace in other branches
  1. 8.x-1.x src/Color.php \Drupal\color_field\Color 1 comment
  2. 8.x-2.x src/Feeds/Target/Color.php \Drupal\color_field\Feeds\Target\Color 1 comment

Defines a color field mapper.

Plugin annotation


@FeedsTarget(
  id = "color",
  field_types = {
    "color_field_type"
  }
)

Hierarchy

  • class \Drupal\color_field\Feeds\Target\Color extends \Drupal\feeds\Plugin\Type\Target\FieldTargetBase implements \Drupal\feeds\Plugin\Type\Target\ConfigurableTargetInterface

Expanded class hierarchy of Color

6 string references to 'Color'
ColorFieldType::mainPropertyName in src/Plugin/Field/FieldType/ColorFieldType.php
ColorFieldType::preSave in src/Plugin/Field/FieldType/ColorFieldType.php
ColorFieldType::propertyDefinitions in src/Plugin/Field/FieldType/ColorFieldType.php
ColorFieldType::schema in src/Plugin/Field/FieldType/ColorFieldType.php
ColorFieldWidgetHTML5::formElement in src/Plugin/Field/FieldWidget/ColorFieldWidgetHTML5.php

... See full list

File

src/Feeds/Target/Color.php, line 23

Namespace

Drupal\color_field\Feeds\Target
View source
class Color extends FieldTargetBase implements ConfigurableTargetInterface {
    
    /**
     * {@inheritdoc}
     */
    public function defaultConfiguration() : array {
        return parent::defaultConfiguration() + [
            'format' => '#hexhex',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
        $form = parent::buildConfigurationForm($form, $form_state);
        $form['format'] = [
            '#type' => 'select',
            '#title' => $this->t('Format'),
            '#options' => [
                '#HEXHEX' => $this->t('#123ABC'),
                'HEXHEX' => $this->t('123ABC'),
                '#hexhex' => $this->t('#123abc'),
                'hexhex' => $this->t('123abc'),
            ],
            '#default_value' => $this->configuration['format'],
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSummary() {
        $summary = parent::getSummary();
        $summary[] = $this->configuration['format'] ? $summary[] = $this->t('Color with format: %format', [
            '%format' => $this->configuration['format'],
        ]) : $this->t('Color by default');
        return $summary;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function prepareValue($delta, array &$values) {
        // Clean up data and format it.
        $color = trim($values['color']);
        if (str_starts_with($color, '#')) {
            $color = substr($color, 1);
        }
        switch ($this->configuration['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;
        }
        $values['color'] = $color;
        $values['opacity'] = $values['opacity'] ? (double) $values['opacity'] : 0.0;
    }
    
    /**
     * {@inheritdoc}
     */
    protected static function prepareTarget(FieldDefinitionInterface $field_definition) {
        return FieldTargetDefinition::createFromFieldDefinition($field_definition)->addProperty('color')
            ->addProperty('opacity');
    }

}

Members