class TextProcessed

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.

Hierarchy

Expanded class hierarchy of TextProcessed

File

drupal/core/modules/field/modules/text/lib/Drupal/text/TextProcessed.php, line 21
Definition of Drupal\text\TextProcessed.

Namespace

Drupal\text
View source
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.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
String::$value protected property The data value.
String::validate public function Implements TypedDataInterface::validate(). Overrides TypedDataInterface::validate
TextProcessed::$format protected property The text format property.
TextProcessed::$name protected property The name.
TextProcessed::$parent protected property The parent data structure.
TextProcessed::$text protected property The text property.
TextProcessed::getName public function Implements ContextAwareInterface::getName(). Overrides ContextAwareInterface::getName
TextProcessed::getParent public function Implements ContextAwareInterface::getParent(). Overrides ContextAwareInterface::getParent
TextProcessed::getValue public function Implements TypedDataInterface::getValue(). Overrides TypedData::getValue
TextProcessed::setName public function Implements ContextAwareInterface::setName(). Overrides ContextAwareInterface::setName
TextProcessed::setParent public function Implements ContextAwareInterface::setParent(). Overrides ContextAwareInterface::setParent
TextProcessed::setValue public function Implements TypedDataInterface::setValue(). Overrides String::setValue
TextProcessed::__construct public function Implements TypedDataInterface::__construct(). Overrides TypedData::__construct
TypedData::$definition protected property The data definition.
TypedData::getDefinition public function Implements TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getString public function Implements TypedDataInterface::getString(). Overrides TypedDataInterface::getString 8
TypedData::getType public function Implements TypedDataInterface::getType(). Overrides TypedDataInterface::getType