You are here

public function FarmInventoryAssetViewsAccessCheck::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/FarmInventoryAssetViewsAccessCheck.php, line 38

Class

FarmInventoryAssetViewsAccessCheck
Checks access for displaying Views of inventory quantities for an asset.

Namespace

Drupal\farm_ui_views\Access

Code

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();
  }

  // Allow access if the asset has an inventory.

  /** @var \Drupal\asset\Entity\AssetInterface $asset */
  $asset = $this->assetStorage
    ->load($asset_id);
  $access = AccessResult::allowedIf($asset
    ->hasField('inventory') && !$asset
    ->get('inventory')
    ->isEmpty());

  // Invalidate the access result when the asset is changed.
  $access
    ->addCacheTags($asset
    ->getCacheTags());
  return $access;
}