ColorFieldWidgetGrid.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldWidgetGrid.
  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_default' widget.
  12. *
  13. * @FieldWidget(
  14. * id = "color_field_widget_grid",
  15. * module = "color_field",
  16. * label = @Translation("Color grid"),
  17. * field_types = {
  18. * "color_field_type"
  19. * }
  20. * )
  21. */
  22. class ColorFieldWidgetGrid extends WidgetBase {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function defaultSettings() {
  27. return array(
  28. 'cell_width' => 10,
  29. 'cell_height' => 10,
  30. 'cell_margin' => 1,
  31. 'box_width' => 115,
  32. 'box_height' => 20,
  33. 'columns' => 16,
  34. ) + parent::defaultSettings();
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function settingsForm(array $form, FormStateInterface $form_state) {
  40. $element = [];
  41. $element['cell_width'] = array(
  42. '#type' => 'textfield',
  43. '#title' => t('Cell width'),
  44. '#default_value' => $this->getSetting('cell_width'),
  45. '#required' => TRUE,
  46. '#description' => t('Width of each individual color cell.'),
  47. );
  48. $element['cell_height'] = array(
  49. '#type' => 'textfield',
  50. '#title' => t('Height width'),
  51. '#default_value' => $this->getSetting('cell_height'),
  52. '#required' => TRUE,
  53. '#description' => t('Height of each individual color cell.'),
  54. );
  55. $element['cell_margin'] = array(
  56. '#type' => 'textfield',
  57. '#title' => t('Cell margin'),
  58. '#default_value' => $this->getSetting('cell_margin'),
  59. '#required' => TRUE,
  60. '#description' => t('Margin of each individual color cell.'),
  61. );
  62. $element['box_width'] = array(
  63. '#type' => 'textfield',
  64. '#title' => t('Box width'),
  65. '#default_value' => $this->getSetting('box_width'),
  66. '#required' => TRUE,
  67. '#description' => t('Width of the color display box.'),
  68. );
  69. $element['box_height'] = array(
  70. '#type' => 'textfield',
  71. '#title' => t('Box height'),
  72. '#default_value' => $this->getSetting('box_height'),
  73. '#required' => TRUE,
  74. '#description' => t('Height of the color display box.'),
  75. );
  76. $element['columns'] = array(
  77. '#type' => 'textfield',
  78. '#title' => t('Columns number'),
  79. '#default_value' => $this->getSetting('columns'),
  80. '#required' => TRUE,
  81. '#description' => t('Number of columns to display. Color order may look strange if this is altered.'),
  82. );
  83. return $element;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function settingsSummary() {
  89. $summary = [];
  90. $cell_width = $this->getSetting('cell_width');
  91. $cell_height = $this->getSetting('cell_height');
  92. $cell_margin = $this->getSetting('cell_margin');
  93. $box_width = $this->getSetting('box_width');
  94. $box_height = $this->getSetting('box_height');
  95. $columns = $this->getSetting('columns');
  96. if (!empty($cell_width)) {
  97. $summary[] = t('Cell width: @cell_width', array('@cell_width' => $cell_width));
  98. }
  99. if (!empty($cell_height)) {
  100. $summary[] = t('Cell height: @cell_height', array('@cell_height' => $cell_height));
  101. }
  102. if (!empty($cell_margin)) {
  103. $summary[] = t('Cell margin: @cell_margin', array('@cell_margin' => $cell_margin));
  104. }
  105. if (!empty($box_width)) {
  106. $summary[] = t('Box width: @box_width', array('@box_width' => $box_width));
  107. }
  108. if (!empty($box_height)) {
  109. $summary[] = t('Box height: @box_height', array('@box_height' => $box_height));
  110. }
  111. if (!empty($columns)) {
  112. $summary[] = t('Columns: @columns', array('@columns' => $columns));
  113. }
  114. if (empty($summary)) {
  115. $summary[] = t('No placeholder');
  116. }
  117. return $summary;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  123. // We are nesting some sub-elements inside the parent, so we need a wrapper.
  124. // We also need to add another #title attribute at the top level for ease in
  125. // identifying this item in error messages. We do not want to display this
  126. // title because the actual title display is handled at a higher level by
  127. // the Field module.
  128. //$element['#theme_wrappers'] = array('color_field_widget_grid');
  129. $element['#attached']['library'][] = 'color_field/color-field-widget-grid';
  130. // Set Drupal settings.
  131. $settings = $this->getSettings();
  132. $element['#attached']['drupalSettings']['color_field']['color_field_widget_grid'] = $settings;
  133. // Prepare color.
  134. $color = NULL;
  135. if (isset($items[$delta]->color)) {
  136. $color = $items[$delta]->color;
  137. if (substr($color, 0, 1) !== '#') {
  138. $color = '#' . $color;
  139. }
  140. }
  141. $element['color'] = array(
  142. '#title' => t('Color'),
  143. '#type' => 'textfield',
  144. '#maxlength' => 7,
  145. '#size' => 7,
  146. '#required' => $element['#required'],
  147. '#default_value' => $color,
  148. '#attributes' => array('class' => array('js-color-field-widget-grid__color')),
  149. );
  150. if ($this->getFieldSetting('opacity')) {
  151. $element['color']['#prefix'] = '<div class="container-inline">';
  152. $element['opacity'] = array(
  153. '#title' => t('Opacity'),
  154. '#type' => 'textfield',
  155. '#maxlength' => 4,
  156. '#size' => 4,
  157. '#required' => $element['#required'],
  158. '#default_value' => isset($items[$delta]->opacity) ? $items[$delta]->opacity : NULL,
  159. '#suffix' => '</div>',
  160. );
  161. }
  162. return $element;
  163. }
  164. }