Automatic Page Generation with Custom Entity Routes
One of the most useful items in the Drupal 8 toolbox is the Paragraphs Module. By creating custom paragraph types, you can have much finer control over the admin and content creation process in Drupal.
A recent client of ThinkShout needed a content type (office locations) to include ‘sub-pages’ for things like office hours, services, and other items depending on the location. Most of the sub-page content was pretty simple, but they also needed to have direct links, be printable, and have the same header as the parent page. This ruled out an Ajax solution.
We’ve been using Paragraphs to make configurable content throughout the site, and since the sub-pages only have Title and Content fields, we thought they would be a good fit here as well. We then decided to explore the possibility of using custom entity routes to fulfill the other requirements.
To start, we created two additional view modes for the sub-page paragraphs called Sub-page
and Menu link
containing the Content and Title fields respectively. By keeping these fields in separate view modes, we make it much easier to work with them.
Next we created a custom module to hold all of our code, ts_sub_pages
. In addition to the standard module files, we added the file ts_sub_pages.routing.yml
, which contains the following:
<span class="s">ts_sub_pages.sub_pages</span><span class="pi">:</span> <span class="s">path</span><span class="pi">:</span> <span class="s1">'</span><span class="s">/node/{node}/sub-page/{paragraph}'</span> <span class="s">defaults</span><span class="pi">:</span> <span class="s">_controller</span><span class="pi">:</span> <span class="s1">'</span><span class="s">\Drupal\ts_sub_pages\Controller\TSSubPagesController::subPageParagraph'</span> <span class="s">_title_callback</span><span class="pi">:</span> <span class="s1">'</span><span class="s">\Drupal\ts_sub_pages\Controller\TSSubPagesController::getTitle'</span> <span class="s">options</span><span class="pi">:</span> <span class="s">parameters</span><span class="pi">:</span> <span class="s">node</span><span class="pi">:</span> <span class="s">type</span><span class="pi">:</span> <span class="s">entity:node</span> <span class="s">paragraph</span><span class="pi">:</span> <span class="s">type</span><span class="pi">:</span> <span class="s">entity:paragraph</span> <span class="s">requirements</span><span class="pi">:</span> <span class="s">_permission</span><span class="pi">:</span> <span class="s1">'</span><span class="s">access</span><span class="nv"> </span><span class="s">content'</span>
This defines a unique system path based on the parent node ID and the paragraph entity ID. It would look like https://example.org/node/12345/sub-page/321
. It also defines the call to the controller and the title_callback, essentially a location where we can create functions to manipulate the entity and its route. The options
define the things to pass into the controller and title callback functions, and we also define access permissions using requirements
.
One of the odd things about the controller and title_callback calls is that they look like a path, but are not. They have a predefined (and minimally documented) structure. You must do the following to make them work:
- Create two folders in your module:
src/Controller
(case is important). - Create a file called
TSSubPagesController.php
- this must match the call. - Define a class matching
TSSubPagesController
inTSSubPagesController.php
- Define a function matching
subPageParagraph
inside theTSSubPagesController
class.
Example below. The names of the controller file, class, and function are up to you, but they must have the same case, and the file and class must match.
Digging into the TSSubPagesController.php
file, we have a setup like so:
<span class="cp"><?php</span><span class="k">namespace</span> <span class="nx">Drupal\ts_sub_pages\Controller</span><span class="p">;</span><span class="k">use</span> <span class="nx">Drupal\Core\Controller\ControllerBase</span><span class="p">;</span><span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\Request</span><span class="p">;</span><span class="k">use</span> <span class="nx">Drupal\node\Entity\Node</span><span class="p">;</span><span class="k">use</span> <span class="nx">Drupal\paragraphs\Entity\Paragraph</span><span class="p">;</span><span class="sd">/** * TS Sub Pages controller. */</span><span class="k">class</span> <span class="nc">TSSubPagesController</span> <span class="k">extends</span> <span class="nx">ControllerBase</span> <span class="p">{</span> <span class="sd">/** * {@inheritdoc} */</span> <span class="k">public</span> <span class="k">function</span> <span class="nf">subPageParagraph</span><span class="p">(</span><span class="nx">Paragraph</span> <span class="nv">$paragraph</span><span class="p">,</span> <span class="nx">Node</span> <span class="nv">$node</span><span class="p">,</span> <span class="nx">Request</span> <span class="nv">$request</span><span class="p">)</span> <span class="p">{</span>
Here we have the namespace - this is our module. Note again that the src
is taken for granted. Next are the Symfony/Drupal use
statements, to pull in the classes/interfaces/traits we’ll need. Then we extend the ControllerBase
class with TSSubPagesController
, and define our subPageParagraph
function. The function pulls in the $node
and $paragraph
options we defined in ts_sub_pages.routing.yml
.
Now we can finally get to work on our sub-pages! Our goal here is to bring in the parent node header fields on every sub-page path. In the Drupal admin interface, go to ‘Manage Display’ for your content type. In our case it was /admin/structure/types/manage/location/display
. Scroll to the bottom and under ‘Custom display settings’ you’ll find a link to ‘Manage view modes’. We added a mode called sub-page
, and added all of the fields from our Location’s header.
Now we can bring that view of the node into the sub-page using the subPageParagraph
function we defined above:
<span class="cp"><?php</span><span class="k">public</span> <span class="k">function</span> <span class="nf">subPageParagraph</span><span class="p">(</span><span class="nx">Paragraph</span> <span class="nv">$paragraph</span><span class="p">,</span> <span class="nx">Node</span> <span class="nv">$node</span><span class="p">,</span> <span class="nx">Request</span> <span class="nv">$request</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$node_view_builder</span> <span class="o">=</span> <span class="nx">\Drupal</span><span class="o">::</span><span class="na">entityTypeManager</span><span class="p">()</span><span class="o">-></span><span class="na">getViewBuilder</span><span class="p">(</span><span class="s1">'node'</span><span class="p">);</span> <span class="nv">$node_header</span> <span class="o">=</span> <span class="nv">$node_view_builder</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="nv">$node</span><span class="p">,</span> <span class="s1">'sub_page'</span><span class="p">);</span> <span class="nv">$paragraph_view_builder</span> <span class="o">=</span> <span class="nx">\Drupal</span><span class="o">::</span><span class="na">entityTypeManager</span><span class="p">()</span><span class="o">-></span><span class="na">getViewBuilder</span><span class="p">(</span><span class="s1">'paragraph'</span><span class="p">);</span> <span class="nv">$paragraph_body</span> <span class="o">=</span> <span class="nv">$paragraph_view_builder</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="nv">$paragraph</span><span class="p">,</span> <span class="s1">'sub_page'</span><span class="p">);</span> <span class="k">return</span> <span class="p">[</span><span class="s1">'node'</span> <span class="o">=></span> <span class="nv">$node_header</span><span class="p">,</span> <span class="s1">'paragraph'</span> <span class="o">=></span> <span class="nv">$paragraph_body</span><span class="p">];</span><span class="p">}</span>
We get the node and paragraphs using getViewBuilder
, then the view modes for each. The node’s ‘sub-page’ view mode contains all of the header fields for the node, and the paragraph ‘sub-page’ view mode contains the paragraph body. We return these, and the result is what looks like a page when we visit the base paragraph url of /node/12345/sub-page/321
. The title is missing though, so we can add that with another small function inside the TSSubPagesController
class (we call it using the _title_callback
in ts_sub_pages.routing.yml
):
<span class="cp"><?php</span><span class="sd">/** * Returns a page title. */</span><span class="k">public</span> <span class="k">function</span> <span class="nf">getTitle</span><span class="p">(</span><span class="nx">Paragraph</span> <span class="nv">$paragraph</span><span class="p">,</span> <span class="nx">Node</span> <span class="nv">$node</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$node_title</span> <span class="o">=</span> <span class="nv">$node</span><span class="o">-></span><span class="na">getTitle</span><span class="p">();</span> <span class="nv">$paragraph_title</span> <span class="o">=</span> <span class="nv">$paragraph</span><span class="o">-></span><span class="na">field_title_text</span><span class="o">-></span><span class="na">value</span><span class="p">;</span> <span class="k">return</span> <span class="nv">$node_title</span> <span class="o">.</span> <span class="s1">' - '</span> <span class="o">.</span> <span class="nv">$paragraph_title</span><span class="p">;</span><span class="p">}</span>
Now we need to build a menu for our sub-pages. For this we can just use the ‘sub-pages’ paragraph field on the parent node. In the admin display, this field is how we add the sub-page paragraphs, but in the public-facing display, we use it to build the menu.
First, make sure you include it in the ‘default’ and ‘sub-page’ displays as a Rendered Entity, using the “Rendered as Entity” Formatter, which has widget configuration where you need to select the “Menu Link” view mode. When we set up the Paragraph, we put the Title field in the ‘Menu Link’ view. Now the field will display the titles of all the node’s sub-pages. To make them functional links, go to the ‘Menu Link’ view mode for your sub-page paragraph type, make the Title a ‘Linked Field’, and use the following widget configuration:
Destination: /node/[paragraph:parent_id]/sub-page/[paragraph:id]Title: [paragraph:field_title_text]
Next we need to account for the fact that the site uses URL aliases. A node called ‘main office’ will get a link such as /locations/main-office
via the Pathauto module. We want our sub-pages to use that path.
We do this by adding a URL Alias to the sub-page routes on creation (insert) or edit (update). In our module, we add the following functions to the ts_sub_pages.module
:
<span class="cp"><?php</span><span class="sd">/** * Implements hook_entity_insert(). */</span><span class="k">function</span> <span class="nf">ts_sub_pages_entity_insert</span><span class="p">(</span><span class="nx">EntityInterface</span> <span class="nv">$entity</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="nv">$entity</span><span class="o">-></span><span class="na">getEntityTypeId</span><span class="p">()</span> <span class="o">==</span> <span class="s1">'paragraph'</span> <span class="o">&&</span> <span class="nv">$entity</span><span class="o">-></span><span class="na">getType</span><span class="p">()</span> <span class="o">==</span> <span class="s2">"custom_subpage"</span><span class="p">)</span> <span class="p">{</span> <span class="nx">_ts_sub_pages_path_alias</span><span class="p">(</span><span class="nv">$entity</span><span class="p">);</span> <span class="p">}</span><span class="p">}</span><span class="sd">/** * Implements hook_entity_update(). */</span><span class="k">function</span> <span class="nf">ts_sub_pages_entity_update</span><span class="p">(</span><span class="nx">EntityInterface</span> <span class="nv">$entity</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="nv">$entity</span><span class="o">-></span><span class="na">getEntityTypeId</span><span class="p">()</span> <span class="o">==</span> <span class="s1">'paragraph'</span> <span class="o">&&</span> <span class="nv">$entity</span><span class="o">-></span><span class="na">getType</span><span class="p">()</span> <span class="o">==</span> <span class="s2">"custom_subpage"</span><span class="p">)</span> <span class="p">{</span> <span class="nx">_ts_sub_pages_path_alias</span><span class="p">(</span><span class="nv">$entity</span><span class="p">);</span> <span class="p">}</span><span class="p">}</span>
These get called every time we add or update the parent node. They call a custom function we define just below. It’s important to note that we have a custom title field field_title_text
defined - your title may be the Drupal default:
<span class="cp"><?php</span><span class="sd">/** * Custom function to create a sub-path alias. */</span><span class="k">function</span> <span class="nf">_ts_sub_pages_path_alias</span><span class="p">(</span><span class="nv">$entity</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$sub_page_slug</span> <span class="o">=</span> <span class="nx">Html</span><span class="o">::</span><span class="na">cleanCssIdentifier</span><span class="p">(</span><span class="nb">strtolower</span><span class="p">(</span><span class="nv">$entity</span><span class="o">-></span><span class="na">field_title_text</span><span class="o">-></span><span class="na">value</span><span class="p">));</span> <span class="nv">$node</span> <span class="o">=</span> <span class="nx">\Drupal</span><span class="o">::</span><span class="na">routeMatch</span><span class="p">()</span><span class="o">-></span><span class="na">getParameter</span><span class="p">(</span><span class="s1">'node'</span><span class="p">);</span> <span class="nv">$language</span> <span class="o">=</span> <span class="nx">\Drupal</span><span class="o">::</span><span class="na">languageManager</span><span class="p">()</span><span class="o">-></span><span class="na">getCurrentLanguage</span><span class="p">()</span><span class="o">-></span><span class="na">getId</span><span class="p">();</span> <span class="nv">$nid</span> <span class="o">=</span> <span class="nv">$node</span><span class="o">-></span><span class="na">id</span><span class="p">();</span> <span class="nv">$alias</span> <span class="o">=</span> <span class="nx">\Drupal</span><span class="o">::</span><span class="na">service</span><span class="p">(</span><span class="s1">'path.alias_manager'</span><span class="p">)</span><span class="o">-></span><span class="na">getAliasByPath</span><span class="p">(</span><span class="s1">'/node/'</span> <span class="o">.</span> <span class="nv">$nid</span><span class="p">);</span> <span class="nv">$system_path</span> <span class="o">=</span> <span class="s2">"/node/"</span> <span class="o">.</span> <span class="nv">$nid</span> <span class="o">.</span> <span class="s2">"/sub-page/"</span> <span class="o">.</span> <span class="nv">$entity</span><span class="o">-></span><span class="na">id</span><span class="p">();</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">\Drupal</span><span class="o">::</span><span class="na">service</span><span class="p">(</span><span class="s1">'path.alias_storage'</span><span class="p">)</span><span class="o">-></span><span class="na">aliasExists</span><span class="p">(</span><span class="nv">$alias</span> <span class="o">.</span> <span class="s2">"/"</span> <span class="o">.</span> <span class="nv">$sub_page_slug</span><span class="p">,</span> <span class="nv">$language</span><span class="p">))</span> <span class="p">{</span> <span class="nx">\Drupal</span><span class="o">::</span><span class="na">service</span><span class="p">(</span><span class="s1">'path.alias_storage'</span><span class="p">)</span> <span class="o">-></span><span class="na">save</span><span class="p">(</span><span class="nv">$system_path</span><span class="p">,</span> <span class="nv">$alias</span> <span class="o">.</span> <span class="s2">"/"</span> <span class="o">.</span> <span class="nv">$sub_page_slug</span><span class="p">,</span> <span class="nv">$language</span><span class="p">);</span> <span class="p">}</span><span class="p">}</span>
This function gets the sub-page paragraph title, and creates a URL-friendly slug. It then loads the paragraph’s node, gets the current language, ID, and alias. We also build the system path of the sub-page, as that’s necessary for the url_alias
table in the Drupal database. Finally, we check that there’s no existing path that matches ours, and add it. This will leave old URL aliases, so if someone had bookmarked a sub-page and the name changes, it will still go to the correct sub-page.
Now we can add the ‘Home’ link and indicate when a sub-page is active. For that we’ll use a custom twig template. The field.html.twig
default file is the starting point, it’s located in core/themes/classy/templates/field/
. Copy and rename it to your theme’s template directory. Based on the field name, this can be called field--field-sub-pages.html.twig
.
The part of the twig file we’re interested in is here:
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="ow">in</span> <span class="nv">items</span> <span class="cp">%}</span> <span class="nt"><div</span><span class="cp">{{</span> <span class="nv">item.attributes.addClass</span><span class="p">(</span><span class="s1">'field__item'</span><span class="p">)</span> <span class="cp">}}</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">item.content</span> <span class="cp">}}</span><span class="nt"></div></span><span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
This occurs three times in the template, to account for multiple fields, labels, etc. Just before each of the for
loops, we add the following ‘home’ link code:
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">url</span><span class="p">(</span><span class="s1">'<current>'</span><span class="p">)[</span><span class="s1">'#markup'</span><span class="p">]</span> <span class="k">ends</span> <span class="k">with</span> <span class="nv">node_path</span> <span class="cp">%}</span> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"field__item active"</span> <span class="na">tabindex=</span><span class="s">"0"</span><span class="nt">></span>Home<span class="nt"></div></span><span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"field__item"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">node_path</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span>Home<span class="nt"></a></div></span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
Next, we make some substantial changes to the loop:
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">sub_text</span> <span class="err">=</span> <span class="nv">item.content</span><span class="p">[</span><span class="s1">'#paragraph'</span><span class="p">]</span><span class="err">.</span><span class="nv">field_title_text.0.value</span> <span class="cp">%}</span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">sub_path</span> <span class="err">=</span> <span class="nv">node_path</span> <span class="err">~</span> <span class="s1">'/'</span> <span class="err">~</span> <span class="nv">sub_text</span><span class="o">|</span><span class="nf">clean_class</span> <span class="cp">%}</span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">url</span><span class="p">(</span><span class="s1">'<current>'</span><span class="p">)[</span><span class="s1">'#markup'</span><span class="p">]</span> <span class="k">ends</span> <span class="k">with</span> <span class="nv">sub_path</span> <span class="cp">%}</span><span class="nt"><li</span><span class="cp">{{</span> <span class="nv">item.attributes.addClass</span><span class="p">(</span><span class="s1">'field__item'</span><span class="p">,</span> <span class="s1">'menu-item'</span><span class="p">,</span> <span class="s1">'active'</span><span class="p">)</span> <span class="cp">}}</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">sub_text</span> <span class="cp">}}</span><span class="nt"></li></span><span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span><span class="nt"><li</span><span class="cp">{{</span> <span class="nv">item.attributes.addClass</span><span class="p">(</span><span class="s1">'field__item'</span><span class="p">,</span> <span class="s1">'menu-item'</span><span class="p">)</span> <span class="cp">}}</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">sub_path</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">sub_text</span> <span class="cp">}}</span><span class="nt"></a></li></span>
Here, sub_text
gets the sub-page title, and sub_path
the path of each sub-page. We then check if the current url ends with the path, and if so, we add the active class and remove the link.
And that’s it! The client can now add as many custom sub-pages as they like. They’ll always pick up the parent node’s base path so they will be printable, direct links. They’ll have the same header as the parent node, and they will automatically be added or deleted from the node’s custom context-aware menu.
Hmm, maybe this would make a good contributed module?