5 * Contains main module functionality.
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;
16 * Implements hook_entity_bundle_field_info().
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',
33 * Implements hook_entity_field_access().
35 * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
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') {
40 if ($items->value == 'no access value') {
41 return AccessResult::forbidden()->addCacheableDependency($items->getEntity());
46 return AccessResult::neutral();
50 * Implements hook_entity_load().
52 * @see \Drupal\Tests\views\Kernel\Handler\FieldFieldTest::testSimpleExecute()
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
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;