link_preview-8.x-1.4/src/Plugin/Field/FieldType/LinkPreviewItem.php

src/Plugin/Field/FieldType/LinkPreviewItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
 
namespace Drupal\link_preview\Plugin\Field\FieldType;
 
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\Core\Url;
use Drupal\link_preview\LinkPreviewItemInterface;
use Drupal\Core\Form\FormStateInterface;
 
/**
 * Plugin implementation of the 'link_preview' field type.
 *
 * @FieldType(
 *   id = "link_preview",
 *   label = @Translation("Link Preview"),
 *   description = @Translation("Stores a URL string, optional varchar link text, optional preview value of link content, and optional blob of attributes to assemble a link."),
 *   default_widget = "link_preview_default",
 *   default_formatter = "link_preview",
 *   constraints = {"LinkType" = {}, "LinkAccess" = {}, "LinkExternalProtocols" = {}, "LinkNotExistingInternal" = {}}
 * )
 */
class LinkPreviewItem extends FieldItemBase implements LinkPreviewItemInterface {
 
 
  public static function defaultStorageSettings() {
    return [
      'uri_length' => 2048,
      'title_length' => 255,
    ] + parent::defaultStorageSettings();
  }
 
  /**
   * {@inheritdoc}
   */
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
    $element = [];
 
    $element['uri_length'] = [
      '#type' => 'number',
      '#title' => t('Maximum uri length'),
      '#default_value' => $this->getSetting('uri_length'),
      '#required' => TRUE,
      '#description' => t('The maximum length of the uri field in characters. This is varchar type and can have maximum row size of 65,535 bytes, which is shared among all columns in table'),
      '#min' => 2048,
      '#disabled' => $has_data,
    ];
 
    $element['title_length'] = [
      '#type' => 'number',
      '#title' => t('Maximum title length'),
      '#default_value' => $this->getSetting('title_length'),
      '#required' => TRUE,
      '#description' => t('The maximum length of the title field in characters. This is varchar type and can have maximum row size of 65,535 bytes, which is shared among all columns in table'),
      '#min' => 2048,
      '#min' => 255,
      '#disabled' => $has_data,
    ];
 
    $element += parent::storageSettingsForm($form, $form_state, $has_data);
 
    return $element;
  }
 
  /**
   * {@inheritdoc}
   *
   */
  public static function defaultFieldSettings() {
    return [
      'title' => DRUPAL_OPTIONAL,
      'link_type' => LinkPreviewItemInterface::LINK_GENERIC,
    ] + parent::defaultFieldSettings();
  }
 
  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['uri'] = DataDefinition::create('uri')
      ->setLabel(t('URI'));
 
    $properties['title'] = DataDefinition::create('string')
      ->setLabel(t('Link text'));
 
    $properties['uri_content'] = DataDefinition::create('string')
      ->setLabel(t('Text'));
 
    $properties['options'] = MapDataDefinition::create()
      ->setLabel(t('Options'));
 
    return $properties;
  }
 
  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      'columns' => [
        'uri' => [
          'description' => 'The URI of the link.',
          'type' => 'varchar',
          'length' =>  $field_definition->getSetting('uri_length'),
        ],
        'title' => [
          'description' => 'The link text.',
          'type' => 'varchar',
          'length' =>  $field_definition->getSetting('title_length'),
        ],
        'uri_content' => [
          'description' => 'The content of uri.',
          'type' => 'text',
          'size' => 'big',
        ],
        'options' => [
          'description' => 'Serialized array of options for the link.',
          'type' => 'blob',
          'size' => 'big',
          'serialize' => TRUE,
        ],
      ],
      'indexes' => [
        'uri' => [['uri', 30]],
      ],
    ];
  }
 
  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('uri')->getValue();
    return $value === NULL || $value === '';
  }
 
  /**
   * {@inheritdoc}
   */
  public function isExternal() {
    return $this->getUrl()->isExternal();
  }
 
  /**
   * {@inheritdoc}
   */
  public static function mainPropertyName() {
    return 'uri';
  }
 
  /**
   * {@inheritdoc}
   */
  public function getUrl() {
    return Url::fromUri($this->uri, (array) $this->options);
  }
 
  /**
   * {@inheritdoc}
   */
  public function setValue($values, $notify = TRUE) {
    // Treat the values as property value of the main property, if no array is
    // given.
    if (isset($values) && !is_array($values)) {
      $values = [static::mainPropertyName() => $values];
    }
    if (isset($values)) {
      $values += [
        'options' => [],
      ];
    }
    // Unserialize the values, this is deprecated as the storage takes care of
    // this, options must not be passed as a string anymore.
    if (is_string($values['options'])) {
      @trigger_error('Support for passing options as a serialized string is deprecated in 8.7.0 and will be removed before Drupal 9.0.0. Pass them as an array instead. See https://www.drupal.org/node/2961643.', E_USER_DEPRECATED);
      $values['options'] = unserialize($values['options'], ['allowed_classes' => FALSE]);
    }
    parent::setValue($values, $notify);
  }
 
  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
    $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
    $constraints = parent::getConstraints();
 
    $settings = $this->getSettings();
    $label = $this->getFieldDefinition()->getLabel();
 
      $uri = $settings['uri_length'];
      $constraints[] = $constraint_manager->create('ComplexData', [
        'uri' => [
          'Length' => [
            'max' => $uri,
            'minMessage' => t('%name: may not be longer than %uri.', ['%name' => $label, '%uri' => $uri]),
          ],
        ],
      ]);
   
 
      $title = $settings['title_length'];
      $constraints[] = $constraint_manager->create('ComplexData', [
        'title' => [
          'Length' => [
            'max' => $title,
            'maxMessage' => t('%name: may not be longer than %max.', ['%name' => $label, '%title' => $title]),
          ],
        ],
      ]);
   
 
    return $constraints;
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc