Color.php in Color Field 8.2
File
src/Feeds/Target/Color.php
View source
<?php
namespace Drupal\color_field\Feeds\Target;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\feeds\FieldTargetDefinition;
use Drupal\feeds\Plugin\Type\Target\ConfigurableTargetInterface;
use Drupal\feeds\Plugin\Type\Target\FieldTargetBase;
class Color extends FieldTargetBase implements ConfigurableTargetInterface {
protected static function prepareTarget(FieldDefinitionInterface $field_definition) {
return FieldTargetDefinition::createFromFieldDefinition($field_definition)
->addProperty('color')
->addProperty('opacity');
}
protected function prepareValue($delta, array &$values) {
$color = trim($values['color']);
if (substr($color, 0, 1) === '#') {
$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;
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'format' => '#hexhex',
];
}
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;
}
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;
}
}
Classes
Name |
Description |
Color |
Defines a color field mapper. |