3 namespace Drupal\redirect\Entity;
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;
14 * The redirect entity class.
18 * label = @Translation("Redirect"),
19 * bundle_label = @Translation("Redirect type"),
21 * "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
23 * "default" = "Drupal\redirect\Form\RedirectForm",
24 * "delete" = "Drupal\redirect\Form\RedirectDeleteForm",
25 * "edit" = "Drupal\redirect\Form\RedirectForm"
27 * "views_data" = "Drupal\redirect\RedirectViewsData",
28 * "storage_schema" = "\Drupal\redirect\RedirectStorageSchema"
30 * base_table = "redirect",
31 * translatable = FALSE,
32 * admin_permission = "administer redirects",
35 * "label" = "redirect_source",
38 * "langcode" = "language",
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}",
47 class Redirect extends ContentEntityBase {
50 * Generates a unique hash for identification purposes.
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
62 public static function generateHash($source_path, array $source_query, $language) {
64 'source' => mb_strtolower($source_path),
65 'language' => $language,
68 if (!empty($source_query)) {
69 $hash['source_query'] = $source_query;
71 redirect_sort_recursive($hash, 'ksort');
72 return Crypt::hashBase64(serialize($hash));
78 public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
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));
94 * Sets the redirect language.
96 * @param string $language
99 public function setLanguage($language) {
100 $this->set('language', $language);
104 * Sets the redirect status code.
106 * @param int $status_code
107 * The redirect status code.
109 public function setStatusCode($status_code) {
110 $this->set('status_code', $status_code);
114 * Gets the redirect status code.
117 * The redirect status code.
119 public function getStatusCode() {
120 return $this->get('status_code')->value;
124 * Sets the redirect created datetime.
126 * @param int $datetime
127 * The redirect created datetime.
129 public function setCreated($datetime) {
130 $this->set('created', $datetime);
134 * Gets the redirect created datetime.
137 * The redirect created datetime.
139 public function getCreated() {
140 return $this->get('created')->value;
144 * Sets the source URL data.
146 * @param string $path
147 * The base url of the source.
148 * @param array $query
151 public function setSource($path, array $query = array()) {
152 $this->redirect_source->set(0, ['path' => ltrim($path, '/'), 'query' => $query]);
156 * Gets the source URL data.
160 public function getSource() {
161 return $this->get('redirect_source')->get(0)->getValue();
165 * Gets the source base URL.
169 public function getSourceUrl() {
170 return $this->get('redirect_source')->get(0)->getUrl()->toString();
174 * Gets the source URL path with its query.
177 * The source URL path, eventually with its query.
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);
188 * Gets the redirect URL data.
191 * The redirect URL data.
193 public function getRedirect() {
194 return $this->get('redirect_redirect')->get(0)->getValue();
198 * Sets the redirect destination URL data.
201 * The base url of the redirect destination.
202 * @param array $query
204 * @param array $options
205 * The source url options.
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]);
215 * Gets the redirect URL.
217 * @return \Drupal\Core\Url
220 public function getRedirectUrl() {
221 return $this->get('redirect_redirect')->get(0)->getUrl();
225 * Gets the redirect URL options.
228 * The redirect URL options.
230 public function getRedirectOptions() {
231 return $this->get('redirect_redirect')->options;
235 * Gets a specific redirect URL option.
239 * @param mixed $default
240 * Default value used in case option does not exist.
245 public function getRedirectOption($key, $default = NULL) {
246 $options = $this->getRedirectOptions();
247 return isset($options[$key]) ? $options[$key] : $default;
251 * Gets the current redirect entity hash.
256 public function getHash() {
257 return $this->get('hash')->value;
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.'))
269 $fields['uuid'] = BaseFieldDefinition::create('uuid')
270 ->setLabel(t('UUID'))
271 ->setDescription(t('The record UUID.'))
274 $fields['hash'] = BaseFieldDefinition::create('string')
275 ->setLabel(t('Hash'))
276 ->setSetting('max_length', 64)
277 ->setDescription(t('The redirect hash.'));
279 $fields['type'] = BaseFieldDefinition::create('string')
280 ->setLabel(t('Type'))
281 ->setDescription(t('The redirect type.'));
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')
288 'target_type' => 'user',
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')))
295 ->setTranslatable(FALSE)
296 ->setDisplayOptions('form', array(
297 'type' => 'redirect_link',
300 ->setDisplayConfigurable('form', TRUE);
302 $fields['redirect_redirect'] = BaseFieldDefinition::create('link')
305 ->setTranslatable(FALSE)
307 'link_type' => LinkItemInterface::LINK_GENERIC,
308 'title' => DRUPAL_DISABLED
310 ->setDisplayOptions('form', array(
314 ->setDisplayConfigurable('form', TRUE);
316 $fields['language'] = BaseFieldDefinition::create('language')
317 ->setLabel(t('Language'))
318 ->setDescription(t('The redirect language.'))
319 ->setDisplayOptions('form', array(
320 'type' => 'language_select',
324 $fields['status_code'] = BaseFieldDefinition::create('integer')
325 ->setLabel(t('Status code'))
326 ->setDescription(t('The redirect status code.'))
327 ->setDefaultValue(0);
329 $fields['created'] = BaseFieldDefinition::create('created')
330 ->setLabel(t('Created'))
331 ->setDescription(t('The date when the redirect was created.'));
336 * Default value callback for 'uid' base field definition.
338 * @see ::baseFieldDefinitions()
341 * An array of default values.
343 public static function getCurrentUserId() {
344 return array(\Drupal::currentUser()->id());