Problem/Motivation

In Drupal 7 one could create a view with a page with a path node/%/something, add a node reference field as a contextual filter, give that filter a basic validation criteria and finally give the page a menu as a tab.
This would result in menu tab next to "view" and "edit" only on nodes that fit the validation criteria.

In Drupal 8 the tab is shown on all nodes, but clicking the tab correctly results in a "Page not found" (or access denied if its set to display that).

Steps to reproduce:

  1. Install the standard profile.
  2. Add a content entity reference field to the article content type and choose to allow Basic page for reference type.
  3. Add a view with a page display
  4. Set the path to node/%node/articles *
  5. Set the menu to "menu tab"
  6. Add a contextual filter with the field added in step 2.
  7. Add validation criteria for the contextual filter and set it to: Content validator and "Basic page" bundle
  8. Set "page not found" for: Action to take if filter value does not validate
  9. Create some content and view a Basic page which has some related articles. Verify that the tab is there
  10. Visit an article content page. - The tab will show up here too

* don't try node/%/articles because it will blow up.
see: #2489940: Views path with menu tab does not validate and results in fatal error

As stated, clicking the tab on article pages, results in a 404 (page not found), since it redirects to "node/[nid]/articles",
which is a non-existing route in this context (it only exists when the nid matches a basic page, and here it is an article).

This is likely caused since menu tabs are created independently of contextual filters and are not aware of
their logic and whether they are valid or not in a given context.
Moreover, while views is altering and adding it's own local tasks,
the local tabs displayed are actually being rendered by the core menu block plugin, which is not aware of the context either.

Proposed resolution

Some thoughts:
Seems that solving this issue would require the menu block plugin to somehow be aware of the context which views defines.
This would also have to somehow account for the various configuration options contextual filters offer, and also handle cache.

Remaining tasks

User interface changes

no

API changes

probably not..

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because it is a functional regression.
Issue priority Normal or Major because it is a functional regression??

Comments

bircher’s picture

Issue summary: View changes
pelegr’s picture

Issue tags: +Barcelona2015

Working with @rcodina on the major issue triage at DrupalCon Barcelona 2015

pelegr’s picture

Issue summary: View changes
pelegr’s picture

Issue summary: View changes
pelegr’s picture

Issue summary: View changes
StatusFileSize
new98.84 KB
new83.89 KB
pelegr’s picture

Issue summary: View changes
tim.plunkett’s picture

Issue tags: +Triaged core major

Discussed with @alexpott, @dawehner, @effulgentsia, and @xjm.
The fact that it shows links that will always 404, and since this is a regression from D7, and it has no reasonable or known workaround, this is definitely a major bug.

dawehner’s picture

Just by looking into the local task subsystem, I think this would actually even require a change in there. This seems not possible at the moment, to be honest.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

megachriz’s picture

Possible workaround:

  1. Install the Views Advanced Routing module.
  2. Enable 'Views Advanced Routing' as described on the project page.
  3. On the Views route settings, set a requirement, for example:
    _custom_access: '\Drupal\mymodule\Controller\MyController::access'
  4. If the requirement is of type '_custom_access', implement this access checker.
    Example access checker for displaying the menu tab only on nodes of type 'article':
    
    namespace Drupal\mymodule\Controller;
    
    use Drupal\node\Entity\Node;
    use Drupal\Core\Access\AccessResult;
    
    /**
     * Controls access for the view 'myview'.
     */
    class MyController {
    
      /**
       * Checks access for the view 'myview'.
       *
       * @param int $node
       *   The node ID.
       */
      public function access($node) {
        $node_object = Node::load($node);
    
        if (!$node_object) {
          return AccessResult::forbidden(strtr('Node @node_id not found.', [
            '@node_id' => $node,
          ]));
        }
    
        if ($node_object->bundle() != 'article') {
          return AccessResult::forbidden(strtr('Node @node_id is not an article.', [
            '@node_id' => $node,
          ]));
        }
    
        // In all other cases, access is granted.
        return AccessResult::allowed();
      }
    
    }
    

Caveats of this workaround:

  • accessing the view page for an invalid object will result into a '403 Forbidden' instead of what was configured on the view (for example a '404 Not Found').
  • The parameter name ($node in the example) MUST exactly match the parameter name configured on the View's path (for example: /node/%node/something).

Access check service

Instead of using '_custom_access' as access checker, you can also implement an 'access_check' service. See for an example the RNG module.
From rng.services.yml:

access_check.rng.entity_is_event:
  class: Drupal\rng\Access\EntityIsEventCheck
  arguments: ['@rng.event_manager']
  tags:
    - { name: access_check, applies_to: _entity_is_event }

More information about access checking on routes:
https://www.drupal.org/node/2122195

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wipeout_dude’s picture

Hi,
I have come across this issue today on a new D8 site.. Is there a fix or workaround for this issue?
Thanks..

tetranz’s picture

I think this is a duplicate of https://www.drupal.org/project/drupal/issues/2778345

I'd have to revisit it to get my head around it again and update it for 8.7 but I seem to remember that my patch worked.

tetranz’s picture

This is definitely a duplicate of 2778345. I've just tested it and it still works although the patch failed to apply. I had to add the lines manually to views.services.yml.

I'll clean it up and hopefully get it in better shape for 8.7.

tetranz’s picture

Issue tags: +duplicate

I've updated https://www.drupal.org/project/drupal/issues/2778345

I believe that solves this.

twod’s picture

Status: Active » Needs review
Issue tags: -duplicate
StatusFileSize
new13.41 KB

This issue is actually older (and already determined to be major as per #7) so let's mark the other one a duplicate. Moving @tetranz's patch here for convenience.

twod’s picture

Title: Show views menu tab depending on argument validation » View argument validators are not taken into account in access checks
Status: Needs review » Closed (duplicate)

Never mind, the other issue is more to the point, sorry about that. Escalating it instead.