TextProcessed.php in Drupal 8
File
core/modules/text/src/TextProcessed.php
View source
<?php
namespace Drupal\text;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TypedData;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Render\FilteredMarkup;
class TextProcessed extends TypedData implements CacheableDependencyInterface {
protected $processed = NULL;
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.");
}
}
public function getValue() {
if ($this->processed !== NULL) {
return FilteredMarkup::create($this->processed
->getProcessedText());
}
$item = $this
->getParent();
$text = $item->{$this->definition
->getSetting('text source')};
if (!isset($text) || $text === '') {
$this->processed = new FilterProcessResult('');
}
else {
$build = [
'#type' => 'processed_text',
'#text' => $text,
'#format' => $item->format,
'#filter_types_to_skip' => [],
'#langcode' => $item
->getLangcode(),
];
$processed_text = $this
->getRenderer()
->renderPlain($build);
$this->processed = FilterProcessResult::createFromRenderArray($build)
->setProcessedText((string) $processed_text);
}
return FilteredMarkup::create($this->processed
->getProcessedText());
}
public function setValue($value, $notify = TRUE) {
$this->processed = $value;
if ($notify && isset($this->parent)) {
$this->parent
->onChange($this->name);
}
}
public function getCacheTags() {
$this
->getValue();
return $this->processed
->getCacheTags();
}
public function getCacheContexts() {
$this
->getValue();
return $this->processed
->getCacheContexts();
}
public function getCacheMaxAge() {
$this
->getValue();
return $this->processed
->getCacheMaxAge();
}
protected function getRenderer() {
return \Drupal::service('renderer');
}
}
Classes
Name |
Description |
TextProcessed |
A computed property for processing text with a format. |