redirect.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. /**
  3. * @file
  4. * The redirect module.
  5. */
  6. /**
  7. * @defgroup redirect_api Redirection API
  8. * @{
  9. * Functions related to URL redirects.
  10. *
  11. * @} End of "defgroup redirect_api".
  12. */
  13. use Drupal\Component\Utility\UrlHelper;
  14. use Drupal\Core\Cache\Cache;
  15. use Drupal\Core\Entity\EntityInterface;
  16. use Drupal\Core\Field\FieldItemList;
  17. use Drupal\Core\Form\FormStateInterface;
  18. use Drupal\Core\Language\Language;
  19. use Drupal\Core\Routing\RouteMatchInterface;
  20. use Drupal\Core\Url;
  21. use Drupal\Core\Site\Settings;
  22. use Drupal\redirect\Entity\Redirect;
  23. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  24. /**
  25. * Implements hook_hook_info().
  26. */
  27. function redirect_hook_info() {
  28. $hooks = array(
  29. 'redirect_load',
  30. 'redirect_load_by_source_alter',
  31. 'redirect_access',
  32. 'redirect_prepare',
  33. 'redirect_validate',
  34. 'redirect_presave',
  35. 'redirect_insert',
  36. 'redirect_update',
  37. 'redirect_delete',
  38. 'redirect_alter',
  39. );
  40. return array_fill_keys($hooks, array('group' => 'redirect'));
  41. }
  42. /**
  43. * Implements hook_help().
  44. */
  45. function redirect_help($route_name, RouteMatchInterface $route_match) {
  46. $output = '';
  47. switch ($route_name) {
  48. case 'help.page.redirect':
  49. $output = '<h3>' . t('About') . '</h3>';
  50. $output .= '<p>' . t('The Redirect module allows users to redirect from old URLs to new URLs. For more information, see the <a href=":online">online documentation for Redirect</a>.', [':online' => 'https://www.drupal.org/documentation/modules/path-redirect']) . '</p>';
  51. $output .= '<dl>';
  52. $output .= '<h3>' . t('Uses') . '</h3>';
  53. $output .= '<dd>' . t('Redirect is accessed from three tabs that help you manage <a href=":list">URL Redirects</a>.', [':list' => Url::fromRoute('redirect.list')->toString()]) . '</dd>';
  54. $output .= '<dt>' . t('Manage URL Redirects') . '</dt>';
  55. $output .= '<dd>' . t('The <a href=":redirect">"URL Redirects"</a> page is used to setup and manage URL Redirects. New redirects are created here using the <a href=":add_form">Add redirect</a> button which presents a form to simplify the creation of redirects . The URL redirects page provides a list of all redirects on the site and allows you to edit them.', [':redirect' => Url::fromRoute('redirect.list')->toString(), ':add_form' => Url::fromRoute('redirect.add')->toString()]) . '</dd>';
  56. if (\Drupal::moduleHandler()->moduleExists('redirect_404')) {
  57. $output .= '<dt>' . t('Fix 404 pages') . '</dt>';
  58. $output .= '<dd>' . t('<a href=":fix_404">"Fix 404 pages"</a> lists all paths that have resulted in 404 errors and do not yet have any redirects assigned to them. This 404 (or Not Found) error message is an HTTP standard response code indicating that the client was able to communicate with a given server, but the server could not find what was requested.', [':fix_404' => Url::fromRoute('redirect_404.fix_404')->toString()]) . '</dd>';
  59. }
  60. elseif (!\Drupal::moduleHandler()->moduleExists('redirect_404') && \Drupal::currentUser()->hasPermission('administer modules')) {
  61. $output .= '<dt>' . t('Fix 404 pages') . '</dt>';
  62. $output .= '<dd>' . t('404 (or Not Found) error message is an HTTP standard response code indicating that the client was able to communicate with a given server, but the server could not find what was requested. Please install the <a href=":extend">Redirect 404</a> submodule to be able to log all paths that have resulted in 404 errors.', [':extend' => Url::fromRoute('system.modules_list')->toString()]) . '</dd>';
  63. }
  64. $output .= '<dt>' . t('Configure Global Redirects') . '</dt>';
  65. $output .= '<dd>' . t('The <a href=":settings">"Settings"</a> page presents you with a number of means to adjust redirect settings.', [':settings' => Url::fromRoute('redirect.settings')->toString()]) . '</dd>';
  66. $output .= '</dl>';
  67. return $output;
  68. break;
  69. }
  70. }
  71. /**
  72. * Implements hook_entity_delete().
  73. *
  74. * Will delete redirects based on the entity URL.
  75. */
  76. function redirect_entity_delete(EntityInterface $entity) {
  77. try {
  78. if ($entity->getEntityType()->hasLinkTemplate('canonical') && $entity->toUrl('canonical')->isRouted()) {
  79. redirect_delete_by_path('internal:/' . $entity->toUrl('canonical')->getInternalPath());
  80. redirect_delete_by_path('entity:' . $entity->getEntityTypeId() . '/' . $entity->id());
  81. }
  82. }
  83. catch (RouteNotFoundException $e) {
  84. // This can happen if a module incorrectly defines a link template, ignore
  85. // such errors.
  86. }
  87. }
  88. /**
  89. * Implements hook_path_update().
  90. *
  91. * Will create redirect from the old path alias to the new one.
  92. */
  93. function redirect_path_update(array $path) {
  94. if (!\Drupal::config('redirect.settings')->get('auto_redirect')) {
  95. return;
  96. }
  97. $original_path = $path['original'];
  98. // Delete all redirects having the same source as this alias.
  99. redirect_delete_by_path($path['alias'], $path['langcode'], FALSE);
  100. if ($original_path['alias'] != $path['alias']) {
  101. if (!redirect_repository()->findMatchingRedirect($original_path['alias'], array(), $original_path['langcode'])) {
  102. $redirect = Redirect::create();
  103. $redirect->setSource($original_path['alias']);
  104. $redirect->setRedirect($path['source']);
  105. $redirect->setLanguage($original_path['langcode']);
  106. $redirect->setStatusCode(\Drupal::config('redirect.settings')->get('default_status_code'));
  107. $redirect->save();
  108. }
  109. }
  110. }
  111. /**
  112. * Implements hook_path_insert().
  113. */
  114. function redirect_path_insert(array $path) {
  115. // Delete all redirects having the same source as this alias.
  116. redirect_delete_by_path($path['alias'], $path['langcode'], FALSE);
  117. }
  118. /**
  119. * Implements hook_path_delete().
  120. */
  121. function redirect_path_delete($path) {
  122. if (!\Drupal::config('redirect.settings')->get('auto_redirect')) {
  123. return;
  124. }
  125. elseif (isset($path['redirect']) && !$path['redirect']) {
  126. return;
  127. }
  128. elseif (empty($path)) {
  129. // @todo Remove this condition and allow $path to use an array type hint
  130. // when http://drupal.org/node/1025904 is fixed.
  131. return;
  132. }
  133. // Redirect from a deleted alias to the system path.
  134. //if (!redirect_load_by_source($path['alias'], $path['language'])) {
  135. // $redirect = new stdClass();
  136. // redirect_create($redirect);
  137. // $redirect->source = $path['alias'];
  138. // $redirect->redirect = $path['source'];
  139. // $redirect->language = $path['language'];
  140. // redirect_save($redirect);
  141. //}
  142. }
  143. /**
  144. * Implements hook_page_build().
  145. *
  146. * Adds an action on 404 pages to create a redirect.
  147. *
  148. * @todo hook_page_build() can no longer be used for this. Find a different way.
  149. */
  150. function redirect_page_build(&$page) {
  151. if (redirect_is_current_page_404() && \Drupal::currentUser()->hasPermission('administer redirects')) {
  152. if (!isset($page['content']['system_main']['actions'])) {
  153. $page['content']['system_main']['actions'] = array(
  154. '#theme' => 'links',
  155. '#links' => array(),
  156. '#attributes' => array('class' => array('action-links')),
  157. '#weight' => -100,
  158. );
  159. }
  160. // We cannot simply use current_path() because if a 404 path is set, then
  161. // that value overrides whatever is in $_GET['q']. The
  162. // drupal_deliver_html_page() function thankfully puts the original current
  163. // path into $_GET['destination'].
  164. $destination = drupal_get_destination();
  165. $page['content']['system_main']['actions']['#links']['add_redirect'] = array(
  166. 'title' => t('Add URL redirect from this page to another location'),
  167. 'href' => 'admin/config/search/redirect/add',
  168. 'query' => array('source' => $destination['destination']) + drupal_get_destination(),
  169. );
  170. }
  171. }
  172. /**
  173. * Gets the redirect repository service.
  174. *
  175. * @return \Drupal\redirect\RedirectRepository
  176. * The repository service.
  177. */
  178. function redirect_repository() {
  179. return \Drupal::service('redirect.repository');
  180. }
  181. /**
  182. * Delete any redirects associated with a path or any of its sub-paths.
  183. *
  184. * Given a source like 'node/1' this function will delete any redirects that
  185. * have that specific source or any sources that match 'node/1/%'.
  186. *
  187. * @param string $path
  188. * An string with an internal Drupal path.
  189. * @param string $langcode
  190. * (optional) If specified, limits deletion to redirects for the given
  191. * language. Defaults to all languages.
  192. * @param bool $match_subpaths_and_redirect
  193. * (optional) Whether redirects with a destination to the given path and
  194. * sub-paths should also be deleted.
  195. *
  196. * @ingroup redirect_api
  197. */
  198. function redirect_delete_by_path($path, $langcode = NULL, $match_subpaths_and_redirect = TRUE) {
  199. $path = ltrim($path, '/');
  200. $database = \Drupal::database();
  201. $query = $database->select('redirect');
  202. $query->addField('redirect', 'rid');
  203. $query_or = db_or();
  204. $query_or->condition('redirect_source__path', $database->escapeLike($path), 'LIKE');
  205. if ($match_subpaths_and_redirect) {
  206. $query_or->condition('redirect_source__path', $database->escapeLike($path . '/') . '%', 'LIKE');
  207. $query_or->condition('redirect_redirect__uri', $database->escapeLike($path), 'LIKE');
  208. $query_or->condition('redirect_redirect__uri', $database->escapeLike($path . '/') . '%', 'LIKE');
  209. }
  210. if ($langcode) {
  211. $query->condition('language', $langcode);
  212. }
  213. $query->condition($query_or);
  214. $rids = $query->execute()->fetchCol();
  215. if ($rids) {
  216. foreach (redirect_repository()->loadMultiple($rids) as $redirect) {
  217. $redirect->delete();
  218. }
  219. }
  220. }
  221. /**
  222. * Sort an array recusively.
  223. *
  224. * @param $array
  225. * The array to sort, by reference.
  226. * @param $callback
  227. * The sorting callback to use (e.g. 'sort', 'ksort', 'asort').
  228. *
  229. * @return
  230. * TRUE on success or FALSE on failure.
  231. */
  232. function redirect_sort_recursive(&$array, $callback = 'sort') {
  233. $result = $callback($array);
  234. foreach ($array as $key => $value) {
  235. if (is_array($value)) {
  236. $result &= redirect_sort_recursive($array[$key], $callback);
  237. }
  238. }
  239. return $result;
  240. }
  241. /**
  242. * Build the URL of a redirect for display purposes only.
  243. */
  244. function redirect_url($path, array $options = array(), $clean_url = NULL) {
  245. // @todo - deal with removal of clean_url config. See
  246. // https://drupal.org/node/1659580
  247. if (!isset($clean_url)) {
  248. //$clean_url = variable_get('clean_url', 0);
  249. }
  250. if ($path == '') {
  251. $path = '<front>';
  252. }
  253. if (!isset($options['alter']) || !empty($options['alter'])) {
  254. \Drupal::moduleHandler()->alter('redirect_url', $path, $options);
  255. }
  256. // The base_url might be rewritten from the language rewrite in domain mode.
  257. if (!isset($options['base_url'])) {
  258. // @todo - is this correct? See https://drupal.org/node/1798832.
  259. if (isset($options['https']) && Settings::get('mixed_mode_sessions', FALSE)) {
  260. if ($options['https'] === TRUE) {
  261. $options['base_url'] = $GLOBALS['base_secure_url'];
  262. $options['absolute'] = TRUE;
  263. }
  264. elseif ($options['https'] === FALSE) {
  265. $options['base_url'] = $GLOBALS['base_insecure_url'];
  266. $options['absolute'] = TRUE;
  267. }
  268. }
  269. else {
  270. $options['base_url'] = $GLOBALS['base_url'];
  271. }
  272. }
  273. if (empty($options['absolute']) || url_is_external($path)) {
  274. $url = $path;
  275. }
  276. else {
  277. $url = $options['base_url'] . base_path() . $path;
  278. }
  279. if (isset($options['query'])) {
  280. $url .= $clean_url ? '?' : '&';
  281. $url .= UrlHelper::buildQuery($options['query']);
  282. }
  283. if (isset($options['fragment'])) {
  284. $url .= '#' . $options['fragment'];
  285. }
  286. return $url;
  287. }
  288. function redirect_status_code_options($code = NULL) {
  289. $codes = array(
  290. 300 => t('300 Multiple Choices'),
  291. 301 => t('301 Moved Permanently'),
  292. 302 => t('302 Found'),
  293. 303 => t('303 See Other'),
  294. 304 => t('304 Not Modified'),
  295. 305 => t('305 Use Proxy'),
  296. 307 => t('307 Temporary Redirect'),
  297. );
  298. return isset($codes[$code]) ? $codes[$code] : $codes;
  299. }
  300. /**
  301. * Returns if the current page request is a page not found (404 status error).
  302. *
  303. * Why the fuck do we have to do this? Why is there not an easier way???
  304. *
  305. * @return
  306. * TRUE if the current page is a 404, or FALSE otherwise.
  307. */
  308. function redirect_is_current_page_404() {
  309. return drupal_get_http_header('Status') == '404 Not Found';
  310. }
  311. /**
  312. * uasort callback; Compare redirects based on language neutrality and rids.
  313. */
  314. function _redirect_uasort($a, $b) {
  315. $a_weight = isset($a->weight) ? $a->weight : 0;
  316. $b_weight = isset($b->weight) ? $b->weight : 0;
  317. if ($a_weight != $b_weight) {
  318. // First sort by weight (case sensitivity).
  319. return $a_weight > $b_weight;
  320. }
  321. elseif ($a->language != $b->language) {
  322. // Then sort by language specific over language neutral.
  323. return $a->language == Language::LANGCODE_NOT_SPECIFIED;
  324. }
  325. elseif (!empty($a->source_options['query']) != !empty($b->source_options['query'])) {
  326. // Then sort by redirects that do not have query strings over ones that do.
  327. return empty($a->source_options['query']);
  328. }
  329. else {
  330. // Lastly sort by the highest redirect ID.
  331. return $a->rid < $b->rid;
  332. }
  333. }
  334. /**
  335. * Implements hook_form_FORM_ID_alter() on behalf of locale.module.
  336. */
  337. function locale_form_redirect_edit_form_alter(array &$form, FormStateInterface $form_state) {
  338. $form['language'] = array(
  339. '#type' => 'select',
  340. '#title' => t('Language'),
  341. '#options' => array(Language::LANGCODE_NOT_SPECIFIED => t('All languages')) + \Drupal::languageManager()->getLanguages(),
  342. '#default_value' => $form['language']['#value'],
  343. '#description' => t('A redirect set for a specific language will always be used when requesting this page in that language, and takes precedence over redirects set for <em>All languages</em>.'),
  344. );
  345. }
  346. /**
  347. * Fetch an array of redirect bulk operations.
  348. *
  349. * @see hook_redirect_operations()
  350. * @see hook_redirect_operations_alter()
  351. */
  352. function redirect_get_redirect_operations() {
  353. $operations = &drupal_static(__FUNCTION__);
  354. if (!isset($operations)) {
  355. $operations = \Drupal::moduleHandler()->invokeAll('redirect_operations');
  356. \Drupal::moduleHandler()->alter('redirect_operations', $operations);
  357. }
  358. return $operations;
  359. }
  360. /**
  361. * Implements hook_redirect_operations().
  362. */
  363. function redirect_redirect_operations() {
  364. $operations['delete'] = array(
  365. 'action' => t('Delete'),
  366. 'action_past' => t('Deleted'),
  367. 'callback' => 'redirect_delete_multiple',
  368. 'confirm' => TRUE,
  369. );
  370. return $operations;
  371. }
  372. /**
  373. * Ajax callback for the redirect link widget.
  374. */
  375. function redirect_source_link_get_status_messages(array $form, FormStateInterface $form_state) {
  376. return $form['redirect_source']['widget'][0]['status_box'];
  377. }
  378. /**
  379. * Implements hook_entity_extra_field_info().
  380. */
  381. function redirect_entity_extra_field_info() {
  382. $extra = [];
  383. if (\Drupal::service('module_handler')->moduleExists('node')) {
  384. $node_types = \Drupal::entityTypeManager()
  385. ->getStorage('node_type')
  386. ->loadMultiple();
  387. foreach ($node_types as $node_type) {
  388. $extra['node'][$node_type->id()]['form']['url_redirects'] = [
  389. 'label' => t('URL redirects'),
  390. 'description' => t('Redirect module form elements'),
  391. 'weight' => 50,
  392. ];
  393. }
  394. }
  395. return $extra;
  396. }
  397. /**
  398. * Implements hook_form_node_form_alter().
  399. */
  400. function redirect_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  401. /** @var \Drupal\node\NodeInterface $node */
  402. $node = $form_state->getFormObject()->getEntity();
  403. if (!$node->isNew() && \Drupal::currentUser()->hasPermission('administer redirects')) {
  404. $nid = $node->id();
  405. // Find redirects to this node.
  406. $redirects = \Drupal::service('redirect.repository')
  407. ->findByDestinationUri(["internal:/node/$nid", "entity:node/$nid"]);
  408. // Assemble the rows for the table.
  409. $rows = [];
  410. /** @var \Drupal\Core\Entity\EntityListBuilder $list_builder */
  411. $list_builder = \Drupal::service('entity.manager')->getListBuilder('redirect');
  412. /** @var \Drupal\redirect\Entity\Redirect[] $redirects */
  413. foreach ($redirects as $redirect) {
  414. $row = [];
  415. $path = $redirect->getSourcePathWithQuery();
  416. $row['path'] = [
  417. 'class' => ['redirect-table__path'],
  418. 'data' => ['#plain_text' => $path],
  419. 'title' => $path,
  420. ];
  421. $row['operations'] = [
  422. 'data' => [
  423. '#type' => 'operations',
  424. '#links' => $list_builder->getOperations($redirect),
  425. ],
  426. ];
  427. $rows[] = $row;
  428. }
  429. // Add the list to the vertical tabs section of the form.
  430. $header = [
  431. ['class' => ['redirect-table__path'], 'data' => t('From')],
  432. ['class' => ['redirect-table__operations'], 'data' => t('Operations')],
  433. ];
  434. $form['url_redirects'] = [
  435. '#type' => 'details',
  436. '#title' => t('URL redirects'),
  437. '#group' => 'advanced',
  438. '#open' => FALSE,
  439. 'table' => [
  440. '#type' => 'table',
  441. '#header' => $header,
  442. '#rows' => $rows,
  443. '#empty' => t('No URL redirects available.'),
  444. '#attributes' => ['class' => ['redirect-table']],
  445. ],
  446. '#attached' => [
  447. 'library' => [
  448. 'redirect/drupal.redirect.admin',
  449. ],
  450. ],
  451. ];
  452. if (!empty($rows)) {
  453. $form['url_redirects']['warning'] = [
  454. '#markup' => t('Note: links open in the current window.'),
  455. '#prefix' => '<p>',
  456. '#suffix' => '</p>',
  457. ];
  458. }
  459. }
  460. }