Score:1

View with a path of api/VERSION/foo/bar/%node - %node is not being upcast to a Node object

in flag

I am trying to create a handful of Views with a REST Export display. They have paths set behind 'api/.../.../.../%node' where %node will be an ID sent to the request to be upcasted. The basic View has a contextual filter of Content ID (excluded), and then "has any term" from this node.

However, in the contextual filter for "Taxonomy Term ID from URL" with "Load terms from the node page":

      if (($node = $this->routeMatch->getParameter('node')) && $node instanceof NodeInterface) {
        $entity = $node;
      }

This check fails, and thus the nodes terms are not checked.

Do I need a custom parameter for this purpose to tell the system what to look for? I figured as long as the ID is a node, the upcast will still occur but it is not.

Here is what xdebug sees in ViewPageController for route params and arguments:

enter image description here

7611 is the node ID, what part of the process should or usually would upcast this to a Node object?

If I do this in the Views TID argument, of course, then I have a Node:

    // Load default argument from node.
    if (!empty($this->options['node'])) {
      // Just check, if a node could be detected.
      if (($node = $this->routeMatch->getParameter('node')) && $node instanceof NodeInterface) {
        $entity = $node;
      }

      if (($node = $this->routeMatch->getParameter('node')) && !empty($node) && !($node instanceof NodeInterface)) {
        $entity = \Drupal::entityTypeManager()->getStorage('node')->load($node);
      }
    }

Edit: possibly related: https://www.drupal.org/project/drupal/issues/2528166

4uk4 avatar
cn flag
You could try to build a standard entity route with upcasting, Views recycles existing routes which should preserve the upcasting.
Kevin avatar
in flag
How would that work? I make an empty route definition, then re-use that path in Views?
4uk4 avatar
cn flag
Yes, I know this would only be a workaround. Otherwise there is nothing we can do here, you've already found the issue which needs to be fixed.
Kevin avatar
in flag
I see - I did not know you could do that.
sonfd avatar
in flag
@4uk4 - is that an actual feature? Is that something that might get "fixed" later?
sonfd avatar
in flag
Could you alter your view's route to tell it to upcast the parameter? https://drupal.stackexchange.com/questions/81362/how-do-i-alter-the-route-defined-by-another-module
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.