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