DrupalX

Extension
Framework

Rereouting

All pages in Drupal 8 usually belong to a specific route. These routes are defined by modules like "node" or "path". Those modules are thus responsible to show e.g. a specific node when a certain URL is being called. You can of course do the same in your own modules by creating *.routing.yml files and corresponding callback methods. In addition to requiring lots of boilerplate code, those routes cannot be completely arbitrary, but they have to follow a specific scheme, for example "/example/dynamic/routes/*" which you have to define in advance. If you even want to integrate those newly defined routes seamlessly into an existing menu (i.e. with the "is-active" marker at the correct position etc.) it gets even more complicated.

In short: It is far from trivial to create new, freely definable, URLs in Drupal 8 and assign corresponding node content to those routes.

DrupalX adds a powerful - but easily understandable - new concept to the mix: Rerouting. It works similar to the well known 301 redirect  in HTTP. But with the difference that the URL in the browser stays the same. Only different content is being loaded. Using a new hook function (hook_reroute) which you can implement e.g. in your theme, you can

a) Redirect URLs from existing nodes to other nodes (So-called "Node-to-Node" rerouting)
b) Redirect URLs which don't point to any node so far (i.e. 404 pages) to an existing node ("Void-to-Node" rerouting)

The hook "hook_reroute" only gets a single argument $route_info of type RouteInfo. This object contains all the information about the incoming URL. Whether it points to a node (and if yes, which content type) or whether it points to a 404 and so forth. In addition you can use that object to modify the page that's actually delivered.

Now, the cool thing is that we are using PHP here. So you can implement any arbitary dynamic logic to quickly rewire all incoming URLs to arbitrary pages.

function hook_reroute($route_info) {
    if ($route_info->nodeType=="news") {
      $node=\Drupal\drupalx\Node::query(["title"=>"News article page"]);
      $route_info->reroute($node);
    }
}

This code, for example, causes all nodes of type "news" to just show a node with title "News article page". You can insert this page somewhere in your menu (even disabled so it doesnt actually show up somewhere) and the current page indicator etc. will all be correct. It's just a normal page. You can even add content to the sidebar like seen in the previous chapter.

This is all well and good, but you might think that we have a little problem now: Great, the news article page with its full layout is now shown instead of the original news article node. But how can we make the original content of that article node (i.e. before rerouting) appear somewhere on that page? Of course, DrupalX also has a solution for this. The "Original" class provides all the information from the original page and can be used in a template to render the original node at any position you like. We recommend you create a paragraph called e.g. "original" with a corresponding template paragraph--original.html.twig like so:

{{Original->content}}

Now, wherever you insert that paragraph, the full original node content is inserted.

Taken all together, this allows to do stuff quite easily that would otherweise be very difficult to achieve with plain Drupal. You can, for example, have an infinite number of virtual pages appear "out of thin air" under arbitary URLs (e.g. for pages that are created dynamically from external sources) and cleanly merge them into an existing menu structure, one page can have multiple aliases or totally different pages are served under the same alias - depending on the current user etc.