You are here

class FarmInventoryAssetViewsAccessCheck in farmOS 2.x

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

Hierarchy

Expanded class hierarchy of FarmInventoryAssetViewsAccessCheck

1 string reference to 'FarmInventoryAssetViewsAccessCheck'
farm_ui_views.services.yml in modules/core/ui/views/farm_ui_views.services.yml
modules/core/ui/views/farm_ui_views.services.yml
1 service uses FarmInventoryAssetViewsAccessCheck
farm_ui_views.asset_inventory_access in modules/core/ui/views/farm_ui_views.services.yml
Drupal\farm_ui_views\Access\FarmInventoryAssetViewsAccessCheck

File

modules/core/ui/views/src/Access/FarmInventoryAssetViewsAccessCheck.php, line 13

Namespace

Drupal\farm_ui_views\Access
View source
class FarmInventoryAssetViewsAccessCheck implements AccessInterface {

  /**
   * The asset storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $assetStorage;

  /**
   * FarmInventoryAssetViewsAccessCheck constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->assetStorage = $entity_type_manager
      ->getStorage('asset');
  }

  /**
   * A custom access check.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FarmInventoryAssetViewsAccessCheck::$assetStorage protected property The asset storage.
FarmInventoryAssetViewsAccessCheck::access public function A custom access check.
FarmInventoryAssetViewsAccessCheck::__construct public function FarmInventoryAssetViewsAccessCheck constructor.