9839edbf39a180e2305f516da95c3118265fa219
[yaffs-website] / web / core / modules / rest / tests / modules / rest_test / rest_test.module
1 <?php
2
3 /**
4  * @file
5  * Contains hook implementations for testing REST module.
6  */
7
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Field\BaseFieldDefinition;
10 use Drupal\Core\Field\FieldDefinitionInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Drupal\Core\Field\FieldItemListInterface;
13 use Drupal\Core\Access\AccessResult;
14
15 /**
16  * Implements hook_entity_field_access().
17  *
18  * @see \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::setUp()
19  */
20 function rest_test_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
21   // @see \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testPost()
22   // @see \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testPatch()
23   if ($field_definition->getName() === 'field_rest_test') {
24     switch ($operation) {
25       case 'view':
26         // Never ever allow this field to be viewed: this lets
27         // EntityResourceTestBase::testGet() test in a "vanilla" way.
28         return AccessResult::forbidden();
29       case 'edit':
30         return AccessResult::forbidden();
31     }
32   }
33
34   // @see \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testGet()
35   // @see \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testPatch()
36   if ($field_definition->getName() === 'field_rest_test_multivalue') {
37     switch ($operation) {
38       case 'view':
39         // Never ever allow this field to be viewed: this lets
40         // EntityResourceTestBase::testGet() test in a "vanilla" way.
41         return AccessResult::forbidden();
42     }
43   }
44
45   // @see \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testGet()
46   // @see \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testPatch()
47   if ($field_definition->getName() === 'rest_test_validation') {
48     switch ($operation) {
49       case 'view':
50         // Never ever allow this field to be viewed: this lets
51         // EntityResourceTestBase::testGet() test in a "vanilla" way.
52         return AccessResult::forbidden();
53     }
54   }
55
56   // No opinion.
57   return AccessResult::neutral();
58 }
59
60 /**
61  * Implements hook_entity_base_field_info().
62  */
63 function rest_test_entity_base_field_info(EntityTypeInterface $entity_type) {
64   $fields = [];
65   $fields['rest_test_validation'] = BaseFieldDefinition::create('string')
66     ->setLabel(t('REST test validation field'))
67     ->setDescription(t('A text field with some special validations attached used for testing purposes'))
68     ->addConstraint('rest_test_validation');
69
70   return $fields;
71 }