Updating Media dependent modules to versions compatible with core Media.
[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\FieldItemInterface;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Field\FormatterBase;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Drupal\media_entity_instagram\Plugin\media\Source\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   /**
29    * The instagram fetcher.
30    *
31    * @var \Drupal\media_entity_instagram\InstagramEmbedFetcher
32    */
33   protected $fetcher;
34
35   /**
36    * Constructs a InstagramEmbedFormatter instance.
37    */
38   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, InstagramEmbedFetcher $fetcher) {
39     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
40     $this->fetcher = $fetcher;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
47     return new static(
48       $plugin_id,
49       $plugin_definition,
50       $configuration['field_definition'],
51       $configuration['settings'],
52       $configuration['label'],
53       $configuration['view_mode'],
54       $configuration['third_party_settings'],
55       $container->get('media_entity_instagram.instagram_embed_fetcher')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function viewElements(FieldItemListInterface $items, $langcode) {
63     $element = [];
64     $settings = $this->getSettings();
65     foreach ($items as $delta => $item) {
66       $matches = [];
67
68       foreach (Instagram::$validationRegexp as $pattern => $key) {
69         if (preg_match($pattern, $this->getEmbedCode($item), $item_matches)) {
70           $matches[] = $item_matches;
71         }
72       }
73
74       if (!empty($matches)) {
75         $matches = reset($matches);
76       }
77
78       if (!empty($matches['shortcode'])) {
79
80         if ($instagram = $this->fetcher->fetchInstagramEmbed($matches['shortcode'], $settings['hidecaption'], $settings['width'])) {
81           $element[$delta] = [
82             '#theme' => 'media_entity_instagram_post',
83             '#post' => (string) $instagram['html'],
84             '#shortcode' => $matches['shortcode'],
85           ];
86         }
87       }
88     }
89
90     if (!empty($element)) {
91       $element['#attached'] = [
92         'library' => [
93           'media_entity_instagram/integration',
94         ],
95       ];
96     }
97
98     return $element;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public static function defaultSettings() {
105     return [
106       'width' => NULL,
107       'hidecaption' => FALSE,
108     ] + parent::defaultSettings();
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function settingsForm(array $form, FormStateInterface $form_state) {
115     $elements = parent::settingsForm($form, $form_state);
116
117     $elements['width'] = [
118       '#type' => 'number',
119       '#title' => $this->t('Width'),
120       '#default_value' => $this->getSetting('width'),
121       '#min' => 320,
122       '#description' => $this->t('Max width of instagram.'),
123     ];
124
125     $elements['hidecaption'] = [
126       '#type' => 'checkbox',
127       '#title' => $this->t('Caption hidden'),
128       '#default_value' => $this->getSetting('hidecaption'),
129       '#description' => $this->t('Enable to hide caption of Instagram posts.'),
130     ];
131
132     return $elements;
133   }
134
135   /**
136    * {@inheritdoc}
137    */
138   public function settingsSummary() {
139     $settings = $this->getSettings();
140
141     $summary = [];
142
143     if ($this->getSetting('width')) {
144       $summary[] = $this->t('Width: @width px', [
145         '@width' => $this->getSetting('width'),
146       ]);
147     }
148
149     $summary[] = $this->t('Caption: @hidecaption', [
150       '@hidecaption' => $settings['hidecaption'] ? $this->t('Hidden') : $this->t('Visible'),
151     ]);
152
153     return $summary;
154   }
155
156   /**
157    * Extracts the raw embed code from input which may or may not be wrapped.
158    *
159    * @param mixed $value
160    *   The input value. Can be a normal string or a value wrapped by the
161    *   Typed Data API.
162    *
163    * @return string|null
164    *   The raw embed code.
165    */
166   protected function getEmbedCode($value) {
167     if (is_string($value)) {
168       return $value;
169     }
170     elseif ($value instanceof FieldItemInterface) {
171       $class = get_class($value);
172       $property = $class::mainPropertyName();
173       if ($property) {
174         return $value->$property;
175       }
176     }
177   }
178
179 }