diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php index 1e55f29..d2db1a6 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php @@ -54,8 +54,8 @@ function testNodeTokenReplacement() { $tests['[node:type]'] = 'article'; $tests['[node:type-name]'] = 'Article'; $tests['[node:title]'] = check_plain($node->getTitle()); - $tests['[node:body]'] = text_sanitize($instance['settings']['text_processing'], $node->language()->id, $node->body[0]->getValue(), 'value'); - $tests['[node:summary]'] = text_sanitize($instance['settings']['text_processing'], $node->language()->id, $node->body[0]->getValue(), 'summary'); + $tests['[node:body]'] = $node->body->processed; + $tests['[node:summary]'] = $node->body->summary_processed; $tests['[node:langcode]'] = check_plain($node->language()->id); $tests['[node:url]'] = url('node/' . $node->id(), $url_options); $tests['[node:edit-url]'] = url('node/' . $node->id() . '/edit', $url_options); @@ -97,7 +97,7 @@ function testNodeTokenReplacement() { // Generate and test sanitized token - use full body as expected value. $tests = array(); - $tests['[node:summary]'] = text_sanitize($instance['settings']['text_processing'], $node->language()->id, $node->body[0]->getValue(), 'value'); + $tests['[node:summary]'] = $node->body->summary_processed; // Test to make sure that we generated something for each token. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated for node without a summary.'); diff --git a/core/modules/node/node.tokens.inc b/core/modules/node/node.tokens.inc index 9240497..397211f 100644 --- a/core/modules/node/node.tokens.inc +++ b/core/modules/node/node.tokens.inc @@ -144,11 +144,11 @@ function node_tokens($type, $tokens, array $data = array(), array $options = arr // If the summary was requested and is not empty, use it. if ($name == 'summary' && !empty($item->summary)) { - $output = $sanitize ? text_sanitize($instance['settings']['text_processing'], $field_langcode, $item->getValue(), 'summary') : $item->summary; + $output = $sanitize ? $item->summary_processed : $item->summary; } // Attempt to provide a suitable version of the 'body' field. else { - $output = $sanitize ? text_sanitize($instance['settings']['text_processing'], $field_langcode, $item->getValue(), 'value') : $item->value; + $output = $sanitize ? $item->processed : $item->value; // A summary was requested. if ($name == 'summary') { // Generate an optionally trimmed summary of the body field. diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/TextItemBase.php b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/TextItemBase.php index 417d354..ee3b13d 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/TextItemBase.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/TextItemBase.php @@ -83,10 +83,10 @@ public function prepareCache() { // source' of a TextProcessed computed property. // @todo Clean up this mess by making the TextProcessed property type // support its own cache integration: https://drupal.org/node/2026339. - foreach ($this->getPropertyDefinitions() as $definition) { + foreach ($this->getPropertyDefinitions() as $property => $definition) { if (isset($definition['class']) && ($definition['class'] == '\Drupal\text\TextProcessed')) { - $source_property = $definition['settings']['text source']; - $this->set('safe_' . $source_property, text_sanitize($text_processing, $langcode, $itemBC, $source_property)); + // Calculate the processed values, so that it can be cached later on. + $this->get($property)->getValue(); } } } diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php index dd0ee71..a34da43 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php @@ -38,11 +38,7 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface $elements = array(); foreach ($items as $delta => $item) { - // @todo Convert text_sanitize() to work on an NG $item. See - // https://drupal.org/node/2026339. - $itemBC = $item->getValue(TRUE); - $output = text_sanitize($this->getFieldSetting('text_processing'), $langcode, $itemBC, 'value'); - $elements[$delta] = array('#markup' => $output); + $elements[$delta] = array('#markup' => $item->processed); } return $elements; diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php index f2ca9e4..b59c73b 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php @@ -71,10 +71,10 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface $text_processing = $this->getFieldSetting('text_processing'); foreach ($items as $delta => $item) { if ($this->getPluginId() == 'text_summary_or_trimmed' && !empty($item->summary)) { - $output = text_sanitize($text_processing, $langcode, $item->getValue(TRUE), 'summary'); + $output = $item->summary_processed; } else { - $output = text_sanitize($text_processing, $langcode, $item->getValue(TRUE), 'value'); + $output = $item->processed; $output = text_summary($output, $text_processing ? $item->format : NULL, $this->getSetting('trim_length')); } $elements[$delta] = array('#markup' => $output); diff --git a/core/modules/text/lib/Drupal/text/TextProcessed.php b/core/modules/text/lib/Drupal/text/TextProcessed.php index 9a1910f..b980037 100644 --- a/core/modules/text/lib/Drupal/text/TextProcessed.php +++ b/core/modules/text/lib/Drupal/text/TextProcessed.php @@ -21,18 +21,11 @@ class TextProcessed extends TypedData { /** - * The text property. + * Cached processed text. * - * @var \Drupal\Core\TypedData\TypedDataInterface + * @var string|false */ - protected $text; - - /** - * The text format property. - * - * @var \Drupal\Core\TypedData\TypedDataInterface - */ - protected $format; + protected $processed = FALSE; /** * Overrides TypedData::__construct(). @@ -51,8 +44,8 @@ public function __construct(array $definition, $name = NULL, TypedDataInterface public function setContext($name = NULL, TypedDataInterface $parent = NULL) { parent::setContext($name, $parent); if (isset($parent)) { - $this->text = $parent->get($this->definition['settings']['text source']); - $this->format = $parent->get('format'); + $this->text = $parent->{($this->definition['settings']['text source'])}; + $this->format = $parent->format; } } @@ -60,22 +53,28 @@ public function setContext($name = NULL, TypedDataInterface $parent = NULL) { * Implements \Drupal\Core\TypedData\TypedDataInterface::getValue(). */ public function getValue($langcode = NULL) { + if ($this->processed !== FALSE) { + return $this->processed; + } - if (!isset($this->text)) { + if (empty($this->definition['settings']['text source'])) { throw new InvalidArgumentException('Computed properties require context for computation.'); } + $text = $this->parent->{($this->definition['settings']['text source'])}; $field = $this->parent->getParent(); $entity = $field->getParent(); $instance = field_info_instance($entity->entityType(), $field->getName(), $entity->bundle()); - if (!empty($instance['settings']['text_processing']) && $this->format->getValue()) { - return check_markup($this->text->getValue(), $this->format->getValue(), $entity->language()->id); + if (!empty($instance['settings']['text_processing']) && $this->parent->format) { + $this->processed = check_markup($text, $this->parent->format, $entity->language()->id); } else { - // If no format is available, still make sure to sanitize the text. - return check_plain($this->text->getValue()); + // Escape all HTML and retain newlines. + // @see \Drupal\text\Plugin\field\formatter\TextPlainFormatter + $this->processed = nl2br(check_plain($text)); } + return $this->processed; } /** @@ -83,9 +82,7 @@ public function getValue($langcode = NULL) { */ public function setValue($value, $notify = TRUE) { if (isset($value)) { - // @todo This is triggered from DatabaseStorageController::invokeFieldMethod() - // in the case of case of non-NG entity types. - // throw new ReadOnlyException('Unable to set a computed property.'); + $this->processed = $value; } } } diff --git a/core/modules/text/text.module b/core/modules/text/text.module index 0bb7427..12288cd 100644 --- a/core/modules/text/text.module +++ b/core/modules/text/text.module @@ -41,42 +41,6 @@ function text_help($path, $arg) { } /** - * Sanitizes the 'value' or 'summary' data of a text value. - * - * Depending on whether the field instance uses text processing, data is run - * through check_plain() or check_markup(). - * - * @param bool $text_processing - * Whether to process the text via check_markup(). - * @param string $langcode - * The language associated with $item. - * @param array $item - * The field value to sanitize. - * @param string $column - * The column to sanitize (either 'value' or 'summary'). - * - * @return string - * The sanitized string. - */ -function text_sanitize($text_processing, $langcode, $item, $column) { - if (isset($item["safe_$column"])) { - return $item["safe_$column"]; - } - - // Optimize by opting out for the trivial 'empty string' case. - if ($item[$column] == '') { - return ''; - } - - if ($text_processing) { - return check_markup($item[$column], $item['format'], $langcode); - } - // Escape all HTML and retain newlines. - // @see \Drupal\text\Plugin\field\formatter\TextPlainFormatter - return nl2br(check_plain($item[$column])); -} - -/** * Generates a trimmed, formatted version of a text field value. * * If the end of the summary is not indicated using the delimiter