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/text/lib/Drupal/text/TextProcessed.php, line 21
Definition of Drupal\text\TextProcessed.

Namespace

Drupal\text
View source
class TextProcessed extends TypedData {

  /**
   * The text property.
   *
   * @var \Drupal\Core\TypedData\TypedDataInterface
   */
  protected $text;

  /**
   * The text format property.
   *
   * @var \Drupal\Core\TypedData\TypedDataInterface
   */
  protected $format;

  /**
   * Overrides TypedData::__construct().
   */
  public function __construct(array $definition, $name = NULL, TypedDataInterface $parent = NULL) {
    parent::__construct($definition, $name, $parent);
    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.");
    }
  }

  /**
   * Overrides TypedData::setContext().
   */
  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');
    }
  }

  /**
   * Implements \Drupal\Core\TypedData\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
      ->getValue()) {
      return check_markup($this->text
        ->getValue(), $this->format
        ->getValue(), $entity
        ->language()->langcode);
    }
    else {

      // If no format is available, still make sure to sanitize the text.
      return check_plain($this->text
        ->getValue());
    }
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::setValue().
   */
  public function setValue($value, $notify = TRUE) {
    if (isset($value)) {
      throw new ReadOnlyException('Unable to set a computed property.');
    }
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::validate().
   */
  public function validate() {

    // @todo: Implement.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TextProcessed::$format protected property The text format property.
TextProcessed::$text protected property The text property.
TextProcessed::getValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::getValue(). Overrides TypedData::getValue
TextProcessed::setContext public function Overrides TypedData::setContext(). Overrides TypedData::setContext
TextProcessed::setValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::setValue(). Overrides TypedData::setValue
TextProcessed::validate public function Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). Overrides TypedData::validate
TextProcessed::__construct public function Overrides TypedData::__construct(). Overrides TypedData::__construct
TypedData::$definition protected property The data definition.
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::getConstraints public function Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints(). Overrides TypedDataInterface::getConstraints 2
TypedData::getDefinition public function Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getName public function Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). Overrides TypedDataInterface::getName
TypedData::getParent public function Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). Overrides TypedDataInterface::getParent
TypedData::getPropertyPath public function Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). Overrides TypedDataInterface::getRoot
TypedData::getString public function Implements \Drupal\Core\TypedData\TypedDataInterface::getString(). Overrides TypedDataInterface::getString 7
TypedData::getType public function Implements \Drupal\Core\TypedData\TypedDataInterface::getType(). Overrides TypedDataInterface::getType