You are here

class FarmLocationAssetViewsAccessCheck in farmOS 2.x

Checks access for displaying Views of assets in a location.

Hierarchy

Expanded class hierarchy of FarmLocationAssetViewsAccessCheck

1 string reference to 'FarmLocationAssetViewsAccessCheck'
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 FarmLocationAssetViewsAccessCheck
farm_ui_views.location_assets_access in modules/core/ui/views/farm_ui_views.services.yml
Drupal\farm_ui_views\Access\FarmLocationAssetViewsAccessCheck

File

modules/core/ui/views/src/Access/FarmLocationAssetViewsAccessCheck.php, line 14

Namespace

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

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

  /**
   * The asset location service.
   *
   * @var \Drupal\farm_location\AssetLocationInterface
   */
  protected $assetLocation;

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

  /**
   * 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 is a location.

    /** @var \Drupal\asset\Entity\AssetInterface $asset */
    $asset = $this->assetStorage
      ->load($asset_id);
    $access = AccessResult::allowedIf($this->assetLocation
      ->isLocation($asset));

    // Invalidate the access result when assets are changed.
    $access
      ->addCacheTags([
      "asset_list",
    ]);
    return $access;
  }

}

Members

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