TextProcessed.php 2.9 KB

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