views_entity_test.module

Same filename in other branches
  1. 9 core/modules/views/tests/modules/views_entity_test/views_entity_test.module
  2. 8.9.x core/modules/views/tests/modules/views_entity_test/views_entity_test.module
  3. 10 core/modules/views/tests/modules/views_entity_test/views_entity_test.module

File

core/modules/views/tests/modules/views_entity_test/views_entity_test.module

View source
<?php


/**
 * @file
 * Contains main module functionality.
 */
declare (strict_types=1);
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_entity_bundle_field_info().
 */
function views_entity_test_entity_base_field_info(EntityTypeInterface $entity_type) {
    if ($entity_type->id() == 'entity_test') {
        $definitions['test_text_access'] = BaseFieldDefinition::create('string')->setLabel(t('Test access'))
            ->setTranslatable(FALSE)
            ->setSetting('max_length', 64)
            ->setDisplayOptions('form', [
            'type' => 'string_textfield',
            'weight' => 10,
        ]);
        return $definitions;
    }
}

/**
 * Implements hook_entity_field_access().
 *
 * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
 */
function views_entity_test_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, ?FieldItemListInterface $items = NULL) {
    if ($field_definition->getName() == 'test_text_access') {
        if ($items) {
            if ($items->value == 'no access value') {
                return AccessResult::forbidden()->addCacheableDependency($items->getEntity());
            }
        }
    }
    // No opinion.
    return AccessResult::neutral();
}

/**
 * Implements hook_entity_load().
 *
 * @see \Drupal\Tests\views\Kernel\Handler\FieldFieldTest::testSimpleExecute()
 */
function views_entity_test_entity_load(array $entities, $entity_type_id) {
    if ($entity_type_id === 'entity_test') {
        // Cast the value of an entity field to be something else than a string so
        // we can check that
        // \Drupal\views\Tests\ViewResultAssertionTrait::assertIdenticalResultsetHelper()
        // takes care of converting all field values to strings.
        foreach ($entities as $entity) {
            $entity->user_id->target_id = (int) $entity->user_id->target_id;
        }
    }
}

Functions

Title Deprecated Summary
views_entity_test_entity_base_field_info Implements hook_entity_bundle_field_info().
views_entity_test_entity_field_access Implements hook_entity_field_access().
views_entity_test_entity_load Implements hook_entity_load().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.