1

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

6
  • You could try to build a standard entity route with upcasting, Views recycles existing routes which should preserve the upcasting.
    – 4uk4
    Commented May 6, 2022 at 19:45
  • How would that work? I make an empty route definition, then re-use that path in Views?
    – Kevin
    Commented May 6, 2022 at 20:26
  • 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.
    – 4uk4
    Commented May 6, 2022 at 20:34
  • 1
    I see - I did not know you could do that.
    – Kevin
    Commented May 6, 2022 at 20:47
  • @4uk4 - is that an actual feature? Is that something that might get "fixed" later?
    – sonfd
    Commented May 6, 2022 at 21:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.