03 328 8646


Drupal 8 Redirect to Latest Node

Problem: The website has a blog, events or something similar and it would greatly benefit editors, and external sites, to be able to simply link to the page for the most recent, or current, entity rather than having to update their links every time new content is generated.

For example, if you have a site with Exhibitions on it you may want to provide a url such as /current-exhibition that redirects users to /exhibitions/annes-great-exhibition when that is the current exhibition and then to /exhibitions/andys-really-quite-good-exhibition when that becomes the current exhibition. At the same time we want the url /current-exhibition to behave like any other given type of node when it is used on the site ie it should appear in content entity browsers, be accepted into internal link fields, appear in linkit etc. so that editors can drop it in wherever they would otherwise have to drop in /exhibitions/annes-great-exhibition . This saves updating, helps reduce user input errors and gives a good consistency to editor workflow.

The answer we've been using is to create a page with the url we want to redirect eg /current-exhibition and to populate it with whatever images, media, text summaries etc it needs to behave as a real node on navigation pages and everywhere else it may be used. We then use the pattern shown at https://www.thirdandgrove.com/redirecting-node-pages-drupal-8 to catch the request and redirect it. With an entity query to get the url of the latest exhibition we can easily redirect the users' requests to the correct location , and you can use any criteria you can think of for the redirect, providing different end urls as you wish.