manniru
1/2/2015 - 3:05 AM

Example showing usage of the processed_text element in Drupal 8

Example showing usage of the processed_text element in Drupal 8

<?php

function twitter_pull_theme() {
  return array(
    'twitter_pull_tweet_listing' => array(
      'variables' => array('params' => array()),
    ),
  );
}
name: Twitter Pull
description: http://drupal.stackexchange.com/questions/139086/how-do-i-apply-check-markup-as-twig-filter/
core: 8.x
type: module
{% for tweet in params.tweets %}
  <div class="tweet-wrapper">
    <div class="tweet-profile-image">
      {# Todo #}
    </div>
    <div class='tweet-text'>
      {{ tweet }}
    </div>
  </div>
{% endfor %}
<?php

namespace Drupal\twitter_pull\Plugin\Block;
use Drupal\Core\Block\BlockBase;

/**
 * Provides a block for executing PHP code.
 *
 * @Block(
 *   id = "twitter_pull_tweets_block",
 *   admin_label = @Translation("Twitter Tweets")
 * )
 */
class TweetsBlock extends BlockBase {

  /**
   * Builds and returns the renderable array for this block plugin.
   *
   * @return array
   *   A renderable array representing the content of the block.
   *
   * @see \Drupal\block\BlockViewBuilder
   */
  public function build() {
    $tweets = array(
      (object) array('text' => 'Hey!'),
      (object) array('text' => 'https://www.drupal.org'),
    );
    foreach ($tweets as $tweet) {
      $cleanTweets[] = array(
        '#type' => 'processed_text',
        '#text' => $tweet->text,
        '#format' => 'full_html',
        // Potentially add keys for #filter_types_to_skip and #langcode.
      );
    }
    $params = array('tweets' => $cleanTweets);
    $tweet_template = array('#theme' => 'twitter_pull_tweet_listing', '#params' => $params);
    return $tweet_template;
  }

}