ColorFieldFormatterText.php in Color Field 8.2
File
src/Plugin/Field/FieldFormatter/ColorFieldFormatterText.php
View source
<?php
namespace Drupal\color_field\Plugin\Field\FieldFormatter;
use Drupal\color_field\Plugin\Field\FieldType\ColorFieldType;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\color_field\ColorHex;
class ColorFieldFormatterText extends FormatterBase {
public static function defaultSettings() {
return [
'format' => 'hex',
'opacity' => TRUE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$opacity = $this
->getFieldSetting('opacity');
$elements = [];
$elements['format'] = [
'#type' => 'select',
'#title' => $this
->t('Format'),
'#options' => $this
->getColorFormat(),
'#default_value' => $this
->getSetting('format'),
];
if ($opacity) {
$elements['opacity'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display opacity'),
'#default_value' => $this
->getSetting('opacity'),
];
}
return $elements;
}
protected function getColorFormat($format = NULL) {
$formats = [];
$formats['hex'] = $this
->t('Hex triplet');
$formats['rgb'] = $this
->t('RGB Decimal');
if ($format) {
return $formats[$format];
}
return $formats;
}
public function settingsSummary() {
$opacity = $this
->getFieldSetting('opacity');
$settings = $this
->getSettings();
$summary = [];
$summary[] = $this
->t('@format', [
'@format' => $this
->getColorFormat($settings['format']),
]);
if ($opacity && $settings['opacity']) {
$summary[] = $this
->t('Display with opacity.');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => $this
->viewValue($item),
];
}
return $elements;
}
protected function viewValue(ColorFieldType $item) {
$opacity = $this
->getFieldSetting('opacity');
$settings = $this
->getSettings();
$color_hex = new ColorHex($item->color, $item->opacity);
switch ($settings['format']) {
case 'hex':
if ($opacity && $settings['opacity']) {
$output = $color_hex
->toString(TRUE);
}
else {
$output = $color_hex
->toString(FALSE);
}
break;
case 'rgb':
if ($opacity && $settings['opacity']) {
$output = $color_hex
->toRgb()
->toString(TRUE);
}
else {
$output = $color_hex
->toRgb()
->toString(FALSE);
}
break;
}
return $output;
}
}