node_access_test_language.module

Same filename in other branches
  1. 9 core/modules/node/tests/modules/node_access_test_language/node_access_test_language.module
  2. 8.9.x core/modules/node/tests/modules/node_access_test_language/node_access_test_language.module
  3. 11.x core/modules/node/tests/modules/node_access_test_language/node_access_test_language.module

Test module with a language-aware node access implementation.

The module adds a 'private' field to page nodes that allows each translation of the node to be marked as private (viewable only by administrators).

File

core/modules/node/tests/modules/node_access_test_language/node_access_test_language.module

View source
<?php


/**
 * @file
 * Test module with a language-aware node access implementation.
 *
 * The module adds a 'private' field to page nodes that allows each translation
 * of the node to be marked as private (viewable only by administrators).
 */
use Drupal\node\NodeInterface;

/**
 * Implements hook_node_grants().
 *
 * This module defines a single grant realm. All users belong to this group.
 */
function node_access_test_language_node_grants($account, $operation) {
    $grants['node_access_language_test'] = [
        7888,
    ];
    return $grants;
}

/**
 * Implements hook_node_access_records().
 */
function node_access_test_language_node_access_records(NodeInterface $node) {
    $grants = [];
    // Create grants for each translation of the node.
    foreach ($node->getTranslationLanguages() as $langcode => $language) {
        // If the translation is not marked as private, grant access.
        $translation = $node->getTranslation($langcode);
        $grants[] = [
            'realm' => 'node_access_language_test',
            'gid' => 7888,
            'grant_view' => empty($translation->field_private->value) ? 1 : 0,
            'grant_update' => 0,
            'grant_delete' => 0,
            'priority' => 0,
            'langcode' => $langcode,
        ];
    }
    return $grants;
}

Functions

Title Deprecated Summary
node_access_test_language_node_access_records Implements hook_node_access_records().
node_access_test_language_node_grants Implements hook_node_grants().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.