ColorFieldWidgetSpectrum.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldWidgetSpectrum.
  5. */
  6. namespace Drupal\color_field\Plugin\Field\FieldWidget;
  7. use Drupal\Core\Field\FieldItemListInterface;
  8. use Drupal\Core\Field\WidgetBase;
  9. use Drupal\Core\Form\FormStateInterface;
  10. /**
  11. * Plugin implementation of the color_field spectrum widget.
  12. *
  13. * @FieldWidget(
  14. * id = "color_field_widget_spectrum",
  15. * module = "color_field",
  16. * label = @Translation("Color spectrum"),
  17. * field_types = {
  18. * "color_field_type"
  19. * }
  20. * )
  21. */
  22. class ColorFieldWidgetSpectrum extends WidgetBase {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function defaultSettings() {
  27. return array(
  28. 'show_input' => FALSE,
  29. 'show_palette' => FALSE,
  30. 'palette' => '',
  31. 'show_palette_only' => TRUE,
  32. 'show_buttons' => FALSE,
  33. 'allow_empty' => FALSE,
  34. ) + parent::defaultSettings();
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function settingsForm(array $form, FormStateInterface $form_state) {
  40. $element = [];
  41. $element['show_input'] = array(
  42. '#type' => 'checkbox',
  43. '#title' => t('Show Input'),
  44. '#default_value' => $this->getSetting('show_input'),
  45. '#description' => t('Allow free form typing.'),
  46. );
  47. $element['show_palette'] = array(
  48. '#type' => 'checkbox',
  49. '#title' => t('Show Palette'),
  50. '#default_value' => $this->getSetting('show_palette'),
  51. '#description' => t('Show or hide Palette in Spectrum Widget'),
  52. );
  53. $element['palette'] = array(
  54. '#type' => 'textarea',
  55. '#title' => t('Color Palette'),
  56. '#default_value' => $this->getSetting('palette'),
  57. '#description' => t('Selectable color palette to accompany the Spectrum Widget'),
  58. '#states' => array(
  59. 'visible' => array(
  60. ':input[name="field[settings][show_palette]"]' => array('checked' => TRUE),
  61. ),
  62. ),
  63. );
  64. $element['show_palette_only'] = array(
  65. '#type' => 'checkbox',
  66. '#title' => t('Show Palette Only'),
  67. '#default_value' => $this->getSetting('show_palette_only'),
  68. '#description' => t('Only show thePalette in Spectrum Widget and nothing else'),
  69. );
  70. $element['show_buttons'] = array(
  71. '#type' => 'checkbox',
  72. '#title' => t('Show Buttons'),
  73. '#default_value' => $this->getSetting('show_buttons'),
  74. '#description' => t('Add Cancel/Confirm Button.'),
  75. );
  76. $element['allow_empty'] = array(
  77. '#type' => 'checkbox',
  78. '#title' => t('Allow Empty'),
  79. '#default_value' => $this->getSetting('allow_empty'),
  80. '#description' => t('Allow empty value.'),
  81. );
  82. return $element;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function settingsSummary() {
  88. $summary = array();
  89. return $summary;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  95. // We are nesting some sub-elements inside the parent, so we need a wrapper.
  96. // We also need to add another #title attribute at the top level for ease in
  97. // identifying this item in error messages. We do not want to display this
  98. // title because the actual title display is handled at a higher level by
  99. // the Field module.
  100. $element['#theme_wrappers'] = array('color_field_widget_spectrum');
  101. $element['#attached']['library'][] = 'color_field/color-field-widget-spectrum';
  102. // Set Drupal settings.
  103. $settings = $this->getSettings();
  104. $settings['show_alpha'] = $this->getFieldSetting('opacity');
  105. $element['#attached']['drupalSettings']['color_field']['color_field_widget_spectrum'] = $settings;
  106. // Prepare color.
  107. $color = NULL;
  108. if (isset($items[$delta]->color)) {
  109. $color = $items[$delta]->color;
  110. if (substr($color, 0, 1) !== '#') {
  111. $color = '#' . $color;
  112. }
  113. }
  114. $element['color'] = array(
  115. '#type' => 'textfield',
  116. '#maxlength' => 7,
  117. '#size' => 7,
  118. '#required' => $element['#required'],
  119. '#default_value' => $color,
  120. '#attributes' => array('class' => array('js-color-field-widget-spectrum__color')),
  121. );
  122. if ($this->getFieldSetting('opacity')) {
  123. $element['opacity'] = array(
  124. '#type' => 'textfield',
  125. '#maxlength' => 4,
  126. '#size' => 4,
  127. '#required' => $element['#required'],
  128. '#default_value' => isset($items[$delta]->opacity) ? $items[$delta]->opacity : NULL,
  129. '#attributes' => array('class' => array('js-color-field-widget-spectrum__opacity', 'visually-hidden')),
  130. );
  131. }
  132. return $element;
  133. }
  134. }