3 namespace Drupal\Component\Utility;
5 use Drupal\Component\Render\HtmlEscapedText;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Component\Render\MarkupInterface;
10 * Contains deprecated functionality related to sanitization of markup.
12 * @deprecated Will be removed before Drupal 9.0.0. Use the appropriate
13 * @link sanitization sanitization functions @endlink or the @link theme_render theme and render systems @endlink
14 * so that the output can can be themed, escaped, and altered properly.
16 * @see https://www.drupal.org/node/2549395
18 * @see TwigExtension::escapeFilter()
19 * @see twig_render_template()
26 * Checks if a string is safe to output.
28 * @param string|\Drupal\Component\Render\MarkupInterface $string
29 * The content to be checked.
30 * @param string $strategy
31 * (optional) This value is ignored.
34 * TRUE if the string has been marked secure, FALSE otherwise.
36 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
37 * Instead, you should just check if a variable is an instance of
38 * \Drupal\Component\Render\MarkupInterface.
40 * @see https://www.drupal.org/node/2549395
42 public static function isSafe($string, $strategy = 'html') {
43 @trigger_error('SafeMarkup::isSafe() is scheduled for removal in Drupal 9.0.0. Instead, you should just check if a variable is an instance of \Drupal\Component\Render\MarkupInterface. See https://www.drupal.org/node/2549395.', E_USER_DEPRECATED);
44 return $string instanceof MarkupInterface;
48 * Encodes special characters in a plain-text string for display as HTML.
50 * Also validates strings as UTF-8. All processed strings are also
51 * automatically flagged as safe markup strings for rendering.
54 * The text to be checked or processed.
56 * @return \Drupal\Component\Render\HtmlEscapedText
57 * An HtmlEscapedText object that escapes when rendered to string.
59 * @deprecated Will be removed before Drupal 9.0.0. Rely on Twig's
60 * auto-escaping feature, or use the @link theme_render #plain_text @endlink
61 * key when constructing a render array that contains plain text in order to
62 * use the renderer's auto-escaping feature. If neither of these are
63 * possible, \Drupal\Component\Utility\Html::escape() can be used in places
64 * where explicit escaping is needed.
66 * @see https://www.drupal.org/node/2549395
67 * @see drupal_validate_utf8()
69 public static function checkPlain($text) {
70 @trigger_error('SafeMarkup::checkPlain() is scheduled for removal in Drupal 9.0.0. Rely on Twig\'s auto-escaping feature, or use the @link theme_render #plain_text @endlink key when constructing a render array that contains plain text in order to use the renderer\'s auto-escaping feature. If neither of these are possible, \Drupal\Component\Utility\Html::escape() can be used in places where explicit escaping is needed. See https://www.drupal.org/node/2549395.', E_USER_DEPRECATED);
71 return new HtmlEscapedText($text);
75 * Formats a string for HTML display by replacing variable placeholders.
77 * @param string $string
78 * A string containing placeholders. The string itself will not be escaped,
79 * any unsafe content must be in $args and inserted via placeholders.
81 * An array with placeholder replacements, keyed by placeholder. See
82 * \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
83 * additional information about placeholders.
85 * @return string|\Drupal\Component\Render\MarkupInterface
86 * The formatted string, which is an instance of MarkupInterface unless
87 * sanitization of an unsafe argument was suppressed (see above).
89 * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
90 * @see \Drupal\Component\Render\FormattableMarkup
92 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
93 * Use \Drupal\Component\Render\FormattableMarkup.
95 * @see https://www.drupal.org/node/2549395
97 public static function format($string, array $args) {
98 @trigger_error('SafeMarkup::format() is scheduled for removal in Drupal 9.0.0. Use \Drupal\Component\Render\FormattableMarkup. See https://www.drupal.org/node/2549395.', E_USER_DEPRECATED);
99 return new FormattableMarkup($string, $args);