Skip to content

Add Node Entity Caster for Views Driven %node paths #348

@DiegoPino

Description

@DiegoPino

What?

Currently it is impossible to add a Tab driven by a View with a /node/%node/somepath page path and using access control at the same time. The reason is: the way parameter casting/route enriching in Drupal (for the last 8 years reported) keeps the %node as a string instead of a NodeInterface, as it should ,bc no parameter upcasting survives the access control logic at the route level of a view.

See https://www.drupal.org/project/drupal/issues/2528166 (and see the last patch, it is basically moving parameter bag setting from one place to another). We need to have NodeInterface upcasting on our active display route because of our need to enrich that Route with the View Mode and the Bundle so we can load the View Mode Settings Form.

The solution is not that complex. basically identify if a %node is being called by a View Generated Route and intercept in a classy/elegant way here:

$params = parent::getRouteParameters($route_match);
$node = $route_match->getParameter('node');
// Users can override the Viewer manually (if DS is enabled)
if ($node instanceof NodeInterface) {
if ($node->hasField('ds_switch') && !empty($node->ds_switch->value)) {
$viewmode = $node->ds_switch->value;
}
else {
$viewmode = $this->viewModeResolver->get($node);
}
$params += ['bundle' => $node->bundle(), 'node' => $node->id(), 'view_mode_name' => $viewmode];
}
return $params;
by adding an extra check, loading the Node and then adding the missing arguments.

Tested an initial (not classy, not elegant) implementation and it solves the issue.

Funny enough, I also found this sloppy code here
https://git.drupalcode.org/project/bootstrap_barrio/-/blob/5.x/bootstrap_barrio.theme#L595-605

This one fails if one tries to access a Valid Route with a non existing Drupal Node ID. Gosh people. So easy as not trying to access a Property on a NULL.

Anyhow. This will be fixed tomorrow so I can keep site building for 1.1.0 (1.1.1 by now)

Metadata

Metadata

Assignees

Labels

Drupal 10Upgrade economyDrupal 9Everyone's favorite version until Drupal 10 comesDrupal ViewsAsk and you should receiveEvents and Subscribersget 2 printed issues for freeView ModeMany fields, each one with a formatter that renders your ADOsbugSomething isn't workingenhancementNew feature or requestexternal bugIt is not us, it is them

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions