Checking entity access
In this recipe, we will demonstrate how to check whether the current user has access to view an article node. In previous recipes throughout the book, we have used the _entity_access
route requirement to perform entity access checks. This recipe will use its own entity access control so that the response is a 404 Not Found
response instead of a 403
Forbidden
response.
How to do it…
- Create a
get
method in theArticleController
controller in your module that has a parameter for thenode
entity object that will be provided by a route parameter:<?php
namespace Drupal\mymodule\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\NodeInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class ArticleController extends ControllerBase {
public function get(NodeInterface $node):
JsonResponse {
}
}
If using the same controller from previous...