ColorFieldWidgetBox.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldWidgetBox.
  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 box widget.
  12. *
  13. * @FieldWidget(
  14. * id = "color_field_widget_box",
  15. * module = "color_field",
  16. * label = @Translation("Color boxes"),
  17. * field_types = {
  18. * "color_field_type"
  19. * }
  20. * )
  21. */
  22. class ColorFieldWidgetBox extends WidgetBase {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function defaultSettings() {
  27. return array(
  28. 'default_colors' => '
  29. #AC725E,#D06B64,#F83A22,#FA573C,#FF7537,#FFAD46
  30. #42D692,#16A765,#7BD148,#B3DC6C,#FBE983
  31. #92E1C0,#9FE1E7,#9FC6E7,#4986E7,#9A9CFF
  32. #B99AFF,#C2C2C2,#CABDBF,#CCA6AC,#F691B2
  33. #CD74E6,#A47AE2',
  34. ) + parent::defaultSettings();
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function settingsForm(array $form, FormStateInterface $form_state) {
  40. $element['default_colors'] = array(
  41. '#type' => 'textarea',
  42. '#title' => t('Default colors'),
  43. '#default_value' => $this->getSetting('default_colors'),
  44. '#required' => TRUE,
  45. '#description' => t('Default colors for pre-selected color boxes'),
  46. );
  47. return $element;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function settingsSummary() {
  53. $summary = [];
  54. $default_colors = $this->getSetting('default_colors');
  55. if (!empty($default_colors)) {
  56. preg_match_all("/#[0-9a-fA-F]{6}/", $default_colors, $default_colors, PREG_SET_ORDER);
  57. foreach ($default_colors as $color) {
  58. $colors = $color[0];
  59. $summary[] = $colors;
  60. }
  61. }
  62. if (empty($summary)) {
  63. $summary[] = t('No default colors');
  64. }
  65. return $summary;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  71. // We are nesting some sub-elements inside the parent, so we need a wrapper.
  72. // We also need to add another #title attribute at the top level for ease in
  73. // identifying this item in error messages. We do not want to display this
  74. // title because the actual title display is handled at a higher level by
  75. // the Field module.
  76. $element['#theme_wrappers'] = array('color_field_widget_box');
  77. $element['#attributes']['class'][] = 'container-inline';
  78. $element['#attached']['library'][] = 'color_field/color-field-widget-box';
  79. // Set Drupal settings.
  80. $settings = [];
  81. $default_colors = $this->getSetting('default_colors');
  82. preg_match_all("/#[0-9a-fA-F]{6}/", $default_colors, $default_colors, PREG_SET_ORDER);
  83. foreach ($default_colors as $color) {
  84. $settings['default_colors'][] = $color[0];
  85. }
  86. $element['#attached']['drupalSettings']['color_field']['color_field_widget_box']['settings'] = $settings;
  87. // Retrieve field label and description.
  88. $element['#title'] = $this->fieldDefinition->getLabel();;
  89. $element['#description'] = $this->fieldDefinition->getDescription();
  90. // Prepare color.
  91. $color = NULL;
  92. if (isset($items[$delta]->color)) {
  93. $color = $items[$delta]->color;
  94. if (substr($color, 0, 1) !== '#') {
  95. $color = '#' . $color;
  96. }
  97. }
  98. $element['color'] = array(
  99. '#maxlength' => 7,
  100. '#size' => 7,
  101. '#type' => 'textfield',
  102. '#default_value' => $color,
  103. '#attributes' => array('class' => array('visually-hidden')),
  104. );
  105. $element['color']['#suffix'] = "<div class='color-field-widget-box-form'></div>";
  106. if ($this->getFieldSetting('opacity')) {
  107. $element['opacity'] = array(
  108. '#title' => t('Opacity'),
  109. '#type' => 'textfield',
  110. '#maxlength' => 4,
  111. '#size' => 4,
  112. '#default_value' => isset($items[$delta]->opacity) ? $items[$delta]->opacity : NULL,
  113. '#placeholder' => $this->getSetting('placeholder_opacity'),
  114. );
  115. }
  116. return $element;
  117. }
  118. }