Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / link / src / Plugin / Field / FieldType / LinkItem.php
1 <?php
2
3 namespace Drupal\link\Plugin\Field\FieldType;
4
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FieldItemBase;
8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\TypedData\DataDefinition;
11 use Drupal\Core\TypedData\MapDataDefinition;
12 use Drupal\Core\Url;
13 use Drupal\link\LinkItemInterface;
14
15 /**
16  * Plugin implementation of the 'link' field type.
17  *
18  * @FieldType(
19  *   id = "link",
20  *   label = @Translation("Link"),
21  *   description = @Translation("Stores a URL string, optional varchar link text, and optional blob of attributes to assemble a link."),
22  *   default_widget = "link_default",
23  *   default_formatter = "link",
24  *   constraints = {"LinkType" = {}, "LinkAccess" = {}, "LinkExternalProtocols" = {}, "LinkNotExistingInternal" = {}}
25  * )
26  */
27 class LinkItem extends FieldItemBase implements LinkItemInterface {
28
29   /**
30    * {@inheritdoc}
31    */
32   public static function defaultFieldSettings() {
33     return [
34       'title' => DRUPAL_OPTIONAL,
35       'link_type' => LinkItemInterface::LINK_GENERIC,
36     ] + parent::defaultFieldSettings();
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
43     $properties['uri'] = DataDefinition::create('uri')
44       ->setLabel(t('URI'));
45
46     $properties['title'] = DataDefinition::create('string')
47       ->setLabel(t('Link text'));
48
49     $properties['options'] = MapDataDefinition::create()
50       ->setLabel(t('Options'));
51
52     return $properties;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function schema(FieldStorageDefinitionInterface $field_definition) {
59     return [
60       'columns' => [
61         'uri' => [
62           'description' => 'The URI of the link.',
63           'type' => 'varchar',
64           'length' => 2048,
65         ],
66         'title' => [
67           'description' => 'The link text.',
68           'type' => 'varchar',
69           'length' => 255,
70         ],
71         'options' => [
72           'description' => 'Serialized array of options for the link.',
73           'type' => 'blob',
74           'size' => 'big',
75           'serialize' => TRUE,
76         ],
77       ],
78       'indexes' => [
79         'uri' => [['uri', 30]],
80       ],
81     ];
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
88     $element = [];
89
90     $element['link_type'] = [
91       '#type' => 'radios',
92       '#title' => t('Allowed link type'),
93       '#default_value' => $this->getSetting('link_type'),
94       '#options' => [
95         static::LINK_INTERNAL => t('Internal links only'),
96         static::LINK_EXTERNAL => t('External links only'),
97         static::LINK_GENERIC => t('Both internal and external links'),
98       ],
99     ];
100
101     $element['title'] = [
102       '#type' => 'radios',
103       '#title' => t('Allow link text'),
104       '#default_value' => $this->getSetting('title'),
105       '#options' => [
106         DRUPAL_DISABLED => t('Disabled'),
107         DRUPAL_OPTIONAL => t('Optional'),
108         DRUPAL_REQUIRED => t('Required'),
109       ],
110     ];
111
112     return $element;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
119     $random = new Random();
120     if ($field_definition->getItemDefinition()->getSetting('link_type') & LinkItemInterface::LINK_EXTERNAL) {
121       // Set of possible top-level domains.
122       $tlds = ['com', 'net', 'gov', 'org', 'edu', 'biz', 'info'];
123       // Set random length for the domain name.
124       $domain_length = mt_rand(7, 15);
125
126       switch ($field_definition->getSetting('title')) {
127         case DRUPAL_DISABLED:
128           $values['title'] = '';
129           break;
130         case DRUPAL_REQUIRED:
131           $values['title'] = $random->sentences(4);
132           break;
133         case DRUPAL_OPTIONAL:
134           // In case of optional title, randomize its generation.
135           $values['title'] = mt_rand(0, 1) ? $random->sentences(4) : '';
136           break;
137       }
138       $values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (count($tlds) - 1))];
139     }
140     else {
141       $values['uri'] = 'base:' . $random->name(mt_rand(1, 64));
142     }
143     return $values;
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function isEmpty() {
150     $value = $this->get('uri')->getValue();
151     return $value === NULL || $value === '';
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public function isExternal() {
158     return $this->getUrl()->isExternal();
159   }
160
161   /**
162    * {@inheritdoc}
163    */
164   public static function mainPropertyName() {
165     return 'uri';
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function getUrl() {
172     return Url::fromUri($this->uri, (array) $this->options);
173   }
174
175   /**
176    * {@inheritdoc}
177    */
178   public function setValue($values, $notify = TRUE) {
179     // Treat the values as property value of the main property, if no array is
180     // given.
181     if (isset($values) && !is_array($values)) {
182       $values = [static::mainPropertyName() => $values];
183     }
184     if (isset($values)) {
185       $values += [
186         'options' => [],
187       ];
188     }
189     // Unserialize the values.
190     // @todo The storage controller should take care of this, see
191     //   SqlContentEntityStorage::loadFieldItems, see
192     //   https://www.drupal.org/node/2414835
193     if (is_string($values['options'])) {
194       $values['options'] = unserialize($values['options']);
195     }
196     parent::setValue($values, $notify);
197   }
198
199 }