3 namespace Drupal\{{ machine_name }}\Plugin\Field\FieldWidget;
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
10 * Defines the '{{ plugin_id }}' field widget.
13 * id = "{{ plugin_id }}",
14 * label = @Translation("{{ plugin_label }}"),
15 * field_types = {"string"},
18 class {{ class }} extends WidgetBase {
23 public static function defaultSettings() {
29 ] + parent::defaultSettings();
35 public function settingsForm(array $form, FormStateInterface $form_state) {
36 $settings = $this->getSettings();
40 '#title' => $this->t('Size of textfield'),
41 '#default_value' => $settings['size'],
45 $element['placeholder'] = [
46 '#type' => 'textfield',
47 '#title' => $this->t('Placeholder'),
48 '#default_value' => $settings['placeholder'],
49 '#description' => $this->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.'),
51 $element['prefix'] = [
52 '#type' => 'textfield',
53 '#title' => $this->t('Prefix'),
54 '#default_value' => $settings['prefix'],
56 $element['suffix'] = [
57 '#type' => 'textfield',
58 '#title' => $this->t('Suffix'),
59 '#default_value' => $settings['suffix'],
68 public function settingsSummary() {
69 $settings = $this->getSettings();
70 $summary[] = $this->t('Textfield size: @size', ['@size' => $settings['size']]);
72 if ($settings['placeholder']) {
73 $summary[] = $this->t('Placeholder: @placeholder', ['@placeholder' => $settings['placeholder']]);
75 if ($settings['prefix']) {
76 $summary[] = $this->t('Prefix: @prefix', ['@prefix' => $settings['prefix']]);
78 if ($settings['suffix']) {
79 $summary[] = $this->t('Suffix: @suffix', ['@suffix' => $settings['suffix']]);
88 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
90 $settings = $this->getSettings();
91 $element['value'] = $element + [
92 '#type' => 'textfield',
93 '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
94 '#size' => $settings['size'],
95 '#placeholder' => $settings['placeholder'],
96 '#maxlength' => $this->getFieldSetting('max_length'),
97 '#field_prefix' => $settings['prefix'],
98 '#field_suffix' => $settings['suffix'],