Mercurial > hg > isophonics-drupal-site
diff core/modules/text/src/TextProcessed.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/text/src/TextProcessed.php Wed Nov 29 16:09:58 2017 +0000 @@ -0,0 +1,67 @@ +<?php + +namespace Drupal\text; + +use Drupal\Core\TypedData\DataDefinitionInterface; +use Drupal\Core\TypedData\TypedDataInterface; +use Drupal\Core\TypedData\TypedData; + +/** + * A computed property for processing text with a format. + * + * Required settings (below the definition's 'settings' key) are: + * - text source: The text property containing the to be processed text. + */ +class TextProcessed extends TypedData { + + /** + * Cached processed text. + * + * @var string|null + */ + protected $processed = NULL; + + /** + * {@inheritdoc} + */ + public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) { + parent::__construct($definition, $name, $parent); + + if ($definition->getSetting('text source') === NULL) { + throw new \InvalidArgumentException("The definition's 'text source' key has to specify the name of the text property to be processed."); + } + } + + /** + * {@inheritdoc} + */ + public function getValue() { + if ($this->processed !== NULL) { + return $this->processed; + } + + $item = $this->getParent(); + $text = $item->{($this->definition->getSetting('text source'))}; + + // Avoid running check_markup() on empty strings. + if (!isset($text) || $text === '') { + $this->processed = ''; + } + else { + $this->processed = check_markup($text, $item->format, $item->getLangcode()); + } + return $this->processed; + } + + /** + * {@inheritdoc} + */ + public function setValue($value, $notify = TRUE) { + $this->processed = $value; + // Notify the parent of any changes. + if ($notify && isset($this->parent)) { + $this->parent->onChange($this->name); + } + } + +}