ColorFieldWidgetDefault.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldWidgetDefault.
  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_widget default input widget.
  12. *
  13. * @FieldWidget(
  14. * id = "color_field_widget_default",
  15. * module = "color_field",
  16. * label = @Translation("Color default"),
  17. * field_types = {
  18. * "color_field_type"
  19. * }
  20. * )
  21. */
  22. class ColorFieldWidgetDefault extends WidgetBase {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function defaultSettings() {
  27. return array(
  28. 'placeholder_color' => '',
  29. 'placeholder_opacity' => '',
  30. ) + parent::defaultSettings();
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function settingsForm(array $form, FormStateInterface $form_state) {
  36. $element['placeholder_color'] = array(
  37. '#type' => 'textfield',
  38. '#title' => t('Color placeholder'),
  39. '#default_value' => $this->getSetting('placeholder_color'),
  40. '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
  41. );
  42. $element['placeholder_opacity'] = array(
  43. '#type' => 'textfield',
  44. '#title' => t('Opacity placeholder'),
  45. '#default_value' => $this->getSetting('placeholder_opacity'),
  46. '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
  47. );
  48. return $element;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function settingsSummary() {
  54. $summary = [];
  55. $placeholder_color = $this->getSetting('placeholder_color');
  56. $placeholder_opacity = $this->getSetting('placeholder_opacity');
  57. if (!empty($placeholder_color)) {
  58. $summary[] = t('Color placeholder: @placeholder_color', array('@placeholder_color' => $placeholder_color));
  59. }
  60. if (!empty($placeholder_opacity)) {
  61. $summary[] = t('Opacity placeholder: @placeholder_opacity', array('@placeholder_opacity' => $placeholder_opacity));
  62. }
  63. if (empty($summary)) {
  64. $summary[] = t('No placeholder');
  65. }
  66. return $summary;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  72. $label = $this->fieldDefinition->getLabel();
  73. $element['color'] = array(
  74. '#title' => t($label),
  75. '#type' => 'textfield',
  76. '#maxlength' => 7,
  77. '#size' => 7,
  78. '#required' => $element['#required'],
  79. '#placeholder' => $this->getSetting('placeholder_color'),
  80. '#default_value' => isset($items[$delta]->color) ? $items[$delta]->color : NULL,
  81. );
  82. if ($this->getFieldSetting('opacity')) {
  83. $element['color']['#prefix'] = '<div class="container-inline">';
  84. $element['opacity'] = array(
  85. '#title' => t('Opacity'),
  86. '#type' => 'textfield',
  87. '#maxlength' => 4,
  88. '#size' => 4,
  89. '#required' => $element['#required'],
  90. '#placeholder' => $this->getSetting('placeholder_opacity'),
  91. '#default_value' => isset($items[$delta]->opacity) ? $items[$delta]->opacity : NULL,
  92. '#suffix' => '</div>',
  93. );
  94. }
  95. return $element;
  96. }
  97. }