I'm working on a node access module. I've set up my logic. I can't seem to get my code to return AccessResult::forbidden(). I can't even get \dpm to run in access method. What am I doing wrong? Below are my routing.yml and controller.
/* nodeaccess.routing.yml */
nodeaccess.people:
path: '/user/{uid}/edit'
defaults:
_controller: '\Drupal\nodeaccess\Controller\NodeaccessController::content'
_title: 'Dashboard'
requirements:
_custom_access: '\Drupal\nodeaccess\Controller\NodeaccessController::access'
/* src/Controller/NodeAccessController.php */
<?php
namespace Drupal\nodeaccess\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;
/**
* Defines the Dashboard controller.
*/
class NodeaccessController extends ControllerBase {
/**
* Returns content for this controller.
*/
public function content() {
$today = date("l");
if ($today == "Thursday") {
// Return 403 Access Denied page.
\dpm("You are not allowed.");
}
}
/**
* Checks access for this controller.
*/
public function access(AccountInterface $account) {
$current_user = User::load(Drupal::currentUser()->id());
$user = User::load(explode("/", $_SERVER["REQUEST_URI"])[1]);
if ($current_user->field_department->target_id != $user->field_department->target_id) {
return AccessResult::forbidden();
}
return AccessResult::allowed();
}
}
AccountInterface $account
nodeacess.routing.yml
file, the_custom_access
is incorrectly indented. Also, check this drupal.org/docs/8/api/routing-system/access-checking-on-routes/…