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