ColorFieldType.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\color_field\Plugin\Field\FieldType\ColorFieldType.
  5. */
  6. namespace Drupal\color_field\Plugin\Field\FieldType;
  7. use Drupal\Core\Field\FieldDefinitionInterface;
  8. use Drupal\Core\Field\FieldItemBase;
  9. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  10. use Drupal\Core\Form\FormStateInterface;
  11. use Drupal\Core\StringTranslation\TranslatableMarkup;
  12. use Drupal\Core\TypedData\DataDefinition;
  13. use Drupal\color_field\ColorHex;
  14. /**
  15. * Plugin implementation of the 'color_type' field type.
  16. *
  17. * @FieldType(
  18. * id = "color_field_type",
  19. * label = @Translation("Color"),
  20. * description = @Translation("Create and store color value."),
  21. * default_widget = "color_field_widget_default",
  22. * default_formatter = "color_field_formatter_text"
  23. * )
  24. */
  25. class ColorFieldType extends FieldItemBase {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function defaultFieldSettings() {
  30. return array(
  31. 'opacity' => TRUE,
  32. ) + parent::defaultFieldSettings();
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function defaultStorageSettings() {
  38. return array(
  39. 'format' => '#HEXHEX',
  40. ) + parent::defaultStorageSettings();
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
  46. $element = [];
  47. $element['format'] = array(
  48. '#type' => 'select',
  49. '#title' => t('Format storage'),
  50. '#description' => t('Choose how to store the color.'),
  51. '#default_value' => $this->getSetting('format'),
  52. '#options' => array(
  53. '#HEXHEX' => t('#123ABC'),
  54. 'HEXHEX' => t('123ABC'),
  55. '#hexhex' => t('#123abc'),
  56. 'hexhex' => t('123abc'),
  57. ),
  58. );
  59. $element += parent::storageSettingsForm($form, $form_state, $has_data);
  60. return $element;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  66. $element = [];
  67. $element['opacity'] = array(
  68. '#type' => 'checkbox',
  69. '#title' => t('Record opacity'),
  70. '#description' => t('Whether or not to record.'),
  71. '#default_value' => $this->getSetting('opacity'),
  72. );
  73. return $element;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public static function schema(FieldStorageDefinitionInterface $field_definition) {
  79. $format = $field_definition->getSetting('format');
  80. $color_length = isset($format) ? strlen($format) : 7 ;
  81. return array(
  82. 'columns' => array(
  83. 'color' => array(
  84. 'description' => 'The color value',
  85. 'type' => 'varchar',
  86. 'length' => $color_length,
  87. 'not null' => FALSE,
  88. ),
  89. 'opacity' => array(
  90. 'description' => 'The opacity/alphavalue property',
  91. 'type' => 'float',
  92. 'size' => 'tiny',
  93. 'not null' => FALSE,
  94. ),
  95. ),
  96. 'indexes' => array(
  97. 'color' => array('color'),
  98. ),
  99. );
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
  105. // Prevent early t() calls by using the TranslatableMarkup.
  106. $properties['color'] = DataDefinition::create('string')
  107. ->setLabel(new TranslatableMarkup('Color'));
  108. $properties['opacity'] = DataDefinition::create('float')
  109. ->setLabel(new TranslatableMarkup('Opacity'));
  110. return $properties;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function isEmpty() {
  116. $value = $this->get('color')->getValue();
  117. return $value === NULL || $value === '';
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function getConstraints() {
  123. $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
  124. $constraints = parent::getConstraints();
  125. $label = $this->getFieldDefinition()->getLabel();
  126. $constraints[] = $constraint_manager->create('ComplexData', array(
  127. 'color' => array(
  128. 'Regex' => array(
  129. 'pattern' => '/^#?(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})$/i',
  130. )
  131. ),
  132. ));
  133. if ($opacity = $this->getSetting('opacity')) {
  134. $min = 0;
  135. $constraints[] = $constraint_manager->create('ComplexData', array(
  136. 'opacity' => array(
  137. 'Range' => array(
  138. 'min' => $min,
  139. 'minMessage' => t('%name: the opacity may be no less than %min.', array('%name' => $label, '%min' => $min)),
  140. )
  141. ),
  142. ));
  143. $max = 1;
  144. $constraints[] = $constraint_manager->create('ComplexData', array(
  145. 'opacity' => array(
  146. 'Range' => array(
  147. 'max' => $max,
  148. 'maxMessage' => t('%name: the opacity may be no greater than %max.', array('%name' => $label, '%max' => $max)),
  149. )
  150. ),
  151. ));
  152. }
  153. return $constraints;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
  159. $settings = $field_definition->getSettings();
  160. if ($format = $settings['format']) {
  161. switch ($format) {
  162. case '#HEXHEX':
  163. $values['color'] = '#111AAA';
  164. break;
  165. case 'HEXHEX':
  166. $values['color'] = '111111';
  167. break;
  168. case '#hexhex':
  169. $values['color'] = '#111aaa';
  170. break;
  171. case 'hexhex':
  172. $values['color'] = '111111';
  173. break;
  174. }
  175. }
  176. $values['opacity'] = 1;
  177. return $values;
  178. }
  179. /**
  180. * {@inheritdoc}
  181. */
  182. public function preSave() {
  183. parent::preSave();
  184. if ($format = $this->getSetting('format')) {
  185. $color = $this->color;
  186. // Clean up data and format it.
  187. $color = trim($color);
  188. if (substr($color, 0, 1) === '#') {
  189. $color = substr($color, 1);
  190. }
  191. switch ($format) {
  192. case '#HEXHEX':
  193. $color = '#' . strtoupper($color);
  194. break;
  195. case 'HEXHEX':
  196. $color = strtoupper($color);
  197. break;
  198. case '#hexhex':
  199. $color = '#' . strtolower($color);
  200. break;
  201. case 'hexhex':
  202. $color = strtolower($color);
  203. break;
  204. }
  205. $this->color = $color;
  206. }
  207. }
  208. }