function MarkupTrait::create

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Render/MarkupTrait.php \Drupal\Component\Render\MarkupTrait::create()
  2. 10 core/lib/Drupal/Component/Render/MarkupTrait.php \Drupal\Component\Render\MarkupTrait::create()
  3. 11.x core/lib/Drupal/Component/Render/MarkupTrait.php \Drupal\Component\Render\MarkupTrait::create()

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.


mixed $string: The string to mark as safe. This value will be cast to a string.

Return value

string|\Drupal\Component\Render\MarkupInterface A safe string.

2 methods override MarkupTrait::create()
FieldFilteredMarkup::create in core/lib/Drupal/Core/Field/FieldFilteredMarkup.php
Overrides \Drupal\Component\Render\MarkupTrait::create().
SafeMarkupTestMarkup::create in core/tests/Drupal/KernelTests/Core/Theme/TwigMarkupInterfaceTest.php
Overrides MarkupTrait::create() to allow creation with empty strings.


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


Implements MarkupInterface and Countable for rendered objects.




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;

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.