comparison core/modules/text/src/TextProcessed.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\text;
4
5 use Drupal\Core\TypedData\DataDefinitionInterface;
6 use Drupal\Core\TypedData\TypedDataInterface;
7 use Drupal\Core\TypedData\TypedData;
8
9 /**
10 * A computed property for processing text with a format.
11 *
12 * Required settings (below the definition's 'settings' key) are:
13 * - text source: The text property containing the to be processed text.
14 */
15 class TextProcessed extends TypedData {
16
17 /**
18 * Cached processed text.
19 *
20 * @var string|null
21 */
22 protected $processed = NULL;
23
24 /**
25 * {@inheritdoc}
26 */
27 public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
28 parent::__construct($definition, $name, $parent);
29
30 if ($definition->getSetting('text source') === NULL) {
31 throw new \InvalidArgumentException("The definition's 'text source' key has to specify the name of the text property to be processed.");
32 }
33 }
34
35 /**
36 * {@inheritdoc}
37 */
38 public function getValue() {
39 if ($this->processed !== NULL) {
40 return $this->processed;
41 }
42
43 $item = $this->getParent();
44 $text = $item->{($this->definition->getSetting('text source'))};
45
46 // Avoid running check_markup() on empty strings.
47 if (!isset($text) || $text === '') {
48 $this->processed = '';
49 }
50 else {
51 $this->processed = check_markup($text, $item->format, $item->getLangcode());
52 }
53 return $this->processed;
54 }
55
56 /**
57 * {@inheritdoc}
58 */
59 public function setValue($value, $notify = TRUE) {
60 $this->processed = $value;
61 // Notify the parent of any changes.
62 if ($notify && isset($this->parent)) {
63 $this->parent->onChange($this->name);
64 }
65 }
66
67 }