View source
<?php
namespace Drupal\color_field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class ColorFieldDefaultWidget extends WidgetBase {
public static function defaultSettings() {
return array(
'placeholder_color' => '',
'placeholder_opacity' => '',
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['placeholder_color'] = array(
'#type' => 'textfield',
'#title' => t('Color placeholder'),
'#default_value' => $this
->getSetting('placeholder_color'),
'#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
);
$element['placeholder_opacity'] = array(
'#type' => 'textfield',
'#title' => t('Opacity placeholder'),
'#default_value' => $this
->getSetting('placeholder_opacity'),
'#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
);
return $element;
}
public function settingsSummary() {
$summary = array();
$placeholder_color = $this
->getSetting('placeholder_color');
$placeholder_opacity = $this
->getSetting('placeholder_opacity');
if (!empty($placeholder_color)) {
$summary[] = t('Color placeholder: @placeholder_color', array(
'@placeholder_color' => $placeholder_color,
));
}
if (!empty($placeholder_opacity)) {
$summary[] = t('Opacity placeholder: @placeholder_opacity', array(
'@placeholder_opacity' => $placeholder_opacity,
));
}
if (empty($summary)) {
$summary[] = t('No placeholder');
}
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['color'] = array(
'#title' => t('Color'),
'#type' => 'textfield',
'#maxlength' => 6,
'#size' => 6,
'#required' => $element['#required'],
'#default_value' => isset($items[$delta]->color) ? $items[$delta]->color : NULL,
);
if ($this
->getFieldSetting('opacity')) {
$element['color']['#prefix'] = '<div class="container-inline">';
$element['opacity'] = array(
'#title' => t('Opacity'),
'#type' => 'textfield',
'#maxlength' => 3,
'#size' => 3,
'#required' => $element['#required'],
'#default_value' => isset($items[$delta]->opacity) ? $items[$delta]->opacity : NULL,
'#suffix' => '</div>',
);
}
return $element;
}
}