3 namespace Drupal\media_entity_instagram\Plugin\Field\FieldFormatter;
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;
16 * Plugin implementation of the 'instagram_embed' formatter.
19 * id = "instagram_embed",
20 * label = @Translation("Instagram embed"),
22 * "link", "string", "string_long"
26 class InstagramEmbedFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
28 use EmbedCodeValueTrait;
31 * The instagram fetcher.
33 * @var \Drupal\media_entity_instagram\Plugin\MediaEntity\Type\InstagramEmbedFetcher
38 * Constructs a InstagramEmbedFormatter instance.
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;
48 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
64 public function viewElements(FieldItemListInterface $items, $langcode) {
66 $settings = $this->getSettings();
67 foreach ($items as $delta => $item) {
70 foreach (Instagram::$validationRegexp as $pattern => $key) {
71 if (preg_match($pattern, $this->getEmbedCode($item), $item_matches)) {
72 $matches[] = $item_matches;
76 if (!empty($matches)) {
77 $matches = reset($matches);
80 if (!empty($matches['shortcode'])) {
82 if ($instagram = $this->fetcher->fetchInstagramEmbed($matches['shortcode'], $settings['hidecaption'], $settings['width'])) {
84 '#theme' => 'media_entity_instagram_post',
85 '#post' => (string) $instagram['html'],
86 '#shortcode' => $matches['shortcode'],
92 if (!empty($element)) {
93 $element['#attached'] = [
95 'media_entity_instagram/integration',
106 public static function defaultSettings() {
109 'hidecaption' => FALSE,
110 ] + parent::defaultSettings();
116 public function settingsForm(array $form, FormStateInterface $form_state) {
117 $elements = parent::settingsForm($form, $form_state);
119 $elements['width'] = [
121 '#title' => $this->t('Width'),
122 '#default_value' => $this->getSetting('width'),
124 '#description' => $this->t('Max width of instagram.'),
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.'),
140 public function settingsSummary() {
141 $settings = $this->getSettings();
145 if ($this->getSetting('width')) {
146 $summary[] = $this->t('Width: @width px', [
147 '@width' => $this->getSetting('width'),
151 $summary[] = $this->t('Caption: @hidecaption', [
152 '@hidecaption' => $settings['hidecaption'] ? $this->t('Hidden') : $this->t('Visible'),