3 namespace Drupal\media_entity_instagram\Plugin\Field\FieldFormatter;
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;
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 {
29 * The instagram fetcher.
31 * @var \Drupal\media_entity_instagram\InstagramEmbedFetcher
36 * Constructs a InstagramEmbedFormatter instance.
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;
46 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
62 public function viewElements(FieldItemListInterface $items, $langcode) {
64 $settings = $this->getSettings();
65 foreach ($items as $delta => $item) {
68 foreach (Instagram::$validationRegexp as $pattern => $key) {
69 if (preg_match($pattern, $this->getEmbedCode($item), $item_matches)) {
70 $matches[] = $item_matches;
74 if (!empty($matches)) {
75 $matches = reset($matches);
78 if (!empty($matches['shortcode'])) {
80 if ($instagram = $this->fetcher->fetchInstagramEmbed($matches['shortcode'], $settings['hidecaption'], $settings['width'])) {
82 '#theme' => 'media_entity_instagram_post',
83 '#post' => (string) $instagram['html'],
84 '#shortcode' => $matches['shortcode'],
90 if (!empty($element)) {
91 $element['#attached'] = [
93 'media_entity_instagram/integration',
104 public static function defaultSettings() {
107 'hidecaption' => FALSE,
108 ] + parent::defaultSettings();
114 public function settingsForm(array $form, FormStateInterface $form_state) {
115 $elements = parent::settingsForm($form, $form_state);
117 $elements['width'] = [
119 '#title' => $this->t('Width'),
120 '#default_value' => $this->getSetting('width'),
122 '#description' => $this->t('Max width of instagram.'),
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.'),
138 public function settingsSummary() {
139 $settings = $this->getSettings();
143 if ($this->getSetting('width')) {
144 $summary[] = $this->t('Width: @width px', [
145 '@width' => $this->getSetting('width'),
149 $summary[] = $this->t('Caption: @hidecaption', [
150 '@hidecaption' => $settings['hidecaption'] ? $this->t('Hidden') : $this->t('Visible'),
157 * Extracts the raw embed code from input which may or may not be wrapped.
159 * @param mixed $value
160 * The input value. Can be a normal string or a value wrapped by the
163 * @return string|null
164 * The raw embed code.
166 protected function getEmbedCode($value) {
167 if (is_string($value)) {
170 elseif ($value instanceof FieldItemInterface) {
171 $class = get_class($value);
172 $property = $class::mainPropertyName();
174 return $value->$property;