Pull merge.
[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  * Implements hook_entity_bundle_field_info().
17  */
18 function views_entity_test_entity_base_field_info(EntityTypeInterface $entity_type) {
19   if ($entity_type->id() == 'entity_test') {
20     $definitions['test_text_access'] = BaseFieldDefinition::create('string')
21       ->setLabel(t('Test access'))
22       ->setTranslatable(FALSE)
23       ->setSetting('max_length', 64)
24       ->setDisplayOptions('form', [
25         'type' => 'string_textfield',
26         'weight' => 10,
27       ]);
28     return $definitions;
29   }
30 }
31
32 /**
33  * Implements hook_entity_field_access().
34  *
35  * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
36  */
37 function views_entity_test_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
38   if ($field_definition->getName() == 'test_text_access') {
39     if ($items) {
40       if ($items->value == 'no access value') {
41         return AccessResult::forbidden()->addCacheableDependency($items->getEntity());
42       }
43     }
44   }
45   // No opinion.
46   return AccessResult::neutral();
47 }
48
49 /**
50  * Implements hook_entity_load().
51  *
52  * @see \Drupal\Tests\views\Kernel\Handler\FieldFieldTest::testSimpleExecute()
53  */
54 function views_entity_test_entity_load(array $entities, $entity_type_id) {
55   if ($entity_type_id === 'entity_test') {
56     // Cast the value of an entity field to be something else than a string so
57     // we can check that
58     // \Drupal\views\Tests\ViewResultAssertionTrait::assertIdenticalResultsetHelper()
59     // takes care of converting all field values to strings.
60     foreach ($entities as $entity) {
61       $entity->user_id->target_id = (int) $entity->user_id->target_id;
62     }
63   }
64 }