Loading redirect.api.php +27 −16 Original line number Diff line number Diff line Loading @@ -5,6 +5,8 @@ * Hooks provided by the Redirect module. */ use Drupal\Core\Routing\TrustedRedirectResponse; /** * @defgroup redirect_api_hooks Redirect API Hooks * @{ Loading Loading @@ -53,6 +55,31 @@ * @{ */ /** * Act on a redirect response when it is triggered. * * This hook is invoked before the response is sent to the user. The redirect * entity itself is sent as well for inspection. * * @param Drupal\Core\Routing\TrustedRedirectResponse $response * The generated redirect response object before it is delivered. * @param \Drupal\redirect\Entity\Redirect $redirect * The redirect entity used to generate the response object. * * @ingroup redirect_api_hooks */ function hook_redirect_response_alter(TrustedRedirectResponse $response, \Drupal\redirect\Entity\Redirect $redirect) { // Set a drupal message. if (!$redirect->getRedirectUrl()->isExternal()) { \Drupal::messenger()->addWarning(t('You are not being directed off-site.')); } // If `some condition`, send to Drupal.org. if (FALSE) { $response->setTrustedTargetUrl('http://drupal.org'); } } /** * Act on redirects being loaded from the database. * Loading Loading @@ -121,22 +148,6 @@ function hook_redirect_prepare($redirect) { } /** * Act on a redirect being redirected. * * This hook is invoked from redirect_redirect() before the redirect callback * is invoked. * * @param $redirect * The redirect that is being used for the redirect. * * @see redirect_redirect() * @see drupal_page_is_cacheable() * @ingroup redirect_api_hooks */ function hook_redirect_alter($redirect) { } /** * @} End of "addtogroup hooks". */ redirect.module +1 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,7 @@ function redirect_hook_info() { 'redirect_update', 'redirect_delete', 'redirect_alter', 'redirect_response', ]; return array_fill_keys($hooks, ['group' => 'redirect']); Loading src/EventSubscriber/RedirectRequestSubscriber.php +4 −0 Original line number Diff line number Diff line Loading @@ -165,6 +165,10 @@ class RedirectRequestSubscriber implements EventSubscriberInterface { ]; $response = new TrustedRedirectResponse($url->setAbsolute()->toString(), $redirect->getStatusCode(), $headers); $response->addCacheableDependency($redirect); // Invoke hook_redirect_response_alter(). $this->moduleHandler->alter('redirect_response', $response, $redirect); $event->setResponse($response); } } Loading tests/modules/redirect_test/redirect_test.info.yml 0 → 100644 +6 −0 Original line number Diff line number Diff line name: 'Redirect module tests' type: module description: 'Support module for redirect testing.' package: Testing dependencies: - drupal:redirect tests/modules/redirect_test/redirect_test.module 0 → 100644 +20 −0 Original line number Diff line number Diff line <?php /** * @file * A test module for testing the redirect module. */ use Drupal\Core\Routing\TrustedRedirectResponse; use Drupal\redirect\Entity\Redirect; /** * Implements hook_redirect_response_alter(). */ function redirect_test_redirect_response_alter(TrustedRedirectResponse $response, Redirect $redirect) { $path = 'test/redirect/2/successful'; $replace = 'test/redirect/other'; if ($redirect->getRedirect()['uri'] == "internal:/" . $path) { $response->setTargetUrl(str_replace($path, $replace, $redirect->getRedirectUrl()->setAbsolute()->toString())); } } Loading
redirect.api.php +27 −16 Original line number Diff line number Diff line Loading @@ -5,6 +5,8 @@ * Hooks provided by the Redirect module. */ use Drupal\Core\Routing\TrustedRedirectResponse; /** * @defgroup redirect_api_hooks Redirect API Hooks * @{ Loading Loading @@ -53,6 +55,31 @@ * @{ */ /** * Act on a redirect response when it is triggered. * * This hook is invoked before the response is sent to the user. The redirect * entity itself is sent as well for inspection. * * @param Drupal\Core\Routing\TrustedRedirectResponse $response * The generated redirect response object before it is delivered. * @param \Drupal\redirect\Entity\Redirect $redirect * The redirect entity used to generate the response object. * * @ingroup redirect_api_hooks */ function hook_redirect_response_alter(TrustedRedirectResponse $response, \Drupal\redirect\Entity\Redirect $redirect) { // Set a drupal message. if (!$redirect->getRedirectUrl()->isExternal()) { \Drupal::messenger()->addWarning(t('You are not being directed off-site.')); } // If `some condition`, send to Drupal.org. if (FALSE) { $response->setTrustedTargetUrl('http://drupal.org'); } } /** * Act on redirects being loaded from the database. * Loading Loading @@ -121,22 +148,6 @@ function hook_redirect_prepare($redirect) { } /** * Act on a redirect being redirected. * * This hook is invoked from redirect_redirect() before the redirect callback * is invoked. * * @param $redirect * The redirect that is being used for the redirect. * * @see redirect_redirect() * @see drupal_page_is_cacheable() * @ingroup redirect_api_hooks */ function hook_redirect_alter($redirect) { } /** * @} End of "addtogroup hooks". */
redirect.module +1 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,7 @@ function redirect_hook_info() { 'redirect_update', 'redirect_delete', 'redirect_alter', 'redirect_response', ]; return array_fill_keys($hooks, ['group' => 'redirect']); Loading
src/EventSubscriber/RedirectRequestSubscriber.php +4 −0 Original line number Diff line number Diff line Loading @@ -165,6 +165,10 @@ class RedirectRequestSubscriber implements EventSubscriberInterface { ]; $response = new TrustedRedirectResponse($url->setAbsolute()->toString(), $redirect->getStatusCode(), $headers); $response->addCacheableDependency($redirect); // Invoke hook_redirect_response_alter(). $this->moduleHandler->alter('redirect_response', $response, $redirect); $event->setResponse($response); } } Loading
tests/modules/redirect_test/redirect_test.info.yml 0 → 100644 +6 −0 Original line number Diff line number Diff line name: 'Redirect module tests' type: module description: 'Support module for redirect testing.' package: Testing dependencies: - drupal:redirect
tests/modules/redirect_test/redirect_test.module 0 → 100644 +20 −0 Original line number Diff line number Diff line <?php /** * @file * A test module for testing the redirect module. */ use Drupal\Core\Routing\TrustedRedirectResponse; use Drupal\redirect\Entity\Redirect; /** * Implements hook_redirect_response_alter(). */ function redirect_test_redirect_response_alter(TrustedRedirectResponse $response, Redirect $redirect) { $path = 'test/redirect/2/successful'; $replace = 'test/redirect/other'; if ($redirect->getRedirect()['uri'] == "internal:/" . $path) { $response->setTargetUrl(str_replace($path, $replace, $redirect->getRedirectUrl()->setAbsolute()->toString())); } }