A computed property for processing text with a format.
Required settings (below the definition's 'settings' key) are:
Expanded class hierarchy of TextProcessed
class TextProcessed extends String implements ContextAwareInterface {
/**
* The text property.
*
* @var \Drupal\Core\TypedData\TypedDataInterface
*/
protected $text;
/**
* The text format property.
*
* @var \Drupal\Core\TypedData\TypedDataInterface
*/
protected $format;
/**
* The name.
*
* @var string
*/
protected $name;
/**
* The parent data structure.
*
* @var \Drupal\Core\Entity\Field\FieldItemInterface
*/
protected $parent;
/**
* Implements TypedDataInterface::__construct().
*/
public function __construct(array $definition) {
$this->definition = $definition;
if (!isset($definition['settings']['text source'])) {
throw new InvalidArgumentException("The definition's 'source' key has to specify the name of the text property to be processed.");
}
}
/**
* Implements ContextAwareInterface::getName().
*/
public function getName() {
return $this->name;
}
/**
* Implements ContextAwareInterface::setName().
*/
public function setName($name) {
$this->name = $name;
}
/**
* Implements ContextAwareInterface::getParent().
*
* @return \Drupal\Core\Entity\Field\FieldItemInterface
*/
public function getParent() {
return $this->parent;
}
/**
* Implements ContextAwareInterface::setParent().
*/
public function setParent($parent) {
$this->parent = $parent;
$this->text = $parent
->get($this->definition['settings']['text source']);
$this->format = $parent
->get('format');
}
/**
* Implements TypedDataInterface::getValue().
*/
public function getValue($langcode = NULL) {
if (!isset($this->text)) {
throw new InvalidArgumentException('Computed properties require context for computation.');
}
$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->value) {
return check_markup($this->text->value, $this->format->value, $entity
->language()->langcode);
}
else {
// If no format is available, still make sure to sanitize the text.
return check_plain($this->text->value);
}
}
/**
* Implements TypedDataInterface::setValue().
*/
public function setValue($value) {
if (isset($value)) {
throw new ReadOnlyException('Unable to set a computed property.');
}
}
}
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
String:: |
protected | property | The data value. | |
String:: |
public | function |
Implements TypedDataInterface::validate(). Overrides TypedDataInterface:: |
|
TextProcessed:: |
protected | property | The text format property. | |
TextProcessed:: |
protected | property | The name. | |
TextProcessed:: |
protected | property | The parent data structure. | |
TextProcessed:: |
protected | property | The text property. | |
TextProcessed:: |
public | function |
Implements ContextAwareInterface::getName(). Overrides ContextAwareInterface:: |
|
TextProcessed:: |
public | function |
Implements ContextAwareInterface::getParent(). Overrides ContextAwareInterface:: |
|
TextProcessed:: |
public | function |
Implements TypedDataInterface::getValue(). Overrides TypedData:: |
|
TextProcessed:: |
public | function |
Implements ContextAwareInterface::setName(). Overrides ContextAwareInterface:: |
|
TextProcessed:: |
public | function |
Implements ContextAwareInterface::setParent(). Overrides ContextAwareInterface:: |
|
TextProcessed:: |
public | function |
Implements TypedDataInterface::setValue(). Overrides String:: |
|
TextProcessed:: |
public | function |
Implements TypedDataInterface::__construct(). Overrides TypedData:: |
|
TypedData:: |
protected | property | The data definition. | |
TypedData:: |
public | function |
Implements TypedDataInterface::getDefinition(). Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Implements TypedDataInterface::getString(). Overrides TypedDataInterface:: |
8 |
TypedData:: |
public | function |
Implements TypedDataInterface::getType(). Overrides TypedDataInterface:: |