Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. MarkupTrait.php

trait MarkupTrait

Implements MarkupInterface and Countable for rendered objects.

Hierarchy

  • trait \Drupal\Component\Render\MarkupTrait

See also

\Drupal\Component\Render\MarkupInterface

6 files declare their use of MarkupTrait
BigPipeMarkup.php in core/modules/big_pipe/src/Render/BigPipeMarkup.php
FieldFilteredMarkup.php in core/lib/Drupal/Core/Field/FieldFilteredMarkup.php
FilteredMarkup.php in core/modules/filter/src/Render/FilteredMarkup.php
IFrameMarkup.php in core/modules/media/src/IFrameMarkup.php
Markup.php in core/lib/Drupal/Core/Render/Markup.php

... See full list

File

core/lib/Drupal/Component/Render/MarkupTrait.php, line 10

Namespace

Drupal\Component\Render
View source
trait MarkupTrait {
    
    /**
     * The safe string.
     *
     * @var string
     */
    protected $string;
    
    /**
     * Creates a Markup object if necessary.
     *
     * If $string is equal to a blank string then it is not necessary to create a
     * Markup object. If $string is an object that implements MarkupInterface it
     * is returned unchanged.
     *
     * @param mixed $string
     *   The string to mark as safe. This value will be cast to a string.
     *
     * @return string|\Drupal\Component\Render\MarkupInterface
     *   A safe string.
     */
    public static function create($string) {
        if ($string instanceof MarkupInterface) {
            return $string;
        }
        $string = (string) $string;
        if ($string === '') {
            return '';
        }
        $safe_string = new static();
        $safe_string->string = $string;
        return $safe_string;
    }
    
    /**
     * Returns the string version of the Markup object.
     *
     * @return string
     *   The safe string content.
     */
    public function __toString() {
        return $this->string;
    }
    
    /**
     * Returns the string length.
     *
     * @return int
     *   The length of the string.
     */
    public function count() : int {
        return mb_strlen($this->string);
    }
    
    /**
     * Returns a representation of the object for use in JSON serialization.
     *
     * @return string
     *   The safe string content.
     */
    public function jsonSerialize() : string {
        return $this->__toString();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
MarkupTrait::$string protected property The safe string.
MarkupTrait::count public function Returns the string length.
MarkupTrait::create public static function Creates a Markup object if necessary. 1
MarkupTrait::jsonSerialize public function Returns a representation of the object for use in JSON serialization.
MarkupTrait::__toString public function Returns the string version of the Markup object.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal