3 namespace Drupal\media_entity_twitter\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FieldItemInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FormatterBase;
8 use Drupal\media_entity_twitter\Plugin\media\Source\Twitter;
11 * Plugin implementation of the 'twitter_embed' formatter.
14 * id = "twitter_embed",
15 * label = @Translation("Twitter embed"),
17 * "link", "string", "string_long"
21 class TwitterEmbedFormatter extends FormatterBase {
24 * Extracts the embed code from a field item.
26 * @param \Drupal\Core\Field\FieldItemInterface $item
30 * The embed code, or NULL if the field type is not supported.
32 protected function getEmbedCode(FieldItemInterface $item) {
33 switch ($item->getFieldDefinition()->getType()) {
49 public function viewElements(FieldItemListInterface $items, $langcode) {
51 foreach ($items as $delta => $item) {
54 foreach (Twitter::$validationRegexp as $pattern => $key) {
55 if (preg_match($pattern, $this->getEmbedCode($item), $item_matches)) {
56 $matches[] = $item_matches;
60 if (!empty($matches)) {
61 $matches = reset($matches);
64 if (!empty($matches['user']) && !empty($matches['id'])) {
66 '#theme' => 'media_entity_twitter_tweet',
67 '#path' => 'https://twitter.com/' . $matches['user'] . '/statuses/' . $matches['id'],
69 'class' => ['twitter-tweet', 'element-hidden'],
70 'data-conversation' => 'none',
77 if (!empty($element)) {
78 $element['#attached'] = [
80 'media_entity_twitter/integration',