EntityTestComputedField.php

Same filename in other branches
  1. 9 core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php
  2. 8.9.x core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php
  3. 10 core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php

Namespace

Drupal\entity_test\Entity

File

core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php

View source
<?php

declare (strict_types=1);
namespace Drupal\entity_test\Entity;

use Drupal\Core\Entity\Attribute\ContentEntityType;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_test\EntityTestViewsData;
use Drupal\entity_test\Plugin\Field\ComputedReferenceTestFieldItemList;
use Drupal\entity_test\Plugin\Field\ComputedTestCacheableIntegerItemList;
use Drupal\entity_test\Plugin\Field\ComputedTestCacheableStringItemList;
use Drupal\entity_test\Plugin\Field\ComputedTestFieldItemList;

/**
 * An entity used for testing computed field values.
 */
class EntityTestComputedField extends EntityTest {
    
    /**
     * {@inheritdoc}
     */
    public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
        $fields = parent::baseFieldDefinitions($entity_type);
        $fields['computed_string_field'] = BaseFieldDefinition::create('string')->setLabel('Computed Field Test')
            ->setComputed(TRUE)
            ->setClass(ComputedTestFieldItemList::class);
        $fields['computed_reference_field'] = BaseFieldDefinition::create('entity_reference')->setLabel('Computed Reference Field Test')
            ->setComputed(TRUE)
            ->setSetting('target_type', 'entity_test')
            ->setClass(ComputedReferenceTestFieldItemList::class);
        // Cacheable metadata can either be provided via the field item properties
        // or via the field item list class directly. Add a computed string field
        // which does the former and a computed integer field which does the latter.
        $fields['computed_test_cacheable_string_field'] = BaseFieldDefinition::create('computed_test_cacheable_string_item')->setLabel(new TranslatableMarkup('Computed Cacheable String Field Test'))
            ->setComputed(TRUE)
            ->setClass(ComputedTestCacheableStringItemList::class)
            ->setReadOnly(FALSE)
            ->setInternal(FALSE);
        $fields['computed_test_cacheable_integer_field'] = BaseFieldDefinition::create('integer')->setLabel(new TranslatableMarkup('Computed Cacheable Integer Field Test'))
            ->setComputed(TRUE)
            ->setClass(ComputedTestCacheableIntegerItemList::class)
            ->setReadOnly(FALSE)
            ->setInternal(FALSE)
            ->setDisplayOptions('view', [
            'weight' => 10,
        ]);
        return $fields;
    }

}

Classes

Title Deprecated Summary
EntityTestComputedField An entity used for testing computed field values.

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