3 namespace Drupal\media_entity_instagram\Plugin\Field\FieldFormatter;
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;
12 * Plugin implementation of the 'instagram_embed' formatter.
15 * id = "instagram_embed",
16 * label = @Translation("Instagram embed"),
18 * "link", "string", "string_long"
22 class InstagramEmbedFormatter extends FormatterBase {
24 use EmbedCodeValueTrait;
29 public function viewElements(FieldItemListInterface $items, $langcode) {
31 $settings = $this->getSettings();
32 foreach ($items as $delta => $item) {
34 foreach (Instagram::$validationRegexp as $pattern => $key) {
35 if (preg_match($pattern, $this->getEmbedCode($item), $matches)) {
40 if (!empty($matches['shortcode'])) {
42 '#type' => 'html_tag',
45 'allowtransparency' => 'true',
47 'position' => 'absolute',
49 'src' => '//instagram.com/p/' . $matches['shortcode'] . '/embed/',
50 'width' => $settings['width'],
51 'height' => $settings['height'],
63 public static function defaultSettings() {
67 ) + parent::defaultSettings();
73 public function settingsForm(array $form, FormStateInterface $form_state) {
74 $elements = parent::settingsForm($form, $form_state);
76 $elements['width'] = array(
78 '#title' => $this->t('Width'),
79 '#default_value' => $this->getSetting('width'),
81 '#description' => $this->t('Width of instagram.'),
84 $elements['height'] = array(
86 '#title' => $this->t('Height'),
87 '#default_value' => $this->getSetting('height'),
89 '#description' => $this->t('Height of instagram.'),
98 public function settingsSummary() {
100 $this->t('Width: @width px', [
101 '@width' => $this->getSetting('width'),
103 $this->t('Height: @height px', [
104 '@height' => $this->getSetting('height'),