123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * @file
- * Contains Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldWidgetBox.
- */
- namespace Drupal\color_field\Plugin\Field\FieldWidget;
- use Drupal\Core\Field\FieldItemListInterface;
- use Drupal\Core\Field\WidgetBase;
- use Drupal\Core\Form\FormStateInterface;
- /**
- * Plugin implementation of the color_field box widget.
- *
- * @FieldWidget(
- * id = "color_field_widget_box",
- * module = "color_field",
- * label = @Translation("Color boxes"),
- * field_types = {
- * "color_field_type"
- * }
- * )
- */
- class ColorFieldWidgetBox extends WidgetBase {
- /**
- * {@inheritdoc}
- */
- public static function defaultSettings() {
- return array(
- '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();
- }
- /**
- * {@inheritdoc}
- */
- public function settingsForm(array $form, FormStateInterface $form_state) {
- $element['default_colors'] = array(
- '#type' => 'textarea',
- '#title' => t('Default colors'),
- '#default_value' => $this->getSetting('default_colors'),
- '#required' => TRUE,
- '#description' => t('Default colors for pre-selected color boxes'),
- );
- return $element;
- }
- /**
- * {@inheritdoc}
- */
- public function settingsSummary() {
- $summary = [];
- $default_colors = $this->getSetting('default_colors');
- if (!empty($default_colors)) {
- preg_match_all("/#[0-9a-fA-F]{6}/", $default_colors, $default_colors, PREG_SET_ORDER);
- foreach ($default_colors as $color) {
- $colors = $color[0];
- $summary[] = $colors;
- }
- }
- if (empty($summary)) {
- $summary[] = t('No default colors');
- }
- return $summary;
- }
- /**
- * {@inheritdoc}
- */
- public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
- // We are nesting some sub-elements inside the parent, so we need a wrapper.
- // We also need to add another #title attribute at the top level for ease in
- // identifying this item in error messages. We do not want to display this
- // title because the actual title display is handled at a higher level by
- // the Field module.
- $element['#theme_wrappers'] = array('color_field_widget_box');
- $element['#attributes']['class'][] = 'container-inline';
- $element['#attached']['library'][] = 'color_field/color-field-widget-box';
- // Set Drupal settings.
- $settings = [];
- $default_colors = $this->getSetting('default_colors');
- preg_match_all("/#[0-9a-fA-F]{6}/", $default_colors, $default_colors, PREG_SET_ORDER);
- foreach ($default_colors as $color) {
- $settings['default_colors'][] = $color[0];
- }
- $element['#attached']['drupalSettings']['color_field']['color_field_widget_box']['settings'] = $settings;
- // Retrieve field label and description.
- $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' => $color,
- '#attributes' => array('class' => array('visually-hidden')),
- );
- $element['color']['#suffix'] = "<div class='color-field-widget-box-form'></div>";
- if ($this->getFieldSetting('opacity')) {
- $element['opacity'] = array(
- '#title' => t('Opacity'),
- '#type' => 'textfield',
- '#maxlength' => 4,
- '#size' => 4,
- '#default_value' => isset($items[$delta]->opacity) ? $items[$delta]->opacity : NULL,
- '#placeholder' => $this->getSetting('placeholder_opacity'),
- );
- }
- return $element;
- }
- }
|