ea86b383f37f3c3859d47e61d74a88849cbdc70c
[yaffs-website] / web / modules / contrib / media_entity_instagram / src / Plugin / Field / FieldFormatter / InstagramEmbedFormatter.php
1 <?php
2
3 namespace Drupal\media_entity_instagram\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\FormatterBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\media_entity\EmbedCodeValueTrait;
9 use Drupal\media_entity_instagram\Plugin\MediaEntity\Type\Instagram;
10
11 /**
12  * Plugin implementation of the 'instagram_embed' formatter.
13  *
14  * @FieldFormatter(
15  *   id = "instagram_embed",
16  *   label = @Translation("Instagram embed"),
17  *   field_types = {
18  *     "link", "string", "string_long"
19  *   }
20  * )
21  */
22 class InstagramEmbedFormatter extends FormatterBase {
23
24   use EmbedCodeValueTrait;
25
26   /**
27    * {@inheritdoc}
28    */
29   public function viewElements(FieldItemListInterface $items, $langcode) {
30     $element = array();
31     $settings = $this->getSettings();
32     foreach ($items as $delta => $item) {
33       $matches = [];
34       foreach (Instagram::$validationRegexp as $pattern => $key) {
35         if (preg_match($pattern, $this->getEmbedCode($item), $matches)) {
36           break;
37         }
38       }
39
40       if (!empty($matches['shortcode'])) {
41         $element[$delta] = [
42           '#type' => 'html_tag',
43           '#tag' => 'iframe',
44           '#attributes' => [
45             'allowtransparency' => 'true',
46             'frameborder' => 0,
47             'position' => 'absolute',
48             'scrolling' => 'no',
49             'src' => '//instagram.com/p/' . $matches['shortcode'] . '/embed/',
50             'width' => $settings['width'],
51             'height' => $settings['height'],
52           ],
53         ];
54       }
55     }
56
57     return $element;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function defaultSettings() {
64     return array(
65       'width' => '480',
66       'height' => '640',
67     ) + parent::defaultSettings();
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function settingsForm(array $form, FormStateInterface $form_state) {
74     $elements = parent::settingsForm($form, $form_state);
75
76     $elements['width'] = array(
77       '#type' => 'number',
78       '#title' => $this->t('Width'),
79       '#default_value' => $this->getSetting('width'),
80       '#min' => 1,
81       '#description' => $this->t('Width of instagram.'),
82     );
83
84     $elements['height'] = array(
85       '#type' => 'number',
86       '#title' => $this->t('Height'),
87       '#default_value' => $this->getSetting('height'),
88       '#min' => 1,
89       '#description' => $this->t('Height of instagram.'),
90     );
91
92     return $elements;
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function settingsSummary() {
99     return [
100       $this->t('Width: @width px', [
101         '@width' => $this->getSetting('width'),
102       ]),
103       $this->t('Height: @height px', [
104         '@height' => $this->getSetting('height'),
105       ]),
106     ];
107   }
108
109 }