You are here

function domain_access_node_access_records in Domain Access 8

Implements hook_node_access_records().

File

domain_access/domain_access.module, line 82
Domain-based access control for content.

Code

function domain_access_node_access_records(NodeInterface $node) {
  $grants = [];

  // Create grants for each translation of the node. See the report at
  // https://www.drupal.org/node/2825419 for the logic here. Note that right
  // now, grants may not be the same for all languages.
  $translations = $node
    ->getTranslationLanguages();
  foreach ($translations as $langcode => $language) {
    $translation = $node
      ->getTranslation($langcode);

    // If there are no domains set, use the current one.
    $domains = \Drupal::service('domain_access.manager')
      ->getAccessValues($translation);

    /** @var \Drupal\domain\DomainInterface $active */
    if (empty($domains) && ($active = \Drupal::service('domain.negotiator')
      ->getActiveDomain())) {
      $domains[$active
        ->id()] = $active
        ->getDomainId();
    }
    foreach ($domains as $id => $domainId) {

      /** @var \Drupal\domain\DomainInterface $domain */
      if ($domain = \Drupal::entityTypeManager()
        ->getStorage('domain')
        ->load($id)) {
        $grants[] = [
          'realm' => $translation
            ->isPublished() ? 'domain_id' : 'domain_unpublished',
          'gid' => $domain
            ->getDomainId(),
          'grant_view' => 1,
          'grant_update' => 1,
          'grant_delete' => 1,
          'langcode' => $langcode,
        ];
      }
    }

    // Set the domain_site grant.
    if ($translation
      ->hasField(DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD) && !empty($translation
      ->get(DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD)->value) && $translation
      ->isPublished()) {
      $grants[] = [
        'realm' => 'domain_site',
        'gid' => 0,
        'grant_view' => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
        'langcode' => $langcode,
      ];
    }
    else {
      $grants[] = [
        'realm' => 'domain_site',
        'gid' => 1,
        'grant_view' => 0,
        'grant_update' => 0,
        'grant_delete' => 0,
        'langcode' => $langcode,
      ];
    }
  }
  return $grants;
}