Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\text;
|
Chris@0
|
4
|
Chris@14
|
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@14
|
9 use Drupal\filter\FilterProcessResult;
|
Chris@14
|
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@14
|
18 class TextProcessed extends TypedData implements CacheableDependencyInterface {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Cached processed text.
|
Chris@0
|
22 *
|
Chris@14
|
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@14
|
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@14
|
49 // Avoid doing unnecessary work on empty strings.
|
Chris@0
|
50 if (!isset($text) || $text === '') {
|
Chris@14
|
51 $this->processed = new FilterProcessResult('');
|
Chris@0
|
52 }
|
Chris@0
|
53 else {
|
Chris@14
|
54 $build = [
|
Chris@14
|
55 '#type' => 'processed_text',
|
Chris@14
|
56 '#text' => $text,
|
Chris@14
|
57 '#format' => $item->format,
|
Chris@14
|
58 '#filter_types_to_skip' => [],
|
Chris@14
|
59 '#langcode' => $item->getLangcode(),
|
Chris@14
|
60 ];
|
Chris@14
|
61 // Capture the cacheability metadata associated with the processed text.
|
Chris@14
|
62 $processed_text = $this->getRenderer()->renderPlain($build);
|
Chris@14
|
63 $this->processed = FilterProcessResult::createFromRenderArray($build)->setProcessedText((string) $processed_text);
|
Chris@0
|
64 }
|
Chris@14
|
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@14
|
79 /**
|
Chris@14
|
80 * {@inheritdoc}
|
Chris@14
|
81 */
|
Chris@14
|
82 public function getCacheTags() {
|
Chris@14
|
83 $this->getValue();
|
Chris@14
|
84 return $this->processed->getCacheTags();
|
Chris@14
|
85 }
|
Chris@14
|
86
|
Chris@14
|
87 /**
|
Chris@14
|
88 * {@inheritdoc}
|
Chris@14
|
89 */
|
Chris@14
|
90 public function getCacheContexts() {
|
Chris@14
|
91 $this->getValue();
|
Chris@14
|
92 return $this->processed->getCacheContexts();
|
Chris@14
|
93 }
|
Chris@14
|
94
|
Chris@14
|
95 /**
|
Chris@14
|
96 * {@inheritdoc}
|
Chris@14
|
97 */
|
Chris@14
|
98 public function getCacheMaxAge() {
|
Chris@14
|
99 $this->getValue();
|
Chris@14
|
100 return $this->processed->getCacheMaxAge();
|
Chris@14
|
101 }
|
Chris@14
|
102
|
Chris@14
|
103 /**
|
Chris@14
|
104 * Returns the renderer service.
|
Chris@14
|
105 *
|
Chris@14
|
106 * @return \Drupal\Core\Render\RendererInterface
|
Chris@14
|
107 */
|
Chris@14
|
108 protected function getRenderer() {
|
Chris@14
|
109 return \Drupal::service('renderer');
|
Chris@14
|
110 }
|
Chris@14
|
111
|
Chris@0
|
112 }
|