You are here

public function NodeTypeAccessService::hookQueryNodeAccessAlter in Nodetype access 8

File

src/NodeTypeAccessService.php, line 89

Class

NodeTypeAccessService

Namespace

Drupal\nodetype_access

Code

public function hookQueryNodeAccessAlter(AlterableInterface $query) {
  $account = $query
    ->getMetaData('account') ?? \Drupal::currentUser();
  $op = $query
    ->getMetaData('op') ?? 'view';
  if ($query instanceof SelectInterface && $op === 'view') {
    $nodeTableAlias = $this
      ->extractBaseTableAlias($query);

    // Bail out if the base table is missing.
    if (!$nodeTableAlias) {
      throw new \Exception('Query tagged for node access but there is no node table, specify the base_table using meta data.');
    }
    $permittedBundleIds = $this
      ->permittedBundleIds($account);
    if ($permittedBundleIds) {
      $query
        ->condition("{$nodeTableAlias}.type", $permittedBundleIds, 'IN');
    }
    else {
      $query
        ->alwaysFalse();
    }
  }
}