Plugin implementation of the 'color_field_swatch' formatter.
Plugin annotation
@FieldFormatter(
id = "color_field_swatch",
module = "color_field",
label = @Translation("Color Swatch"),
field_types = {
"color_field",
}
)
Hierarchy
- class \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldSwatchFormatter extends \Drupal\Core\Field\FormatterBase
Expanded class hierarchy of ColorFieldSwatchFormatter
File
-
src/
Plugin/ Field/ FieldFormatter/ ColorFieldSwatchFormatter.php, line 26
Namespace
Drupal\color_field\Plugin\Field\FieldFormatterView source
class ColorFieldSwatchFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'width' => 50,
'height' => 50,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['width'] = array(
'#type' => 'number',
'#title' => t('Width'),
'#default_value' => $this->getSetting('width'),
'#min' => 1,
'#description' => t(''),
);
$elements['height'] = array(
'#type' => 'number',
'#title' => t('Height'),
'#default_value' => $this->getSetting('height'),
'#min' => 1,
'#description' => t(''),
);
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$settings = $this->getSettings();
$summary[] = t('Width: @width Height: @height', array(
'@width' => $settings['width'],
'@height' => $settings['height'],
));
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
$settings = $this->getSettings();
$opacity = $this->getFieldSetting('opacity');
foreach ($items as $delta => $item) {
$color = color_field_hex2rgb($item->color);
if ($opacity) {
$rgbtext = 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ',' . $item->opacity . ')';
}
else {
$rgbtext = 'rgb(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ')';
}
$elements[$delta] = array(
'#markup' => '<div style="background-color: ' . $rgbtext . '; width: ' . $settings['width'] . 'px; height: ' . $settings['height'] . 'px;"></div>',
);
}
return $elements;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
ColorFieldSwatchFormatter::defaultSettings | public static | function | |
ColorFieldSwatchFormatter::settingsForm | public | function | |
ColorFieldSwatchFormatter::settingsSummary | public | function | |
ColorFieldSwatchFormatter::viewElements | public | function |