Plugin implementation of the 'color_field_swatch' formatter.
Plugin annotation
@FieldFormatter(
id = "color_field_text",
module = "color_field",
label = @Translation("Color Text"),
field_types = {
"color_field",
}
)
Hierarchy
- class \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldTextFormatter extends \Drupal\Core\Field\FormatterBase
Expanded class hierarchy of ColorFieldTextFormatter
Fichier
-
src/
Plugin/ Field/ FieldFormatter/ ColorFieldTextFormatter.php, line 26
Namespace
Drupal\color_field\Plugin\Field\FieldFormatterView source
class ColorFieldTextFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'format' => 'hexadecimal',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['format'] = array(
'#type' => 'select',
'#title' => t('Format'),
'#options' => $this->getColorFormat(),
'#default_value' => $this->getSetting('format'),
);
return $elements;
}
public function getColorFormat($format = NULL) {
$formats = array();
$formats['hexadecimal'] = $this->t('Hexadecimal Colors');
$formats['rgb'] = $this->t('RGB Colors');
$formats['rgba'] = $this->t('RGBA Colors');
// $formats['hsl'] = $this->t('HSL Colors');
// $formats['hsla'] = $this->t('HSLA Colors');
if ($format) {
return $formats[$format];
}
return $formats;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$format = $this->getSetting('format');
$summary[] = t('Format: @format', array(
'@format' => $this->getColorFormat($format),
));
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$format = $this->getSetting('format');
$opacity = $this->getFieldSetting('opacity');
foreach ($items as $delta => $item) {
switch ($format) {
case 'hexadecimal':
$output = '#' . $item->color;
break;
case 'rgb':
$color = color_field_hex2rgb($item->color);
$output = 'rgb(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ')';
break;
case 'rgba':
$color = color_field_hex2rgb($item->color);
$item->opacity = $opacity ? $item->color : 1;
$output = 'rgb(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ',' . $item->color . ')';
break;
}
$elements[$delta] = array(
'#markup' => $output,
);
}
return $elements;
}
}
Members
Titre Trier par ordre décroissant | Modifiers | Object type | Résumé |
---|---|---|---|
ColorFieldTextFormatter::defaultSettings | public static | function | |
ColorFieldTextFormatter::getColorFormat | public | function | |
ColorFieldTextFormatter::settingsForm | public | function | |
ColorFieldTextFormatter::settingsSummary | public | function | |
ColorFieldTextFormatter::viewElements | public | function |