Problem/Motivation
When you enabled revisions on a node type and you want to view that revision you will get a fatal error / WSOD, e.g. /node/1234/revisions/12345/view
The reason is that
$node = \Drupal::routeMatch()->getParameter('node')
returns a string with the node id instead of the node object. Therefore the $node->getType() call fails. It's reported in Drupal core at #2730631: Upcast node and node_revision parameters of node revision routes.
Steps to reproduce
Proposed resolution
To avoid the problem wrap it into a condition.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
if ($node instanceof NodeInterface) {
$suggestions[] = 'page__node__' . $node->getType();
}
}
If the suggestion is also relevant for the revision page, then you might want to load the full node object.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
if ($revision_id = \Drupal::routeMatch()->getParameter('node_revision')) {
if ($node_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision_id)) {
$node = $node_revision;
}
}
if ($node instanceof NodeInterface) {
$suggestions[] = 'page__node__' . $node->getType();
}
}
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #3
doxigo commentedThanks for the catch, fixed based on the talk we had