ColorFieldFormatterCss.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterCss.
  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\FieldItemListInterface;
  10. use Drupal\Core\Form\FormStateInterface;
  11. use Drupal\color_field\ColorHex;
  12. /**
  13. * Plugin implementation of the color_field css declaration formatter.
  14. *
  15. * @FieldFormatter(
  16. * id = "color_field_formatter_css",
  17. * module = "color_field",
  18. * label = @Translation("Color CSS declaration"),
  19. * field_types = {
  20. * "color_field_type"
  21. * }
  22. * )
  23. */
  24. class ColorFieldFormatterCss extends FormatterBase {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function defaultSettings() {
  29. return array(
  30. 'selector' => 'body',
  31. 'property' => 'background-color',
  32. 'important' => TRUE,
  33. 'opacity' => TRUE,
  34. ) + parent::defaultSettings();
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function settingsForm(array $form, FormStateInterface $form_state) {
  40. $opacity = $this->getFieldSetting('opacity');
  41. $elements = [];
  42. $elements['selector'] = array(
  43. '#title' => t('Selector'),
  44. '#description' => t('A valid CSS selector such as <code>.links > li > a, #logo</code>.'),
  45. '#type' => 'textarea',
  46. '#rows' => '1',
  47. '#default_value' => $this->getSetting('selector'),
  48. '#required' => TRUE,
  49. '#placeholder' => 'body > div > a',
  50. );
  51. //$element['token'] = array(
  52. // '#theme' => 'token_tree',
  53. // '#token_types' => array($instance['entity_type']),
  54. // '#dialog' => TRUE,
  55. //);
  56. $elements['property'] = array(
  57. '#title' => t('Property'),
  58. '#description' => t(''),
  59. '#type' => 'select',
  60. '#default_value' => $this->getSetting('property'),
  61. '#required' => TRUE,
  62. '#options' => array(
  63. 'background-color' => t('Background color'),
  64. 'color' => t('Text color'),
  65. ),
  66. );
  67. $elements['important'] = array(
  68. '#title' => t('Important'),
  69. '#description' => t('Whenever this declaration is more important than others.'),
  70. '#type' => 'checkbox',
  71. '#default_value' => $this->getSetting('important'),
  72. );
  73. if ($opacity) {
  74. $elements['opacity'] = array(
  75. '#type' => 'checkbox',
  76. '#title' => t('Display opacity'),
  77. '#default_value' => $this->getSetting('opacity'),
  78. );
  79. }
  80. return $elements;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function settingsSummary() {
  86. $opacity = $this->getFieldSetting('opacity');
  87. $settings = $this->getSettings();
  88. $summary = [];
  89. $summary[] = t('CSS selector : @css_selector', array('@css_selector' => $settings['selector']));
  90. $summary[] = t('CSS property : @css_property', array('@css_property' => $settings['property']));
  91. $summary[] = t('!important declaration : @important_declaration', array('@important_declaration' => (($settings['important']) ? t('Yes') : t('No'))));
  92. if ($opacity && $settings['opacity']) {
  93. $summary[] = t('Display with opacity.');
  94. }
  95. return $summary;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function viewElements(FieldItemListInterface $items, $langcode) {
  101. $settings = $this->getSettings();
  102. $elements = [];
  103. foreach ($items as $delta => $item) {
  104. $value = $this->viewValue($item);
  105. $selector = $settings['selector'];
  106. $important = ($settings['important']) ? ' !important' : '';
  107. $property = $settings['property'];
  108. $inline_css = $selector . ' { ' . $property . ': ' . $value . $important . '; }';
  109. // @todo: Not sure this is the best way.
  110. // https://www.drupal.org/node/2391025
  111. // https://www.drupal.org/node/2274843
  112. $elements['#attached']['html_head'][] = [[
  113. '#tag' => 'style',
  114. '#value' => $inline_css,
  115. ], 'colorfield_css'];
  116. }
  117. return $elements;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. protected function viewValue(ColorFieldType $item) {
  123. $opacity = $this->getFieldSetting('opacity');
  124. $settings = $this->getSettings();
  125. $color_hex = new ColorHex($item->color, $item->opacity);
  126. if ($opacity && $settings['opacity']) {
  127. $rgbtext = $color_hex->toRGB()->toString(TRUE);
  128. } else {
  129. $rgbtext = $color_hex->toRGB()->toString(FALSE);
  130. }
  131. return $rgbtext;
  132. }
  133. }