Commit 7ba64cc7 authored by Paulo Henrique Cota Starling's avatar Paulo Henrique Cota Starling Committed by Sascha Grossenbacher
Browse files

Issue #2909182 by dpagini, angrytoast, recrit, Berdir, Quicksaver: API...

Issue #2909182 by dpagini, angrytoast, recrit, Berdir, Quicksaver: API migration: No hook_redirect_alter() in 8.x
parent b69d7546
Loading
Loading
Loading
Loading
+27 −16
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
 * Hooks provided by the Redirect module.
 */

use Drupal\Core\Routing\TrustedRedirectResponse;

/**
 * @defgroup redirect_api_hooks Redirect API Hooks
 * @{
@@ -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.
 *
@@ -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".
 */
+1 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ function redirect_hook_info() {
    'redirect_update',
    'redirect_delete',
    'redirect_alter',
    'redirect_response',
  ];

  return array_fill_keys($hooks, ['group' => 'redirect']);
+4 −0
Original line number Diff line number Diff line
@@ -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);
    }
  }
+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
+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