1

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->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 %}
<div class="tweet-wrapper">
<div class="tweet-profile-image">
    <img src='{{ tweet.user.profile_image_url }}'/>
</div>
<div class='tweet-text'>
    <!--check_markup($tweet->text, 'full_html');-->
    {{ tweet.text }}
</div>


</div>
{% 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->text = check_markup($tweet->text, 'full_html');
  $cleanTweets[] = $tweet;
}
dpm($tweets);
$params = array('tweets' => $cleanTweets);
$tweet_template = array('#theme' => 'twitter_pull_tweet_listing', '#params' => $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.

4

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' => 'processed_text',
    '#text' => $tweet->text,
    // Relying on the full_html text format to do certain things is probably
    // not a good idea.
    '#format' => 'full_html',
    // Potentially add keys for #filter_types_to_skip and #langcode.
  );
}
$params = array('tweets' => $cleanTweets);
return array('#theme' => 'twitter_pull_tweet_listing', '#params' => $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.

  • Great answer, but I wouldn't re-assign the render array structure to $tweet->text, that overwrites the existing data, better pass it as a separate array or part of a bigger render array structure to the template. – Berdir Dec 8 '14 at 7:02
  • @Berdir - I agree 100%! Feel free to edit the code. – Cottser Dec 8 '14 at 12:07
  • @Cottser - Yes, I will be presenting this on an html page. I created a twitter pull module which creates a twitter block of twitter tweets. The tweet string contained a string representation of the link to the tweet, so I wanted to make sure that it was automatically wrapped with a <a> tag. Is this a security issue? I did it in D7. Second - I looked here api.drupal.org/api/drupal/… and I don't see 'proccessed_text' as a valid option. Is your code correct? – user1015214 Dec 8 '14 at 13:42
  • @user1015214 - The Form API documentation is manually maintained (not auto-generated) so it's out of date in several areas with regard to Drupal 8. If you look at check_markup() in D8 it's partially a render array builder which uses '#type' => 'processed_text'. – Cottser Dec 8 '14 at 17:31
  • @Cottser - I tried using the processed text element as you suggested, with the code above, and it didn't change anything. Would you have an idea why? Do I need any other code besides for what you posted above? – user1015214 Dec 23 '14 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.