annotate core/modules/text/src/TextProcessed.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\text;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\CacheableDependencyInterface;
Chris@0 6 use Drupal\Core\TypedData\DataDefinitionInterface;
Chris@0 7 use Drupal\Core\TypedData\TypedDataInterface;
Chris@0 8 use Drupal\Core\TypedData\TypedData;
Chris@0 9 use Drupal\filter\FilterProcessResult;
Chris@0 10 use Drupal\filter\Render\FilteredMarkup;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * A computed property for processing text with a format.
Chris@0 14 *
Chris@0 15 * Required settings (below the definition's 'settings' key) are:
Chris@0 16 * - text source: The text property containing the to be processed text.
Chris@0 17 */
Chris@0 18 class TextProcessed extends TypedData implements CacheableDependencyInterface {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Cached processed text.
Chris@0 22 *
Chris@0 23 * @var \Drupal\filter\FilterProcessResult|null
Chris@0 24 */
Chris@0 25 protected $processed = NULL;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * {@inheritdoc}
Chris@0 29 */
Chris@0 30 public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
Chris@0 31 parent::__construct($definition, $name, $parent);
Chris@0 32
Chris@0 33 if ($definition->getSetting('text source') === NULL) {
Chris@0 34 throw new \InvalidArgumentException("The definition's 'text source' key has to specify the name of the text property to be processed.");
Chris@0 35 }
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * {@inheritdoc}
Chris@0 40 */
Chris@0 41 public function getValue() {
Chris@0 42 if ($this->processed !== NULL) {
Chris@0 43 return FilteredMarkup::create($this->processed->getProcessedText());
Chris@0 44 }
Chris@0 45
Chris@0 46 $item = $this->getParent();
Chris@0 47 $text = $item->{($this->definition->getSetting('text source'))};
Chris@0 48
Chris@0 49 // Avoid doing unnecessary work on empty strings.
Chris@0 50 if (!isset($text) || $text === '') {
Chris@0 51 $this->processed = new FilterProcessResult('');
Chris@0 52 }
Chris@0 53 else {
Chris@0 54 $build = [
Chris@0 55 '#type' => 'processed_text',
Chris@0 56 '#text' => $text,
Chris@0 57 '#format' => $item->format,
Chris@0 58 '#filter_types_to_skip' => [],
Chris@0 59 '#langcode' => $item->getLangcode(),
Chris@0 60 ];
Chris@0 61 // Capture the cacheability metadata associated with the processed text.
Chris@0 62 $processed_text = $this->getRenderer()->renderPlain($build);
Chris@0 63 $this->processed = FilterProcessResult::createFromRenderArray($build)->setProcessedText((string) $processed_text);
Chris@0 64 }
Chris@0 65 return FilteredMarkup::create($this->processed->getProcessedText());
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * {@inheritdoc}
Chris@0 70 */
Chris@0 71 public function setValue($value, $notify = TRUE) {
Chris@0 72 $this->processed = $value;
Chris@0 73 // Notify the parent of any changes.
Chris@0 74 if ($notify && isset($this->parent)) {
Chris@0 75 $this->parent->onChange($this->name);
Chris@0 76 }
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * {@inheritdoc}
Chris@0 81 */
Chris@0 82 public function getCacheTags() {
Chris@0 83 $this->getValue();
Chris@0 84 return $this->processed->getCacheTags();
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * {@inheritdoc}
Chris@0 89 */
Chris@0 90 public function getCacheContexts() {
Chris@0 91 $this->getValue();
Chris@0 92 return $this->processed->getCacheContexts();
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * {@inheritdoc}
Chris@0 97 */
Chris@0 98 public function getCacheMaxAge() {
Chris@0 99 $this->getValue();
Chris@0 100 return $this->processed->getCacheMaxAge();
Chris@0 101 }
Chris@0 102
Chris@0 103 /**
Chris@0 104 * Returns the renderer service.
Chris@0 105 *
Chris@0 106 * @return \Drupal\Core\Render\RendererInterface
Chris@0 107 */
Chris@0 108 protected function getRenderer() {
Chris@0 109 return \Drupal::service('renderer');
Chris@0 110 }
Chris@0 111
Chris@0 112 }