drupal 9 custom route access checking code example
Example: drupal 9 custom access checking for routes
<?php
namespace Drupal\mymod\Access;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\mymod\StoreService;
class CardEditAccessChecker implements AccessInterface {
private $ss = NULL;
public function __construct(StoreService $ss) {
$this->ss = $ss;
}
public function access($product_id) {
if ($this->ss->isMarketPlaceManager()) {
return AccessResult::allowed();
}
if ($this->ss->ownsProduct($product_id)) {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}
}