View source
<?php
namespace Drupal\jquery_colorpicker\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\jquery_colorpicker\Service\JQueryColorpickerServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class JQueryColorpickerDefaultWidget extends WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface {
protected $JQueryColorpickerService;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, JQueryColorpickerServiceInterface $jQueryColorpickerService) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->JQueryColorpickerService = $jQueryColorpickerService;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('jquery_colorpicker.service'));
}
public static function defaultSettings() {
return [
'color' => 'FFFFFF',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['color'] = [
'#type' => 'textfield',
'#field_prefix' => '#',
'#title' => t('Color'),
'#default_value' => $this
->getSetting('color'),
'#required' => TRUE,
'#element_validate' => [
[
$this,
'settingsFormValidate',
],
],
];
return $element;
}
public function settingsFormValidate($element, FormStateInterface $form_state) {
$color = $form_state
->getValue($element['#parents']);
$results = $this->JQueryColorpickerService
->validateColor($color);
$form_state
->setValueForElement($element, $results['color']);
if (isset($results['error'])) {
$form_state
->setError($element, $results['error']);
}
}
public function settingsSummary() {
$summary = [];
$summary[] = t('Default Color: @color', [
'@color' => '#' . $this
->getSetting('color'),
]);
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
$element['value'] = $element + [
'#type' => 'jquery_colorpicker',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : 'FFFFFF',
'#description' => $element['#description'],
'#cardinality' => $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality(),
];
return $element;
}
}