Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
Alex Pott
committed
* Contains \Drupal\text\TextProcessed.
Dries Buytaert
committed
*/
namespace Drupal\text;
use Drupal\Core\TypedData\DataDefinitionInterface;
catch
committed
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TypedData;
Dries Buytaert
committed
/**
* 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.
*/
catch
committed
class TextProcessed extends TypedData {
Dries Buytaert
committed
/**
* Cached processed text.
Dries Buytaert
committed
*
* @var string|null
Dries Buytaert
committed
*/
protected $processed = NULL;
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
Dries Buytaert
committed
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.");
Dries Buytaert
committed
}
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
Alex Bronstein
committed
public function getValue() {
if ($this->processed !== NULL) {
return $this->processed;
Dries Buytaert
committed
}
$item = $this->getParent();
$text = $item->{($this->definition->getSetting('text source'))};
// Avoid running check_markup() on empty strings.
if (!isset($text) || $text === '') {
$this->processed = '';
}
Dries Buytaert
committed
else {
$this->processed = check_markup($text, $item->format, $item->getLangcode());
Dries Buytaert
committed
}
return $this->processed;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
catch
committed
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);
Dries Buytaert
committed
}
}