860be6fa7dfc7bae63584bed96b8bd382186b42b
[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\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FormatterBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\media_entity\EmbedCodeValueTrait;
11 use Drupal\media_entity_instagram\Plugin\MediaEntity\Type\Instagram;
12 use Drupal\media_entity_instagram\InstagramEmbedFetcher;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Plugin implementation of the 'instagram_embed' formatter.
17  *
18  * @FieldFormatter(
19  *   id = "instagram_embed",
20  *   label = @Translation("Instagram embed"),
21  *   field_types = {
22  *     "link", "string", "string_long"
23  *   }
24  * )
25  */
26 class InstagramEmbedFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
27
28   use EmbedCodeValueTrait;
29
30   /**
31    * The instagram fetcher.
32    *
33    * @var \Drupal\media_entity_instagram\Plugin\MediaEntity\Type\InstagramEmbedFetcher
34    */
35   protected $fetcher;
36
37   /**
38    * Constructs a InstagramEmbedFormatter instance.
39    */
40   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, InstagramEmbedFetcher $fetcher) {
41     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
42     $this->fetcher = $fetcher;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49     return new static(
50       $plugin_id,
51       $plugin_definition,
52       $configuration['field_definition'],
53       $configuration['settings'],
54       $configuration['label'],
55       $configuration['view_mode'],
56       $configuration['third_party_settings'],
57       $container->get('media_entity_instagram.instagram_embed_fetcher')
58     );
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function viewElements(FieldItemListInterface $items, $langcode) {
65     $element = [];
66     $settings = $this->getSettings();
67     foreach ($items as $delta => $item) {
68       $matches = [];
69
70       foreach (Instagram::$validationRegexp as $pattern => $key) {
71         if (preg_match($pattern, $this->getEmbedCode($item), $item_matches)) {
72           $matches[] = $item_matches;
73         }
74       }
75
76       if (!empty($matches)) {
77         $matches = reset($matches);
78       }
79
80       if (!empty($matches['shortcode'])) {
81
82         if ($instagram = $this->fetcher->fetchInstagramEmbed($matches['shortcode'], $settings['hidecaption'], $settings['width'])) {
83           $element[$delta] = [
84             '#theme' => 'media_entity_instagram_post',
85             '#post' => (string) $instagram['html'],
86             '#shortcode' => $matches['shortcode'],
87           ];
88         }
89       }
90     }
91
92     if (!empty($element)) {
93       $element['#attached'] = [
94         'library' => [
95           'media_entity_instagram/integration',
96         ],
97       ];
98     }
99
100     return $element;
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public static function defaultSettings() {
107     return [
108       'width' => NULL,
109       'hidecaption' => FALSE,
110     ] + parent::defaultSettings();
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function settingsForm(array $form, FormStateInterface $form_state) {
117     $elements = parent::settingsForm($form, $form_state);
118
119     $elements['width'] = [
120       '#type' => 'number',
121       '#title' => $this->t('Width'),
122       '#default_value' => $this->getSetting('width'),
123       '#min' => 320,
124       '#description' => $this->t('Max width of instagram.'),
125     ];
126
127     $elements['hidecaption'] = [
128       '#type' => 'checkbox',
129       '#title' => $this->t('Caption hidden'),
130       '#default_value' => $this->getSetting('hidecaption'),
131       '#description' => $this->t('Enable to hide caption of Instagram posts.'),
132     ];
133
134     return $elements;
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function settingsSummary() {
141     $settings = $this->getSettings();
142
143     $summary = [];
144
145     if ($this->getSetting('width')) {
146       $summary[] = $this->t('Width: @width px', [
147         '@width' => $this->getSetting('width'),
148       ]);
149     }
150
151     $summary[] = $this->t('Caption: @hidecaption', [
152       '@hidecaption' => $settings['hidecaption'] ? $this->t('Hidden') : $this->t('Visible'),
153     ]);
154
155     return $summary;
156   }
157
158 }