Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
74
core/modules/text/src/TextProcessed.php
Normal file
74
core/modules/text/src/TextProcessed.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\text\TextProcessed.
|
||||
*/
|
||||
|
||||
namespace Drupal\text;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\TypedData\DataDefinitionInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
use Drupal\Core\TypedData\TypedData;
|
||||
|
||||
/**
|
||||
* A computed property for processing text with a format.
|
||||
*
|
||||
* Required settings (below the definition's 'settings' key) are:
|
||||
* - text source: The text property containing the to be processed text.
|
||||
*/
|
||||
class TextProcessed extends TypedData {
|
||||
|
||||
/**
|
||||
* Cached processed text.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $processed = NULL;
|
||||
|
||||
/**
|
||||
* Overrides TypedData::__construct().
|
||||
*/
|
||||
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
|
||||
parent::__construct($definition, $name, $parent);
|
||||
|
||||
if ($definition->getSetting('text source') === NULL) {
|
||||
throw new \InvalidArgumentException("The definition's 'text source' key has to specify the name of the text property to be processed.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Core\TypedData\TypedDataInterface::getValue().
|
||||
*/
|
||||
public function getValue($langcode = NULL) {
|
||||
if ($this->processed !== NULL) {
|
||||
return $this->processed;
|
||||
}
|
||||
|
||||
$item = $this->getParent();
|
||||
$text = $item->{($this->definition->getSetting('text source'))};
|
||||
|
||||
// Avoid running check_markup() or
|
||||
// \Drupal\Component\Utility\SafeMarkup::checkPlain() on empty strings.
|
||||
if (!isset($text) || $text === '') {
|
||||
$this->processed = '';
|
||||
}
|
||||
else {
|
||||
$this->processed = check_markup($text, $item->format, $item->getLangcode());
|
||||
}
|
||||
return $this->processed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Core\TypedData\TypedDataInterface::setValue().
|
||||
*/
|
||||
public function setValue($value, $notify = TRUE) {
|
||||
$this->processed = $value;
|
||||
// Notify the parent of any changes.
|
||||
if ($notify && isset($this->parent)) {
|
||||
$this->parent->onChange($this->name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in a new issue