Commit a9eb516f authored by targoo's avatar targoo

Add support multiple DB format

parent f2318b68
......@@ -211,11 +211,11 @@ class ColorFieldType extends FieldItemBase {
public function preSave() {
parent::preSave();
$values = $this->values;
if ($format = $this->getSetting('format')) {
$color = $this->color;
if ($format = $this->getSettings('format')) {
// Clean up data and format it.
$color = trim($values['color']);
$color = trim($color);
if (substr($color, 0, 1) === '#') {
$color = substr($color, 1);
......@@ -223,21 +223,21 @@ class ColorFieldType extends FieldItemBase {
switch ($format) {
case '#HEXHEX':
$values['color'] = '#' . strtoupper($color);
$color = '#' . strtoupper($color);
break;
case 'HEXHEX':
$values['color'] = strtoupper($color);
$color = strtoupper($color);
break;
case '#hexhex':
$values['color'] = '#' . strtolower($color);
$color = '#' . strtolower($color);
break;
case 'hexhex':
$values['color'] = strtolower($color);
$color = strtolower($color);
break;
}
}
$this->values = $values;
$this->color = $color;
}
}
}
......@@ -104,11 +104,20 @@ class ColorFieldWidgetBox extends WidgetBase {
$element['#title'] = $this->fieldDefinition->getLabel();;
$element['#description'] = $this->fieldDefinition->getDescription();
// Prepare color.
$color = NULL;
if (isset($items[$delta]->color)) {
$color = $items[$delta]->color;
if (substr($color, 0, 1) !== '#') {
$color = '#' . $color;
}
}
$element['color'] = array(
'#maxlength' => 7,
'#size' => 7,
'#type' => 'textfield',
'#default_value' => isset($items[$delta]->color) ? $items[$delta]->color : NULL,
'#default_value' => $color,
'#attributes' => array('class' => array('visually-hidden')),
);
$element['color']['#suffix'] = "<div class='color-field-widget-box-form'></div>";
......
......@@ -152,13 +152,22 @@ class ColorFieldWidgetGrid extends WidgetBase {
$settings = $this->getSettings();
$element['#attached']['drupalSettings']['color_field']['color_field_widget_grid'] = $settings;
// Prepare color.
$color = NULL;
if (isset($items[$delta]->color)) {
$color = $items[$delta]->color;
if (substr($color, 0, 1) !== '#') {
$color = '#' . $color;
}
}
$element['color'] = array(
'#title' => t('Color'),
'#type' => 'textfield',
'#maxlength' => 7,
'#size' => 7,
'#required' => $element['#required'],
'#default_value' => isset($items[$delta]->color) ? $items[$delta]->color : NULL,
'#default_value' => $color,
'#attributes' => array('class' => array('js-color-field-widget-grid__color')),
);
......
......@@ -55,13 +55,22 @@ class ColorFieldWidgetHTML5 extends WidgetBase {
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// Prepare color.
$color = NULL;
if (isset($items[$delta]->color)) {
$color = $items[$delta]->color;
if (substr($color, 0, 1) !== '#') {
$color = '#' . $color;
}
}
$element['color'] = array(
'#title' => t('Color'),
'#type' => 'color',
'#maxlength' => 7,
'#size' => 7,
'#required' => $element['#required'],
'#default_value' => isset($items[$delta]->color) ? $items[$delta]->color : NULL,
'#default_value' => $color,
);
if ($this->getFieldSetting('opacity')) {
......
......@@ -117,12 +117,21 @@ class ColorFieldWidgetSpectrum extends WidgetBase {
$settings['show_alpha'] = $this->getFieldSetting('opacity');
$element['#attached']['drupalSettings']['color_field']['color_field_widget_spectrum'] = $settings;
// Prepare color.
$color = NULL;
if (isset($items[$delta]->color)) {
$color = $items[$delta]->color;
if (substr($color, 0, 1) !== '#') {
$color = '#' . $color;
}
}
$element['color'] = array(
'#type' => 'textfield',
'#maxlength' => 7,
'#size' => 7,
'#required' => $element['#required'],
'#default_value' => isset($items[$delta]->color) ? $items[$delta]->color : NULL,
'#default_value' => $color,
'#attributes' => array('class' => array('js-color-field-widget-spectrum__color')),
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment