Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldWidget / EmailDefaultWidget.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Render\Element\Email;
9
10 /**
11  * Plugin implementation of the 'email_default' widget.
12  *
13  * @FieldWidget(
14  *   id = "email_default",
15  *   label = @Translation("Email"),
16  *   field_types = {
17  *     "email"
18  *   }
19  * )
20  */
21 class EmailDefaultWidget extends WidgetBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public static function defaultSettings() {
27     return [
28       'size' => 60,
29       'placeholder' => '',
30     ] + parent::defaultSettings();
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function settingsForm(array $form, FormStateInterface $form_state) {
37     $element['size'] = [
38       '#type' => 'number',
39       '#title' => $this->t('Textfield size'),
40       '#default_value' => $this->getSetting('size'),
41       '#required' => TRUE,
42       '#min' => 1,
43     ];
44     $element['placeholder'] = [
45       '#type' => 'textfield',
46       '#title' => t('Placeholder'),
47       '#default_value' => $this->getSetting('placeholder'),
48       '#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.'),
49     ];
50     return $element;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function settingsSummary() {
57     $summary = [];
58
59     $placeholder = $this->getSetting('placeholder');
60     if (!empty($placeholder)) {
61       $summary[] = t('Placeholder: @placeholder', ['@placeholder' => $placeholder]);
62     }
63     else {
64       $summary[] = t('No placeholder');
65     }
66     $summary[] = t('Textfield size: @size', ['@size' => $this->getSetting('size')]);
67
68     return $summary;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
75     $element['value'] = $element + [
76       '#type' => 'email',
77       '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
78       '#placeholder' => $this->getSetting('placeholder'),
79       '#size' => $this->getSetting('size'),
80       '#maxlength' => Email::EMAIL_MAX_LENGTH,
81     ];
82     return $element;
83   }
84
85 }