Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldType / UriItem.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\Core\TypedData\DataDefinition;
8
9 /**
10  * Defines the 'uri' entity field type.
11  *
12  * URIs are not length limited by RFC 2616, but we need to provide a sensible
13  * default. There is a de-facto limit of 2000 characters in browsers and other
14  * implementors, so we go with 2048.
15  *
16  * @FieldType(
17  *   id = "uri",
18  *   label = @Translation("URI"),
19  *   description = @Translation("An entity field containing a URI."),
20  *   no_ui = TRUE,
21  *   default_formatter = "uri_link",
22  *   default_widget = "uri",
23  * )
24  */
25 class UriItem extends StringItem {
26
27   /**
28    * {@inheritdoc}
29    */
30   public static function defaultStorageSettings() {
31     $storage_settings = parent::defaultStorageSettings();
32     // is_ascii doesn't make sense for URIs.
33     unset($storage_settings['is_ascii']);
34     $storage_settings['max_length'] = 2048;
35     return $storage_settings;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
42     $properties['value'] = DataDefinition::create('uri')
43       ->setLabel(t('URI value'))
44       ->setSetting('case_sensitive', $field_definition->getSetting('case_sensitive'))
45       ->setRequired(TRUE);
46
47     return $properties;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public static function schema(FieldStorageDefinitionInterface $field_definition) {
54     return [
55       'columns' => [
56         'value' => [
57           'type' => 'varchar',
58           'length' => (int) $field_definition->getSetting('max_length'),
59           'binary' => $field_definition->getSetting('case_sensitive'),
60         ],
61       ],
62     ];
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function isEmpty() {
69     $value = $this->getValue();
70     if (!isset($value['value']) || $value['value'] === '') {
71       return TRUE;
72     }
73     return parent::isEmpty();
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
80     $values = parent::generateSampleValue($field_definition);
81     $suffix_length = $field_definition->getSetting('max_length') - 7;
82     foreach ($values as $key => $value) {
83       $values[$key] = 'http://' . mb_substr($value, 0, $suffix_length);
84     }
85     return $values;
86   }
87
88 }