ColorFieldFormatterSwatch.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatch.
  5. */
  6. namespace Drupal\color_field\Plugin\Field\FieldFormatter;
  7. use Drupal\color_field\Plugin\Field\FieldType\ColorFieldType;
  8. use Drupal\Core\Field\FormatterBase;
  9. use Drupal\Core\Field\FieldItemInterface;
  10. use Drupal\Core\Field\FieldItemListInterface;
  11. use Drupal\Core\Form\FormStateInterface;
  12. use Drupal\color_field\ColorHex;
  13. /**
  14. * Plugin implementation of the color_field swatch formatter.
  15. *
  16. * @FieldFormatter(
  17. * id = "color_field_formatter_swatch",
  18. * module = "color_field",
  19. * label = @Translation("Color swatch"),
  20. * field_types = {
  21. * "color_field_type"
  22. * }
  23. * )
  24. */
  25. class ColorFieldFormatterSwatch extends FormatterBase {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function defaultSettings() {
  30. return array(
  31. 'shape' => 'square',
  32. 'width' => 50,
  33. 'height' => 50,
  34. 'opacity' => TRUE,
  35. ) + parent::defaultSettings();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function settingsForm(array $form, FormStateInterface $form_state) {
  41. $opacity = $this->getFieldSetting('opacity');
  42. $elements = [];
  43. $elements['shape'] = array(
  44. '#type' => 'select',
  45. '#title' => t('Shape'),
  46. '#options' => $this->getShape(),
  47. '#default_value' => $this->getSetting('shape'),
  48. '#description' => t(''),
  49. );
  50. $elements['width'] = array(
  51. '#type' => 'number',
  52. '#title' => t('Width'),
  53. '#default_value' => $this->getSetting('width'),
  54. '#min' => 1,
  55. '#description' => t(''),
  56. );
  57. $elements['height'] = array(
  58. '#type' => 'number',
  59. '#title' => t('Height'),
  60. '#default_value' => $this->getSetting('height'),
  61. '#min' => 1,
  62. '#description' => t(''),
  63. );
  64. if ($opacity) {
  65. $elements['opacity'] = array(
  66. '#type' => 'checkbox',
  67. '#title' => t('Display opacity'),
  68. '#default_value' => $this->getSetting('opacity'),
  69. );
  70. }
  71. return $elements;
  72. }
  73. /**
  74. * @param string $shape
  75. * @return array|string
  76. */
  77. protected function getShape($shape = NULL) {
  78. $formats = [];
  79. $formats['square'] = $this->t('Square');
  80. $formats['circle'] = $this->t('Circle');
  81. $formats['parallelogram'] = $this->t('Parallelogram');
  82. if ($shape) {
  83. return $formats[$shape];
  84. }
  85. return $formats;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function settingsSummary() {
  91. $opacity = $this->getFieldSetting('opacity');
  92. $settings = $this->getSettings();
  93. $summary = [];
  94. $summary[] = t('@shape', array(
  95. '@shape' => $this->getShape($settings['shape']),
  96. ));
  97. $summary[] = t('Width: @width Height: @height', array(
  98. '@width' => $settings['width'],
  99. '@height' => $settings['height']
  100. ));
  101. if ($opacity && $settings['opacity']) {
  102. $summary[] = t('Display with opacity.');
  103. }
  104. return $summary;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function viewElements(FieldItemListInterface $items, $langcode) {
  110. $settings = $this->getSettings();
  111. $elements = [];
  112. $elements['#attached']['library'][] = 'color_field/color-field-formatter-swatch';
  113. foreach ($items as $delta => $item) {
  114. $elements[$delta] = array(
  115. '#theme' => 'color_field_formatter_swatch',
  116. '#color' => $this->viewValue($item),
  117. '#shape' => $settings['shape'],
  118. '#width' => $settings['width'],
  119. '#height' => $settings['height'],
  120. );
  121. }
  122. return $elements;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. protected function viewValue(ColorFieldType $item) {
  128. $opacity = $this->getFieldSetting('opacity');
  129. $settings = $this->getSettings();
  130. $color_hex = new ColorHex($item->color, $item->opacity);
  131. if ($opacity && $settings['opacity']) {
  132. $rgbtext = $color_hex->toRGB()->toString(TRUE);
  133. } else {
  134. $rgbtext = $color_hex->toRGB()->toString(FALSE);
  135. }
  136. return $rgbtext;
  137. }
  138. }