3 namespace Drupal\link\Plugin\Field\FieldType;
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;
13 use Drupal\link\LinkItemInterface;
16 * Plugin implementation of the 'link' field type.
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" = {}}
27 class LinkItem extends FieldItemBase implements LinkItemInterface {
32 public static function defaultFieldSettings() {
34 'title' => DRUPAL_OPTIONAL,
35 'link_type' => LinkItemInterface::LINK_GENERIC,
36 ] + parent::defaultFieldSettings();
42 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
43 $properties['uri'] = DataDefinition::create('uri')
46 $properties['title'] = DataDefinition::create('string')
47 ->setLabel(t('Link text'));
49 $properties['options'] = MapDataDefinition::create()
50 ->setLabel(t('Options'));
58 public static function schema(FieldStorageDefinitionInterface $field_definition) {
62 'description' => 'The URI of the link.',
67 'description' => 'The link text.',
72 'description' => 'Serialized array of options for the link.',
79 'uri' => [['uri', 30]],
87 public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
90 $element['link_type'] = [
92 '#title' => t('Allowed link type'),
93 '#default_value' => $this->getSetting('link_type'),
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'),
101 $element['title'] = [
103 '#title' => t('Allow link text'),
104 '#default_value' => $this->getSetting('title'),
106 DRUPAL_DISABLED => t('Disabled'),
107 DRUPAL_OPTIONAL => t('Optional'),
108 DRUPAL_REQUIRED => t('Required'),
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);
126 switch ($field_definition->getSetting('title')) {
127 case DRUPAL_DISABLED:
128 $values['title'] = '';
130 case DRUPAL_REQUIRED:
131 $values['title'] = $random->sentences(4);
133 case DRUPAL_OPTIONAL:
134 // In case of optional title, randomize its generation.
135 $values['title'] = mt_rand(0, 1) ? $random->sentences(4) : '';
138 $values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (count($tlds) - 1))];
141 $values['uri'] = 'base:' . $random->name(mt_rand(1, 64));
149 public function isEmpty() {
150 $value = $this->get('uri')->getValue();
151 return $value === NULL || $value === '';
157 public function isExternal() {
158 return $this->getUrl()->isExternal();
164 public static function mainPropertyName() {
171 public function getUrl() {
172 return Url::fromUri($this->uri, (array) $this->options);
178 public function setValue($values, $notify = TRUE) {
179 // Treat the values as property value of the main property, if no array is
181 if (isset($values) && !is_array($values)) {
182 $values = [static::mainPropertyName() => $values];
184 if (isset($values)) {
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']);
196 parent::setValue($values, $notify);