cd136538f242c77ef0de4bcc3715cd93558e08d4
[yaffs-website] / web / core / modules / views / tests / modules / views_entity_test / views_entity_test.module
1 <?php
2
3 /**
4  * @file
5  * Contains main module functionality.
6  */
7
8 use Drupal\Core\Access\AccessResult;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Field\BaseFieldDefinition;
11 use Drupal\Core\Field\FieldDefinitionInterface;
12 use Drupal\Core\Field\FieldItemListInterface;
13 use Drupal\Core\Session\AccountInterface;
14
15
16 /**
17  * Implements hook_entity_bundle_field_info().
18  */
19 function views_entity_test_entity_base_field_info(EntityTypeInterface $entity_type) {
20   if ($entity_type->id() == 'entity_test') {
21     $definitions['test_text_access'] = BaseFieldDefinition::create('string')
22       ->setLabel(t('Test access'))
23       ->setTranslatable(FALSE)
24       ->setSetting('max_length', 64)
25       ->setDisplayOptions('form', [
26         'type' => 'string_textfield',
27         'weight' => 10,
28       ]);
29     return $definitions;
30   }
31 }
32
33 /**
34  * Implements hook_entity_field_access().
35  *
36  * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
37  */
38 function views_entity_test_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
39   if ($field_definition->getName() == 'test_text_access') {
40     if ($items) {
41       if ($items->value == 'no access value') {
42         return AccessResult::forbidden()->addCacheableDependency($items->getEntity());
43       }
44     }
45   }
46   // No opinion.
47   return AccessResult::neutral();
48 }