Mercurial > hg > isophonics-drupal-site
comparison core/modules/text/src/TextProcessed.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\text; | 3 namespace Drupal\text; |
4 | 4 |
5 use Drupal\Core\Cache\CacheableDependencyInterface; | |
5 use Drupal\Core\TypedData\DataDefinitionInterface; | 6 use Drupal\Core\TypedData\DataDefinitionInterface; |
6 use Drupal\Core\TypedData\TypedDataInterface; | 7 use Drupal\Core\TypedData\TypedDataInterface; |
7 use Drupal\Core\TypedData\TypedData; | 8 use Drupal\Core\TypedData\TypedData; |
9 use Drupal\filter\FilterProcessResult; | |
10 use Drupal\filter\Render\FilteredMarkup; | |
8 | 11 |
9 /** | 12 /** |
10 * A computed property for processing text with a format. | 13 * A computed property for processing text with a format. |
11 * | 14 * |
12 * Required settings (below the definition's 'settings' key) are: | 15 * Required settings (below the definition's 'settings' key) are: |
13 * - text source: The text property containing the to be processed text. | 16 * - text source: The text property containing the to be processed text. |
14 */ | 17 */ |
15 class TextProcessed extends TypedData { | 18 class TextProcessed extends TypedData implements CacheableDependencyInterface { |
16 | 19 |
17 /** | 20 /** |
18 * Cached processed text. | 21 * Cached processed text. |
19 * | 22 * |
20 * @var string|null | 23 * @var \Drupal\filter\FilterProcessResult|null |
21 */ | 24 */ |
22 protected $processed = NULL; | 25 protected $processed = NULL; |
23 | 26 |
24 /** | 27 /** |
25 * {@inheritdoc} | 28 * {@inheritdoc} |
35 /** | 38 /** |
36 * {@inheritdoc} | 39 * {@inheritdoc} |
37 */ | 40 */ |
38 public function getValue() { | 41 public function getValue() { |
39 if ($this->processed !== NULL) { | 42 if ($this->processed !== NULL) { |
40 return $this->processed; | 43 return FilteredMarkup::create($this->processed->getProcessedText()); |
41 } | 44 } |
42 | 45 |
43 $item = $this->getParent(); | 46 $item = $this->getParent(); |
44 $text = $item->{($this->definition->getSetting('text source'))}; | 47 $text = $item->{($this->definition->getSetting('text source'))}; |
45 | 48 |
46 // Avoid running check_markup() on empty strings. | 49 // Avoid doing unnecessary work on empty strings. |
47 if (!isset($text) || $text === '') { | 50 if (!isset($text) || $text === '') { |
48 $this->processed = ''; | 51 $this->processed = new FilterProcessResult(''); |
49 } | 52 } |
50 else { | 53 else { |
51 $this->processed = check_markup($text, $item->format, $item->getLangcode()); | 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); | |
52 } | 64 } |
53 return $this->processed; | 65 return FilteredMarkup::create($this->processed->getProcessedText()); |
54 } | 66 } |
55 | 67 |
56 /** | 68 /** |
57 * {@inheritdoc} | 69 * {@inheritdoc} |
58 */ | 70 */ |
62 if ($notify && isset($this->parent)) { | 74 if ($notify && isset($this->parent)) { |
63 $this->parent->onChange($this->name); | 75 $this->parent->onChange($this->name); |
64 } | 76 } |
65 } | 77 } |
66 | 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 | |
67 } | 112 } |