Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / redirect / src / Entity / Redirect.php
1 <?php
2
3 namespace Drupal\redirect\Entity;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Component\Utility\UrlHelper;
7 use Drupal\Core\Entity\ContentEntityBase;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Field\BaseFieldDefinition;
11 use Drupal\link\LinkItemInterface;
12
13 /**
14  * The redirect entity class.
15  *
16  * @ContentEntityType(
17  *   id = "redirect",
18  *   label = @Translation("Redirect"),
19  *   bundle_label = @Translation("Redirect type"),
20  *   handlers = {
21  *     "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
22  *     "form" = {
23  *       "default" = "Drupal\redirect\Form\RedirectForm",
24  *       "delete" = "Drupal\redirect\Form\RedirectDeleteForm",
25  *       "edit" = "Drupal\redirect\Form\RedirectForm"
26  *     },
27  *     "views_data" = "Drupal\redirect\RedirectViewsData",
28  *     "storage_schema" = "\Drupal\redirect\RedirectStorageSchema"
29  *   },
30  *   base_table = "redirect",
31  *   translatable = FALSE,
32  *   admin_permission = "administer redirects",
33  *   entity_keys = {
34  *     "id" = "rid",
35  *     "label" = "redirect_source",
36  *     "uuid" = "uuid",
37  *     "bundle" = "type",
38  *     "langcode" = "language",
39  *   },
40  *   links = {
41  *     "canonical" = "/admin/config/search/redirect/edit/{redirect}",
42  *     "delete-form" = "/admin/config/search/redirect/delete/{redirect}",
43  *     "edit-form" = "/admin/config/search/redirect/edit/{redirect}",
44  *   }
45  * )
46  */
47 class Redirect extends ContentEntityBase {
48
49   /**
50    * Generates a unique hash for identification purposes.
51    *
52    * @param string $source_path
53    *   Source path of the redirect.
54    * @param array $source_query
55    *   Source query as an array.
56    * @param string $language
57    *   Redirect language.
58    *
59    * @return string
60    *   Base 64 hash.
61    */
62   public static function generateHash($source_path, array $source_query, $language) {
63     $hash = array(
64       'source' => mb_strtolower($source_path),
65       'language' => $language,
66     );
67
68     if (!empty($source_query)) {
69       $hash['source_query'] = $source_query;
70     }
71     redirect_sort_recursive($hash, 'ksort');
72     return Crypt::hashBase64(serialize($hash));
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
79     $values += array(
80       'type' => 'redirect',
81     );
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function preSave(EntityStorageInterface $storage_controller) {
88     // Get the language code directly from the field as language() might not
89     // be up to date if the language was just changed.
90     $this->set('hash', Redirect::generateHash($this->redirect_source->path, (array) $this->redirect_source->query, $this->get('language')->value));
91   }
92
93   /**
94    * Sets the redirect language.
95    *
96    * @param string $language
97    *   Language code.
98    */
99   public function setLanguage($language) {
100     $this->set('language', $language);
101   }
102
103   /**
104    * Sets the redirect status code.
105    *
106    * @param int $status_code
107    *   The redirect status code.
108    */
109   public function setStatusCode($status_code) {
110     $this->set('status_code', $status_code);
111   }
112
113   /**
114    * Gets the redirect status code.
115    *
116    * @return int
117    *   The redirect status code.
118    */
119   public function getStatusCode() {
120     return $this->get('status_code')->value;
121   }
122
123   /**
124    * Sets the redirect created datetime.
125    *
126    * @param int $datetime
127    *   The redirect created datetime.
128    */
129   public function setCreated($datetime) {
130     $this->set('created', $datetime);
131   }
132
133   /**
134    * Gets the redirect created datetime.
135    *
136    * @return int
137    *   The redirect created datetime.
138    */
139   public function getCreated() {
140     return $this->get('created')->value;
141   }
142
143   /**
144    * Sets the source URL data.
145    *
146    * @param string $path
147    *   The base url of the source.
148    * @param array $query
149    *   Query arguments.
150    */
151   public function setSource($path, array $query = array()) {
152     $this->redirect_source->set(0, ['path' => ltrim($path, '/'), 'query' => $query]);
153   }
154
155   /**
156    * Gets the source URL data.
157    *
158    * @return array
159    */
160   public function getSource() {
161     return $this->get('redirect_source')->get(0)->getValue();
162   }
163
164   /**
165    * Gets the source base URL.
166    *
167    * @return string
168    */
169   public function getSourceUrl() {
170     return $this->get('redirect_source')->get(0)->getUrl()->toString();
171   }
172
173   /**
174    * Gets the source URL path with its query.
175    *
176    * @return string
177    *   The source URL path, eventually with its query.
178    */
179   public function getSourcePathWithQuery() {
180     $path = '/' . $this->get('redirect_source')->path;
181     if ($this->get('redirect_source')->query) {
182       $path .= '?' . UrlHelper::buildQuery($this->get('redirect_source')->query);
183     }
184     return $path;
185   }
186
187   /**
188    * Gets the redirect URL data.
189    *
190    * @return array
191    *   The redirect URL data.
192    */
193   public function getRedirect() {
194     return $this->get('redirect_redirect')->get(0)->getValue();
195   }
196
197   /**
198    * Sets the redirect destination URL data.
199    *
200    * @param string $url
201    *   The base url of the redirect destination.
202    * @param array $query
203    *   Query arguments.
204    * @param array $options
205    *   The source url options.
206    */
207   public function setRedirect($url, array $query = array(), array $options = array()) {
208     $uri = $url . ($query ? '?' . UrlHelper::buildQuery($query) : '');
209     $external = UrlHelper::isValid($url, TRUE);
210     $uri = ($external ? $url : 'internal:/' . ltrim($uri, '/'));
211     $this->redirect_redirect->set(0, ['uri' => $uri, 'options' => $options]);
212   }
213
214   /**
215    * Gets the redirect URL.
216    *
217    * @return \Drupal\Core\Url
218    *   The redirect URL.
219    */
220   public function getRedirectUrl() {
221     return $this->get('redirect_redirect')->get(0)->getUrl();
222   }
223
224   /**
225    * Gets the redirect URL options.
226    *
227    * @return array
228    *   The redirect URL options.
229    */
230   public function getRedirectOptions() {
231     return $this->get('redirect_redirect')->options;
232   }
233
234   /**
235    * Gets a specific redirect URL option.
236    *
237    * @param string $key
238    *   Option key.
239    * @param mixed $default
240    *   Default value used in case option does not exist.
241    *
242    * @return mixed
243    *   The option value.
244    */
245   public function getRedirectOption($key, $default = NULL) {
246     $options = $this->getRedirectOptions();
247     return isset($options[$key]) ? $options[$key] : $default;
248   }
249
250   /**
251    * Gets the current redirect entity hash.
252    *
253    * @return string
254    *   The hash.
255    */
256   public function getHash() {
257     return $this->get('hash')->value;
258   }
259
260   /**
261    * {@inheritdoc}
262    */
263   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
264     $fields['rid'] = BaseFieldDefinition::create('integer')
265       ->setLabel(t('Redirect ID'))
266       ->setDescription(t('The redirect ID.'))
267       ->setReadOnly(TRUE);
268
269     $fields['uuid'] = BaseFieldDefinition::create('uuid')
270       ->setLabel(t('UUID'))
271       ->setDescription(t('The record UUID.'))
272       ->setReadOnly(TRUE);
273
274     $fields['hash'] = BaseFieldDefinition::create('string')
275       ->setLabel(t('Hash'))
276       ->setSetting('max_length', 64)
277       ->setDescription(t('The redirect hash.'));
278
279     $fields['type'] = BaseFieldDefinition::create('string')
280       ->setLabel(t('Type'))
281       ->setDescription(t('The redirect type.'));
282
283     $fields['uid'] = BaseFieldDefinition::create('entity_reference')
284       ->setLabel(t('User ID'))
285       ->setDescription(t('The user ID of the node author.'))
286       ->setDefaultValueCallback('\Drupal\redirect\Entity\Redirect::getCurrentUserId')
287       ->setSettings(array(
288         'target_type' => 'user',
289       ));
290
291     $fields['redirect_source'] = BaseFieldDefinition::create('redirect_source')
292       ->setLabel(t('From'))
293       ->setDescription(t("Enter an internal Drupal path or path alias to redirect (e.g. %example1 or %example2). Fragment anchors (e.g. %anchor) are <strong>not</strong> allowed.", array('%example1' => 'node/123', '%example2' => 'taxonomy/term/123', '%anchor' => '#anchor')))
294       ->setRequired(TRUE)
295       ->setTranslatable(FALSE)
296       ->setDisplayOptions('form', array(
297         'type' => 'redirect_link',
298         'weight' => -5,
299       ))
300       ->setDisplayConfigurable('form', TRUE);
301
302     $fields['redirect_redirect'] = BaseFieldDefinition::create('link')
303       ->setLabel(t('To'))
304       ->setRequired(TRUE)
305       ->setTranslatable(FALSE)
306       ->setSettings(array(
307         'link_type' => LinkItemInterface::LINK_GENERIC,
308         'title' => DRUPAL_DISABLED
309       ))
310       ->setDisplayOptions('form', array(
311         'type' => 'link',
312         'weight' => -4,
313       ))
314       ->setDisplayConfigurable('form', TRUE);
315
316     $fields['language'] = BaseFieldDefinition::create('language')
317       ->setLabel(t('Language'))
318       ->setDescription(t('The redirect language.'))
319       ->setDisplayOptions('form', array(
320         'type' => 'language_select',
321         'weight' => 2,
322       ));
323
324     $fields['status_code'] = BaseFieldDefinition::create('integer')
325       ->setLabel(t('Status code'))
326       ->setDescription(t('The redirect status code.'))
327       ->setDefaultValue(0);
328
329     $fields['created'] = BaseFieldDefinition::create('created')
330       ->setLabel(t('Created'))
331       ->setDescription(t('The date when the redirect was created.'));
332     return $fields;
333   }
334
335   /**
336    * Default value callback for 'uid' base field definition.
337    *
338    * @see ::baseFieldDefinitions()
339    *
340    * @return array
341    *   An array of default values.
342    */
343   public static function getCurrentUserId() {
344     return array(\Drupal::currentUser()->id());
345   }
346
347 }