View source
<?php
namespace Drupal\color_field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class ColorFieldWidgetSpectrum extends ColorFieldWidgetBase {
public static function defaultSettings() {
return [
'show_input' => FALSE,
'show_palette' => FALSE,
'palette' => '',
'show_palette_only' => FALSE,
'show_buttons' => FALSE,
'cancel_text' => 'Cancel',
'choose_text' => 'Choose',
'allow_empty' => FALSE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['show_input'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show Input'),
'#default_value' => $this
->getSetting('show_input'),
'#description' => $this
->t('Allow free form typing.'),
];
$element['show_palette'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show Palette'),
'#default_value' => $this
->getSetting('show_palette'),
'#description' => $this
->t('Show or hide Palette in Spectrum Widget'),
];
$element['palette'] = [
'#type' => 'textarea',
'#title' => $this
->t('Color Palette'),
'#default_value' => $this
->getSetting('palette'),
'#description' => $this
->t('Selectable color palette to accompany the Spectrum Widget. Most possible color values will work - including rgba, rgb, hex, web color names, hsl, hsv. Separate values with a comma, and group them with square brackets - ensure one group per line. Ex: <br> ["#fff","#aaa","#f00","#00f"],<br>["#414141","#242424","#0a8db9"]'),
'#states' => [
'visible' => [
':input[name="fields[' . $this->fieldDefinition
->getName() . '][settings_edit_form][settings][show_palette]"]' => [
'checked' => TRUE,
],
],
],
];
$element['show_palette_only'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show Palette Only'),
'#default_value' => $this
->getSetting('show_palette_only'),
'#description' => $this
->t('Only show the palette in Spectrum Widget and nothing else'),
'#states' => [
'visible' => [
':input[name="fields[' . $this->fieldDefinition
->getName() . '][settings_edit_form][settings][show_palette]"]' => [
'checked' => TRUE,
],
],
],
];
$element['show_buttons'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show Buttons'),
'#default_value' => $this
->getSetting('show_buttons'),
'#description' => $this
->t('Add Cancel/Confirm Button.'),
];
$element['cancel_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Cancel button text'),
'#description' => $this
->t('Leave empty to stay default.'),
'#default_value' => $this
->getSetting('cancel_text'),
'#states' => [
'visible' => [
':input[name="fields[' . $this->fieldDefinition
->getName() . '][settings_edit_form][settings][show_buttons]"]' => [
'checked' => TRUE,
],
],
],
];
$element['choose_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Choose button text'),
'#description' => $this
->t('Leave empty to stay default.'),
'#default_value' => $this
->getSetting('choose_text'),
'#states' => [
'visible' => [
':input[name="fields[' . $this->fieldDefinition
->getName() . '][settings_edit_form][settings][show_buttons]"]' => [
'checked' => TRUE,
],
],
],
];
$element['allow_empty'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow Empty'),
'#default_value' => $this
->getSetting('allow_empty'),
'#description' => $this
->t('Allow empty value.'),
];
return $element;
}
public function settingsSummary() {
$summary = [];
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['#attached']['library'][] = 'color_field/color-field-widget-spectrum';
$settings = $this
->getSettings();
$defaults = self::defaultSettings();
foreach ($settings as $key => $value) {
if (is_bool($defaults[$key])) {
$settings[$key] = boolval($value);
}
}
if (!empty($settings['palette'])) {
$settings['palette'] = str_replace([
' ',
"\n",
'"',
"'",
], '', $settings['palette']);
$rows = explode("\r", $settings['palette']);
$settings['palette'] = [];
$re = '/(rgba|hsva|hsla)[\\(\\s][0-9]*[%\\,\\s]+[0-9]*[%\\,\\s]+[0-9]*[%\\,\\s]+[0-9\\.]+[\\)\\s]*|(rgb|hsv|hsl)[\\(\\s][0-9]+[%\\,\\s]+[0-9]+[%\\,\\s]+[0-9]+[\\)\\s]*|[\\#]?[0-9a-f]+|[a-z]+/mi';
foreach ($rows as $row) {
preg_match_all($re, $row, $matches);
$settings['palette'][] = $matches[0];
}
}
$settings['show_alpha'] = (bool) $this
->getFieldSetting('opacity');
$element['#attributes']['id'] = $element['#uid'];
$element['#attributes']['class'][] = 'js-color-field-widget-spectrum';
$element['#attached']['drupalSettings']['color_field']['color_field_widget_spectrum'][$element['#uid']] = $settings;
$element['color']['#attributes']['class'][] = 'js-color-field-widget-spectrum__color';
$element['opacity']['#attributes']['class'][] = 'js-color-field-widget-spectrum__opacity';
return $element;
}
}