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