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