1 {% extends "base/class.php.twig" %}
4 \Drupal\{{ module }}\Plugin\Field\FieldWidget\{{ class_name }}.
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Plugin\Field\FieldWidget;
12 use Drupal\Core\Field\FieldItemListInterface;
13 use Drupal\Core\Field\WidgetBase;
14 use Drupal\Core\Form\FormStateInterface;
17 {% block class_declaration %}
19 * Plugin implementation of the '{{ plugin_id }}' widget.
22 * id = "{{ plugin_id }}",
23 * label = @Translation("{{ label }}"){% if field_type %},
29 * At least one field_types annotation array entry is necessary to display this formatter in the UI.
30 * ex. field_types = { "field_type" }
34 class {{ class_name }} extends WidgetBase {% endblock %}
35 {% block class_methods %}
39 public static function defaultSettings() {
43 ] + parent::defaultSettings();
49 public function settingsForm(array $form, FormStateInterface $form_state) {
54 '#title' => t('Size of textfield'),
55 '#default_value' => $this->getSetting('size'),
59 $elements['placeholder'] = [
60 '#type' => 'textfield',
61 '#title' => t('Placeholder'),
62 '#default_value' => $this->getSetting('placeholder'),
63 '#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.'),
72 public function settingsSummary() {
75 $summary[] = t('Textfield size: @size', ['@size' => $this->getSetting('size')]);
76 if (!empty($this->getSetting('placeholder'))) {
77 $summary[] = t('Placeholder: @placeholder', ['@placeholder' => $this->getSetting('placeholder')]);
86 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
87 $element['value'] = $element + [
88 '#type' => 'textfield',
89 '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
90 '#size' => $this->getSetting('size'),
91 '#placeholder' => $this->getSetting('placeholder'),
92 '#maxlength' => $this->getFieldSetting('max_length'),