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