Same filename and directory in other branches
  1. 10 core/modules/rest/tests/modules/config_test_rest/config_test_rest.module
  2. 9 core/modules/rest/tests/modules/config_test_rest/config_test_rest.module

Contains hook implementations for testing REST module.

File

core/modules/rest/tests/modules/config_test_rest/config_test_rest.module
View source
<?php

/**
 * @file
 * Contains hook implementations for testing REST module.
 */
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultReasonInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_entity_type_alter().
 */
function config_test_rest_entity_type_alter(array &$entity_types) {

  // Undo part of what config_test_entity_type_alter() did: remove this
  // config_test_no_status entity type, because it uses the same entity class as
  // the config_test entity type, which makes REST deserialization impossible.
  unset($entity_types['config_test_no_status']);
}

/**
 * Implements hook_ENTITY_TYPE_access().
 */
function config_test_rest_config_test_access(EntityInterface $entity, $operation, AccountInterface $account) {

  // Add permission, so that EntityResourceTestBase's scenarios can test access
  // being denied. By default, all access is always allowed for the config_test
  // config entity.
  $access_result = AccessResult::forbiddenIf(!$account
    ->hasPermission('view config_test'))
    ->cachePerPermissions();
  if (!$access_result
    ->isAllowed() && $access_result instanceof AccessResultReasonInterface) {
    $access_result
      ->setReason("The 'view config_test' permission is required.");
  }
  return $access_result;
}

Functions