You are here

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

Class

FarmAssetLogViewsAccessCheck
Checks access for displaying Views of logs that reference assets.

Namespace

Drupal\farm_ui_views\Access

Code

public function access(RouteMatchInterface $route_match) {

  // If there is no "asset" or "log_type" parameter, bail.
  $asset_id = $route_match
    ->getParameter('asset');
  $log_type = $route_match
    ->getParameter('log_type');
  if (empty($asset_id) || empty($log_type)) {
    return AccessResult::allowed();
  }

  // If the log type is "all", bail.
  if ($log_type == 'all') {
    return AccessResult::allowed();
  }

  // Build a count query for logs of this type.
  $query = $this->logStorage
    ->getAggregateQuery()
    ->accessCheck(TRUE)
    ->condition('type', $log_type)
    ->count();

  // Only include logs that reference the asset.
  $reference_condition = $query
    ->orConditionGroup()
    ->condition('asset.entity.id', $asset_id)
    ->condition('location.entity.id', $asset_id);
  $query
    ->condition($reference_condition);

  // Determine access based on the log count.
  $count = $query
    ->execute();
  $access = AccessResult::allowedIf($count > 0);

  // Invalidate the access result when logs of this bundle are changed.
  $access
    ->addCacheTags([
    "log_list:{$log_type}",
  ]);
  return $access;
}