ColorFieldFormatterText.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterText.
  5. */
  6. namespace Drupal\color_field\Plugin\Field\FieldFormatter;
  7. use Drupal\color_field\Plugin\Field\FieldType\ColorFieldType;
  8. use Drupal\Core\Field\FieldItemListInterface;
  9. use Drupal\Core\Field\FormatterBase;
  10. use Drupal\Core\Form\FormStateInterface;
  11. use Drupal\color_field\ColorHex;
  12. /**
  13. * Plugin implementation of the color_field text formatter.
  14. *
  15. * @FieldFormatter(
  16. * id = "color_field_formatter_text",
  17. * module = "color_field",
  18. * label = @Translation("Color text"),
  19. * field_types = {
  20. * "color_field_type"
  21. * }
  22. * )
  23. */
  24. class ColorFieldFormatterText extends FormatterBase {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function defaultSettings() {
  29. return array(
  30. 'format' => 'hex',
  31. 'opacity' => TRUE,
  32. ) + parent::defaultSettings();
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function settingsForm(array $form, FormStateInterface $form_state) {
  38. $opacity = $this->getFieldSetting('opacity');
  39. $elements = [];
  40. $elements['format'] = array(
  41. '#type' => 'select',
  42. '#title' => t('Format'),
  43. '#options' => $this->getColorFormat(),
  44. '#default_value' => $this->getSetting('format'),
  45. );
  46. if ($opacity) {
  47. $elements['opacity'] = array(
  48. '#type' => 'checkbox',
  49. '#title' => t('Display opacity'),
  50. '#default_value' => $this->getSetting('opacity'),
  51. );
  52. }
  53. return $elements;
  54. }
  55. /**
  56. * @param string $format
  57. * @return array|string
  58. */
  59. protected function getColorFormat($format = NULL) {
  60. $formats = [];
  61. $formats['hex'] = $this->t('Hex triplet');
  62. $formats['rgb'] = $this->t('RGB Decimal');
  63. if ($format) {
  64. return $formats[$format];
  65. }
  66. return $formats;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function settingsSummary() {
  72. $opacity = $this->getFieldSetting('opacity');
  73. $settings = $this->getSettings();
  74. $summary = [];
  75. $summary[] = t('@format', array(
  76. '@format' => $this->getColorFormat($settings['format']),
  77. ));
  78. if ($opacity && $settings['opacity']) {
  79. $summary[] = t('Display with opacity.');
  80. }
  81. return $summary;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function viewElements(FieldItemListInterface $items, $langcode) {
  87. $elements = [];
  88. foreach ($items as $delta => $item) {
  89. $elements[$delta] = ['#markup' => $this->viewValue($item)];
  90. }
  91. return $elements;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function viewValue(ColorFieldType $item) {
  97. $opacity = $this->getFieldSetting('opacity');
  98. $settings = $this->getSettings();
  99. $color_hex = new ColorHex($item->color, $item->opacity);
  100. switch ($settings['format']) {
  101. case 'hex':
  102. if ($opacity && $settings['opacity']) {
  103. $output = $color_hex->toString(TRUE);
  104. } else {
  105. $output = $color_hex->toString(FALSE);
  106. }
  107. break;
  108. case 'rgb':
  109. if ($opacity && $settings['opacity']) {
  110. $output = $color_hex->toRGB()->toString(TRUE);
  111. } else {
  112. $output = $color_hex->toRGB()->toString(FALSE);
  113. }
  114. break;
  115. }
  116. return $output;
  117. }
  118. }