0

I have a view displaying list of latest node revisions.

The view has title field.

The website gives users permissions to access node/{nid}(shows latest revision if published default revision doesn't exist) and node/{nid}/latest (shows latest revision if defaut revision and latest revision is different) depending moderation state and user groups.

I want to override the view's title field according to the rules below:

  • if current user can access node/{nid}/latest the title links to the page
  • else if current user can access node/{nid} the title links to the page
  • else show only title text

Dozens or hundreds of people may simultaneously access the view so I want to keep it fast to display. What is the fastest way to check those?

edit: I searched about hook_node_grants but it only supports view/update/delete operations so I think it can't hold data for node/{nid}/latest. Should I store every pattern in table just like node_access table does and look up on it when view is updated?

  • 2
    Create 3 diff version of your block view. Change the Access for each to permission or user role whatever makes more sense to you. Cache your view, should be already turned on. – No Sssweat Nov 12 '19 at 8:34
2

If you know the route name you can check access in PHP. For the second link you can use this route:

\Drupal::service('access_manager')->checkNamedRoute('entity.node.canonical', ['node' => '1'])).

You can find the first one with the help of the Devel module when you click on Current Route Info while displaying the content and look for [_route].

I would put the nid into the title field and override the field content in a preprocess hook. Display the link for each route if the access check returns true. Caching is no problem because caching is by default permission based.

| improve this answer | |
  • It works like a charm! I made a field plugin using the service. Really appreciate your help! – 863 Nov 23 '19 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.