How do I apply check_markup() as Twig filter? I am trying to modify output in my twig file. In D7, I was able modify data in my array with this

check_markup($tweet-&gt;text, 'full_html');


and I see that this function still exists in D8. However, I'm not sure how I would use this in a filter in twig. This is what I have so far:

{% for tweet in params.tweets %}
&lt;div class="tweet-wrapper"&gt;
&lt;div class="tweet-profile-image"&gt;
    &lt;img src='{{ tweet.user.profile_image_url }}'/&gt;
&lt;/div&gt;
&lt;div class='tweet-text'&gt;
    &lt;!--check_markup($tweet-&gt;text, 'full_html');--&gt;
    {{ tweet.text }}
&lt;/div&gt;


&lt;/div&gt;
{% endfor %}


I actually tried to do this before I sent it to the template as well, but it didn't change anything:

 foreach($tweets as $tweet) {
  $tweet-&gt;text = check_markup($tweet-&gt;text, 'full_html');
  $cleanTweets[] = $tweet;
}
dpm($tweets);
$params = array('tweets' =&gt; $cleanTweets);
$tweet_template = array('#theme' =&gt; 'twitter_pull_tweet_listing', '#params' =&gt; $params);
return $tweet_template;


What I'm trying to do here is make drupal create links for web links in the twitter tweet text. This worked fine for me in D7.
First of all, what is the motivation here? Are you getting double escaping or something? Providing more background information may help to get you a better answer.

Before we get too much further, relying on a certain configuration of the full_html text format seems like it's not a good solution. For example, to get the below to work for automatically creating links for this scenario, I had to enable Convert URLs into links in the full_html text format.

If you take a look at the documentation for check_markup() you'll see this note:


  Note: this function should only be used when filtering text for use elsewhere than on a rendered HTML page. If this is part of a HTML page, then a renderable array with a #type 'processed_text' element should be used instead of this, because that will allow cache tags to be set and bubbled up, assets to be loaded and #post_render_cache callbacks to be associated. In other words: if you are presenting the filtered text in a HTML page, the only way this will be presented correctly, is by using the 'processed_text' element.


So based on that I think what you may want to do is in your block build method create a 'processed_text' element (render array):

foreach ($tweets as $tweet) {
  $cleanTweets[] = array(
    '#type' =&gt; 'processed_text',
    '#text' =&gt; $tweet-&gt;text,
    // Relying on the full_html text format to do certain things is probably
    // not a good idea.
    '#format' =&gt; 'full_html',
    // Potentially add keys for #filter_types_to_skip and #langcode.
  );
}
$params = array('tweets' =&gt; $cleanTweets);
return array('#theme' =&gt; 'twitter_pull_tweet_listing', '#params' =&gt; $params);


Then you can do this in your Twig template (twitter-pull-tweet-listing.html.twig):

{% for tweet in params.tweets %}
  …
  {{ tweet }}
  …
{% endfor %}


To relate this to Drupal 7, that Twig template code would be roughly equivalent to running render() on each array element (individual render array) in the $cleanTweets array.

If you're more interested in creating an actual Twig filter, the Twig documentation has some information on that, and Symfony has some very comprehensive documentation that matches up with how you would create a Twig extension in Drupal 8.