public function FarmAssetChildrenViewsAccessCheck::access in farmOS 2.x
A custom access check.
Parameters
\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match.
File
- modules/
core/ ui/ views/ src/ Access/ FarmAssetChildrenViewsAccessCheck.php, line 49
Class
- FarmAssetChildrenViewsAccessCheck
- Checks access for displaying Views of asset children.
Namespace
Drupal\farm_ui_views\AccessCode
public function access(RouteMatchInterface $route_match) {
// If there is no "asset" parameter, bail.
$asset_id = $route_match
->getParameter('asset');
if (empty($asset_id)) {
return AccessResult::allowed();
}
// If the asset is a location, deny access.
/** @var \Drupal\asset\Entity\AssetInterface $asset */
$asset = $this->assetStorage
->load($asset_id);
if ($this->assetLocation
->isLocation($asset)) {
return AccessResult::forbidden();
}
// Run a count query to see if there are any assets that reference this
// asset as a parent.
$count = $this->assetStorage
->getAggregateQuery()
->accessCheck(TRUE)
->condition('parent.entity.id', $asset_id)
->count()
->execute();
// Determine access based on the child count.
$access = AccessResult::allowedIf($count > 0);
// Invalidate the access result when assets are changed.
$access
->addCacheTags([
"asset_list",
]);
return $access;
}