View source
<?php
namespace Drupal\color_field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class ColorFieldWidgetGrid extends ColorFieldWidgetBase {
public static function defaultSettings() {
return [
'cell_width' => 10,
'cell_height' => 10,
'cell_margin' => 1,
'box_width' => 115,
'box_height' => 20,
'columns' => 16,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['cell_width'] = [
'#type' => 'textfield',
'#title' => $this
->t('Cell width'),
'#default_value' => $this
->getSetting('cell_width'),
'#required' => TRUE,
'#description' => $this
->t('Width of each individual color cell.'),
];
$element['cell_height'] = [
'#type' => 'textfield',
'#title' => $this
->t('Height width'),
'#default_value' => $this
->getSetting('cell_height'),
'#required' => TRUE,
'#description' => $this
->t('Height of each individual color cell.'),
];
$element['cell_margin'] = [
'#type' => 'textfield',
'#title' => $this
->t('Cell margin'),
'#default_value' => $this
->getSetting('cell_margin'),
'#required' => TRUE,
'#description' => $this
->t('Margin of each individual color cell.'),
];
$element['box_width'] = [
'#type' => 'textfield',
'#title' => $this
->t('Box width'),
'#default_value' => $this
->getSetting('box_width'),
'#required' => TRUE,
'#description' => $this
->t('Width of the color display box.'),
];
$element['box_height'] = [
'#type' => 'textfield',
'#title' => $this
->t('Box height'),
'#default_value' => $this
->getSetting('box_height'),
'#required' => TRUE,
'#description' => $this
->t('Height of the color display box.'),
];
$element['columns'] = [
'#type' => 'textfield',
'#title' => $this
->t('Columns number'),
'#default_value' => $this
->getSetting('columns'),
'#required' => TRUE,
'#description' => $this
->t('Number of columns to display. Color order may look strange if this is altered.'),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$cell_width = $this
->getSetting('cell_width');
$cell_height = $this
->getSetting('cell_height');
$cell_margin = $this
->getSetting('cell_margin');
$box_width = $this
->getSetting('box_width');
$box_height = $this
->getSetting('box_height');
$columns = $this
->getSetting('columns');
if (!empty($cell_width)) {
$summary[] = $this
->t('Cell width: @cell_width', [
'@cell_width' => $cell_width,
]);
}
if (!empty($cell_height)) {
$summary[] = $this
->t('Cell height: @cell_height', [
'@cell_height' => $cell_height,
]);
}
if (!empty($cell_margin)) {
$summary[] = $this
->t('Cell margin: @cell_margin', [
'@cell_margin' => $cell_margin,
]);
}
if (!empty($box_width)) {
$summary[] = $this
->t('Box width: @box_width', [
'@box_width' => $box_width,
]);
}
if (!empty($box_height)) {
$summary[] = $this
->t('Box height: @box_height', [
'@box_height' => $box_height,
]);
}
if (!empty($columns)) {
$summary[] = $this
->t('Columns: @columns', [
'@columns' => $columns,
]);
}
if (empty($summary)) {
$summary[] = $this
->t('No placeholder');
}
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-grid';
$settings = $this
->getSettings();
$element['#attached']['drupalSettings']['color_field']['color_field_widget_grid'][$element['#uid']] = $settings;
$element['color']['#attributes']['class'][] = 'js-color-field-widget-grid__color';
$element['color']['#attributes']['id'] = $element['#uid'];
$element['color']['#wrapper_attributes']['class'][] = 'clearfix';
return $element;
}
}