5089c302f2e86cebc7313f81ed501121b4ccf733
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / field / widget.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Defines the '{{ plugin_id }}' field widget.
11  *
12  * @FieldWidget(
13  *   id = "{{ plugin_id }}",
14  *   label = @Translation("{{ plugin_label }}"),
15  *   field_types = {"string"},
16  * )
17  */
18 class {{ class }} extends WidgetBase {
19
20 {% if configurable %}
21   /**
22    * {@inheritdoc}
23    */
24   public static function defaultSettings() {
25     return [
26       'foo' => 'bar',
27     ] + parent::defaultSettings();
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function settingsForm(array $form, FormStateInterface $form_state) {
34
35     $element['foo'] = [
36       '#type' => 'textfield',
37       '#title' => $this->t('Foo'),
38       '#default_value' => $this->getSetting('foo'),
39     ];
40
41     return $element;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function settingsSummary() {
48     $summary[] = $this->t('Foo: @foo', ['@foo' => $this->getSetting('foo')]);
49     return $summary;
50   }
51
52 {% endif %}
53   /**
54    * {@inheritdoc}
55    */
56   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
57
58     $element['value'] = $element + [
59       '#type' => 'textfield',
60       '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
61     ];
62
63     return $element;
64   }
65
66 }