Hello Guys,
I am handling few scenarios in my project where I have bunch of internal path's (these can be redirects / aliases) for which I am using 'findMatchingRedirect' method of RedirectRepository Class, to get a redirect for given path.
I have a node (nid = 123) having alias : /alias-exists-for-node-123
I have chained redirects for this node (as below)
From To Status code Original language
/this-is-test-redirect /node/123 301 Not specified
/this-is-test-redirect-10 /this-is-test-redirect 301 Not specified
/this-is-test-redirect-11 /this-is-test-redirect-10 301 Not specified
/this-is-test-redirect-12 /this-is-test-redirect-11 301 Not specified
In current scenario, I have below URL's
/this-is-test-redirect
/this-is-test-redirect-12
I have used above urls in foreach loop and findMatchingRedirect method for getting a redirect for a url.
$redirects = null; $alias_list = [];
$current_lang = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
foreach ($urls as $path) {
try {
$redirects = \Drupal::service('redirect.repository')->findMatchingRedirect($path, [], $current_lang);
} catch (RedirectLoopException $e) {
\Drupal::logger('redirect')->warning($e->getMessage());
}
if($redirects) {
$internal_redirect_uri = $redirects->getRedirect()['uri'];
// $alias_list[] = 'get-alias-by-path'; // use method to get alias by path
}
}
When I Pass below paths, it returns alias of node 123
/this-is-test-redirect-12
/this-is-test-redirect
But when I change the order of paths,
/this-is-test-redirect
/this-is-test-redirect-12
I get caught in to Redirect loop identified issue from findMatchingRedirect method.
Redirect loop identified at /this-is-test-redirect path for redirect 1223
I debug the findMatchingRedirect method, I observed that private array variable $foundRedirects does carry the old redirect-id even after execution completed for first path (i.e /this-is-test-redirect) and about to start for next path(/this-is-test-redirect-12).
Any help would be appreciated! Also correct me if I am doing something wrong.
Thanks !
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | resetFoundRedirect-3059894-13.patch | 1.56 KB | berdir |
| #13 | resetFoundRedirect-3059894-13-test-only.patch | 1.23 KB | berdir |
| #9 | resetFoundRedirect-3059894-9.patch | 490 bytes | nileema19 |
| #6 | resetFoundRedirect-3059894-6.patch | 392 bytes | nileema19 |
Comments
Comment #2
nileema19 commentedComment #3
nileema19 commentedComment #4
nileema19 commentedComment #5
kyuubi commentedThis seems to be related to #3061173: scope foundRedirects to the current request from the request stack. Would be awesome if someone could have a look at this as in the our case it breaks redirects in production (GraphQL subrequests)
Comment #6
nileema19 commentedI have tested some test-cases with attached patch, this has solved my issue.
Though I am still testing this patch. Please review.
Meanwhile I am looking for other solution.
Comment #7
berdirAdding the else seems unnecessary/unrelated and the patch doesn't follow coding standards (2 spaces). Leaving at needs work to have the tests run.
Comment #8
berdirComment #9
nileema19 commented@Berdir, thank you for the inputs!
I have attached the updated patch.
Comment #10
berdirComment #11
mbovan commentedWe use a batch process to update aliases/redirects for multiple entities and we had the same problem as described in the issue summary.
I can confirm that patch from #9 fixes the issue.
Comment #12
kyuubi commented#9 works like a charm for me.
Comment #13
berdirHere's a test for this.
Comment #16
berdirCommitted.
Comment #17
kyuubi commentedAwesome, thanks!