View source
<?php
namespace Drupal\color_field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class ColorFieldWidgetBox extends ColorFieldWidgetBase {
public static function defaultSettings() {
return [
'default_colors' => '
#ac725e,#d06b64,#f83a22,#fa573c,#ff7537,#ffad46
#42d692,#16a765,#7bd148,#b3dc6c,#fbe983
#92e1c0,#9fe1e7,#9fc6e7,#4986e7,#9a9cff
#b99aff,#c2c2c2,#cabdbf,#cca6ac,#f691b2
#cd74e6,#a47ae2',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['default_colors'] = [
'#type' => 'textarea',
'#title' => $this
->t('Default colors'),
'#default_value' => $this
->getSetting('default_colors'),
'#required' => TRUE,
'#element_validate' => [
[
$this,
'settingsColorValidate',
],
],
'#description' => $this
->t('Default colors for pre-selected color boxes. Enter as 6 digit upper case hex - such as #FF0000.'),
];
return $element;
}
public function settingsColorValidate(array $element, FormStateInterface $form_state) {
$default_colors = $form_state
->getValue($element['#parents']);
$colors = '';
if (!empty($default_colors)) {
preg_match_all("/#[0-9a-f]{6}/i", $default_colors, $default_colors, PREG_SET_ORDER);
foreach ($default_colors as $color) {
if (!empty($colors)) {
$colors .= ',';
}
$colors .= strtolower($color[0]);
}
}
$form_state
->setValue($element['#parents'], $colors);
}
public function settingsSummary() {
$summary = [];
$default_colors = $this
->getSetting('default_colors');
if (!empty($default_colors)) {
preg_match_all("/#[0-9A-F]{6}/i", $default_colors, $default_colors, PREG_SET_ORDER);
foreach ($default_colors as $color) {
$colors = $color[0];
$summary[] = $colors;
}
}
if (empty($summary)) {
$summary[] = $this
->t('No default colors');
}
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
if ($element['color']['#default_value']) {
$element['color']['#default_value'] = strtoupper($element['color']['#default_value']);
if (strlen($element['color']['#default_value']) === 6) {
$element['color']['#default_value'] = '#' . $element['color']['#default_value'];
}
}
elseif ($element['#required']) {
$element['color']['#attributes']['class'][] = 'color_field_widget_box__color';
}
$element['#attached']['library'][] = 'color_field/color-field-widget-box';
$settings[$element['#uid']] = [
'required' => $this->fieldDefinition
->isRequired(),
];
$default_colors = $this
->getSetting('default_colors');
preg_match_all("/#[0-9A-F]{6}/i", $default_colors, $default_colors, PREG_SET_ORDER);
foreach ($default_colors as $color) {
$settings[$element['#uid']]['palette'][] = $color[0];
}
$element['#attached']['drupalSettings']['color_field']['color_field_widget_box']['settings'] = $settings;
$element['color']['#suffix'] = "<div class='color-field-widget-box-form' id='" . $element['#uid'] . "'></div>";
return $element;
}
}