5 use Drupal\Core\TypedData\DataDefinitionInterface;
6 use Drupal\Core\TypedData\TypedDataInterface;
7 use Drupal\Core\TypedData\TypedData;
10 * A computed property for processing text with a format.
12 * Required settings (below the definition's 'settings' key) are:
13 * - text source: The text property containing the to be processed text.
15 class TextProcessed extends TypedData {
18 * Cached processed text.
22 protected $processed = NULL;
27 public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
28 parent::__construct($definition, $name, $parent);
30 if ($definition->getSetting('text source') === NULL) {
31 throw new \InvalidArgumentException("The definition's 'text source' key has to specify the name of the text property to be processed.");
38 public function getValue() {
39 if ($this->processed !== NULL) {
40 return $this->processed;
43 $item = $this->getParent();
44 $text = $item->{($this->definition->getSetting('text source'))};
46 // Avoid running check_markup() on empty strings.
47 if (!isset($text) || $text === '') {
48 $this->processed = '';
51 $this->processed = check_markup($text, $item->format, $item->getLangcode());
53 return $this->processed;
59 public function setValue($value, $notify = TRUE) {
60 $this->processed = $value;
61 // Notify the parent of any changes.
62 if ($notify && isset($this->parent)) {
63 $this->parent->onChange($this->name);