ComputedString.php

Same filename in other branches
  1. 9 core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php
  2. 8.9.x core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php
  3. 11.x core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php

Namespace

Drupal\entity_test\TypedData

File

core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php

View source
<?php

namespace Drupal\entity_test\TypedData;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\TypedData\TypedData;

/**
 * A computed property for test strings.
 */
class ComputedString extends TypedData implements CacheableDependencyInterface {
    
    /**
     * The data value.
     *
     * @var mixed
     */
    protected $value;
    
    /**
     * {@inheritdoc}
     */
    public function getValue() {
        
        /** @var \Drupal\Core\Field\FieldItemInterface $item */
        $item = $this->getParent();
        $computed_value = "Computed! " . $item->get('value')
            ->getString();
        return $computed_value;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCastedValue() {
        return $this->getString();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheTags() {
        return [
            'you_are_it',
            'no_tag_backs',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheContexts() {
        return [
            'request_format',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheMaxAge() {
        return Cache::PERMANENT;
    }

}

Classes

Title Deprecated Summary
ComputedString A computed property for test strings.

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