3 namespace Drupal\redirect\Entity;
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;
15 * The redirect entity class.
19 * label = @Translation("Redirect"),
20 * bundle_label = @Translation("Redirect type"),
22 * "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
24 * "default" = "Drupal\redirect\Form\RedirectForm",
25 * "delete" = "Drupal\redirect\Form\RedirectDeleteForm",
26 * "edit" = "Drupal\redirect\Form\RedirectForm"
28 * "views_data" = "Drupal\views\EntityViewsData",
29 * "storage_schema" = "\Drupal\redirect\RedirectStorageSchema"
31 * base_table = "redirect",
32 * translatable = FALSE,
33 * admin_permission = "administer redirects",
36 * "label" = "redirect_source",
39 * "langcode" = "language",
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}",
48 class Redirect extends ContentEntityBase {
51 * Generates a unique hash for identification purposes.
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
63 public static function generateHash($source_path, array $source_query, $language) {
65 'source' => Unicode::strtolower($source_path),
66 'language' => $language,
69 if (!empty($source_query)) {
70 $hash['source_query'] = $source_query;
72 redirect_sort_recursive($hash, 'ksort');
73 return Crypt::hashBase64(serialize($hash));
79 public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
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()));
93 * Sets the redirect language.
95 * @param string $language
98 public function setLanguage($language) {
99 $this->set('language', $language);
103 * Sets the redirect status code.
105 * @param int $status_code
106 * The redirect status code.
108 public function setStatusCode($status_code) {
109 $this->set('status_code', $status_code);
113 * Gets the redirect status code.
116 * The redirect status code.
118 public function getStatusCode() {
119 return $this->get('status_code')->value;
123 * Sets the redirect created datetime.
125 * @param int $datetime
126 * The redirect created datetime.
128 public function setCreated($datetime) {
129 $this->set('created', $datetime);
133 * Gets the redirect created datetime.
136 * The redirect created datetime.
138 public function getCreated() {
139 return $this->get('created')->value;
143 * Sets the source URL data.
145 * @param string $path
146 * The base url of the source.
147 * @param array $query
150 public function setSource($path, array $query = array()) {
151 $this->redirect_source->set(0, ['path' => ltrim($path, '/'), 'query' => $query]);
155 * Gets the source URL data.
159 public function getSource() {
160 return $this->get('redirect_source')->get(0)->getValue();
164 * Gets the source base URL.
168 public function getSourceUrl() {
169 return $this->get('redirect_source')->get(0)->getUrl()->toString();
173 * Gets the source URL path with its query.
176 * The source URL path, eventually with its query.
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);
187 * Gets the redirect URL data.
190 * The redirect URL data.
192 public function getRedirect() {
193 return $this->get('redirect_redirect')->get(0)->getValue();
197 * Sets the redirect destination URL data.
200 * The base url of the redirect destination.
201 * @param array $query
203 * @param array $options
204 * The source url options.
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]);
212 * Gets the redirect URL.
214 * @return \Drupal\Core\Url
217 public function getRedirectUrl() {
218 return $this->get('redirect_redirect')->get(0)->getUrl();
222 * Gets the redirect URL options.
225 * The redirect URL options.
227 public function getRedirectOptions() {
228 return $this->get('redirect_redirect')->options;
232 * Gets a specific redirect URL option.
236 * @param mixed $default
237 * Default value used in case option does not exist.
242 public function getRedirectOption($key, $default = NULL) {
243 $options = $this->getRedirectOptions();
244 return isset($options[$key]) ? $options[$key] : $default;
248 * Gets the current redirect entity hash.
253 public function getHash() {
254 return $this->get('hash')->value;
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.'))
266 $fields['uuid'] = BaseFieldDefinition::create('uuid')
267 ->setLabel(t('UUID'))
268 ->setDescription(t('The record UUID.'))
271 $fields['hash'] = BaseFieldDefinition::create('string')
272 ->setLabel(t('Hash'))
273 ->setSetting('max_length', 64)
274 ->setDescription(t('The redirect hash.'));
276 $fields['type'] = BaseFieldDefinition::create('string')
277 ->setLabel(t('Type'))
278 ->setDescription(t('The redirect type.'));
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')
285 'target_type' => 'user',
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')))
292 ->setTranslatable(FALSE)
293 ->setDisplayOptions('form', array(
294 'type' => 'redirect_link',
297 ->setDisplayConfigurable('form', TRUE);
299 $fields['redirect_redirect'] = BaseFieldDefinition::create('link')
302 ->setTranslatable(FALSE)
304 'link_type' => LinkItemInterface::LINK_GENERIC,
305 'title' => DRUPAL_DISABLED
307 ->setDisplayOptions('form', array(
311 ->setDisplayConfigurable('form', TRUE);
313 $fields['language'] = BaseFieldDefinition::create('language')
314 ->setLabel(t('Language'))
315 ->setDescription(t('The redirect language.'))
316 ->setDisplayOptions('form', array(
317 'type' => 'language_select',
321 $fields['status_code'] = BaseFieldDefinition::create('integer')
322 ->setLabel(t('Status code'))
323 ->setDescription(t('The redirect status code.'))
324 ->setDefaultValue(0);
326 $fields['created'] = BaseFieldDefinition::create('created')
327 ->setLabel(t('Created'))
328 ->setDescription(t('The date when the redirect was created.'));
333 * Default value callback for 'uid' base field definition.
335 * @see ::baseFieldDefinitions()
338 * An array of default values.
340 public static function getCurrentUserId() {
341 return array(\Drupal::currentUser()->id());