First of all thanks for the integration of Instagram with media entity, seems to work smoothly. One thing I am missing though, is that the embedded posts should be responsive, like they are if you would use the embed code from Instagram directly. I also had the problem, that videos did not play in the embedded posts.

Did I miss something and this should be possible already?

Thank you for your help.

CommentFileSizeAuthor
#50 interdiff.txt715 bytesbkosborne
#50 make_posts_responsive-2807735-49.patch31.73 KBbkosborne
#49 interdiff.txt715 bytesbkosborne
#49 make_posts_responsive-2807735-49.patch715 bytesbkosborne
#46 interdiff-2807735-43-46.txt662 bytesit-cru
#46 make_posts_responsive-2807735-46.patch31.67 KBit-cru
#43 interdiff-2807735-42-43.txt1.03 KBtjwelde
#43 make_posts_responsive-2807735-43.patch31.64 KBtjwelde
#42 interdiff-2807735-38-42.txt767 byteschr.fritsch
#42 make_posts_responsive-2807735-42.patch31.54 KBchr.fritsch
#40 iframe.png401.81 KBchr.fritsch
#40 oembed.png420.52 KBchr.fritsch
#38 interdiff-2807735-35-38.patch12.01 KBchr.fritsch
#38 make_posts_responsive-2807735-38.patch31.52 KBchr.fritsch
#35 interdiff-2807735-33-35.txt14.19 KBchr.fritsch
#35 make_posts_responsive-2807735-35.patch27.21 KBchr.fritsch
#33 make_posts_responsive-2807735-33.patch17.68 KBchr.fritsch
#31 make_posts_responsive-2807735-31.patch16 KBit-cru
#29 make_posts_responsive-2807735-29.patch15.73 KBit-cru
#24 make_posts_responsive-2807735-24-D8.patch5.58 KBdarrenwh
#24 interdiff-2807735-22-24.txt542 bytesdarrenwh
#22 make_posts_responsive-2807735-22.patch5.83 KBit-cru
#20 make_posts_responsive-2807735-20.patch5.87 KBit-cru
#13 interdiff-2807735-6-13.txt3.54 KBchr.fritsch
#13 make_posts_responsive-2807735-13.patch7.71 KBchr.fritsch
#11 interdiff-2807735-6-11.txt3.54 KBchr.fritsch
#11 make_posts_responsive-2807735-11.patch7.78 KBchr.fritsch
#6 2807735_6.patch5.71 KBslashrsm
#5 instagram_embedding_fieldformatter.jpg22.75 KBabaier

Comments

toni4i created an issue. See original summary.

slashrsm’s picture

Issue tags: +D8Media

Videos should play I guess as we don't do anything custom with that. We just print the embed code.

With regards to the responsiveness I am not sure. Never really looked at it, but it would be very nice to support that. I assume this would only require a CSS tweak?

abaier’s picture

Okay, I tried it with another video and it worked. The before mentioned one does not even play on instagram.com itself, sorry …

Regarding the responsiveness I managed to solve this by leaving the pixel values (width/height) inside the display settings of the field formatter empty and assign the following css to the iframe: width: 100%; max-width: 100%; … Its container element defines the final size, and everything now scales properly according to the browser width.
To mention is, that this only worked after I added //platform.instagram.com/en_US/embeds.js to my libraries.yml – without it the height of the iframe will not be recalculated to fit the actual size of the post and the content would be cropped.

Instagram prepared this quite well, so we maybe don't really need the fixed size parameters, but rather percentage values:

The embedded post is responsive and will adapt to the size of its container. This means that the height will vary depending on the container width and the length of the caption. You can set the maximum width by using the maxwidth parameter. You can also use the hidecaption parameter to display the post without the caption.

(https://www.instagram.com/developer/embedding / Chapter: "Embedding for Developers")

Would be nice if you could have a look at this behaviour. Thanks in advance!

slashrsm’s picture

Would you upload the patch for that change that you did on your site?

abaier’s picture

StatusFileSize
new22.75 KB

I did not change anything in the module itself. I just left the fields inside the field formatter empty instead of adding pixel values. The rest was only css inside my theme.

slashrsm’s picture

StatusFileSize
new5.71 KB

Something like this I guess....

slashrsm’s picture

Status: Active » Needs review
slashrsm’s picture

Issue tags: +Novice
abaier’s picture

Seems to work fine, thank you! The only thing I noticed was, that sometimes not all embedded posts got the right height assigned, when multiple posts were on one page.

slashrsm’s picture

Status: Needs review » Needs work
chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new7.78 KB
new3.54 KB

I got the same error like toni4i with multiple images. So i found this very small js https://github.com/ryanburnette/responsive-instagram, which worked very fine for me.

Here is a new patch

Status: Needs review » Needs work

The last submitted patch, 11: make_posts_responsive-2807735-11.patch, failed testing.

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new7.71 KB
new3.54 KB

Damn it. Missed one test

abaier’s picture

Hey @chr.fritsch, I reversed patch #6 and applied #13. Now I am experiencing, that the iframe of every individual post on the page gets the same fixed size, for me 610px width and 640px height, instead of applying the height of "iframe .embed", which would be the correct value. So I end up with some posts being cropped and some having whitespace below them.

Are there any other settings that I have to adjust or maybe some css that is necessary to make it work? I already set the iframe to width: 100%, max-width: 100%; to scale it within my container. But unfortunately the height does not get updated correctly.

Thanks for your help!

it-cru’s picture

Why you are using iFrame for a responsive embed of Instagram, when instagram API gives us a responsive oEmbed solution without requirement of API Key or Client ID for free?

https://www.instagram.com/developer/embedding/

Perhaps it would be better to re-factor the instagram embed field formatter to use oEmbed instead of iFrame?

Our old dirty way without using media_entity_instagram with an instagram paragraph:

  /* @var Drupal\paragraphs\Entity\Paragraph $paragraph */
  $paragraph = $variables['paragraph'];
  if ($paragraph->getType() === 'instagram' && !$paragraph->hasField('field_media')) {
    try {
      $instagram_url = $paragraph->field_link->getValue();
      $instagram_url = $instagram_url[0]['uri'];

      $client = \Drupal::httpClient();
      $request = $client->request('GET', 'http://api.instagram.com/oembed?url=' . $instagram_url . '&omitscript=true');

      if ($request->getStatusCode() === 200) {
        $instagram_json = json_decode($request->getBody()->getContents());
        $instagram = array(
          '#markup' => (string) $instagram_json->html,
        );
        $variables['content']['instagram'] = $instagram;
        $variables['#attached']['library'][] = 'paragraphs_starterkit_instagram/instagram.embeds';
      }
    } catch(Exception $e) {}
chr.fritsch’s picture

I talked about that with slashrsm on IRC. We both think it makes sense to switch to the API approach. Would be nice if you could provide a patch for that

it-cru’s picture

I will try to start working on a patch for this today. I think main problem is, that we does not request API when we render the instagram embed. Perhaps we could hold JSON response in some text field or so? Because when you have many instagram media entities in a node it blocks rendering.

it-cru’s picture

Perhaps we can use https://www.drupal.org/project/json_field for storing JSON data from embed API call of each pinterest media entity. When we have only a source field we can fetch JSON directly as fallback?

it-cru’s picture

I take a look at media_entity_twitter and I think it would be enoug to cache data from embedding JSON request. I will sprint on this before DrupalCamp in Munich next week if someone wants to join at Hubert Burda Media :)

it-cru’s picture

StatusFileSize
new5.87 KB

No caching of requested JSON from instagram embed API yet. Hide caption enable/disable per view mode. Hope I have time to work on JSON caching service on next days.

Status: Needs review » Needs work

The last submitted patch, 20: make_posts_responsive-2807735-20.patch, failed testing.

it-cru’s picture

Status: Needs work » Needs review
StatusFileSize
new5.83 KB

Re-factor patch from #20. Hope now tests does'n fail.

Status: Needs review » Needs work

The last submitted patch, 22: make_posts_responsive-2807735-22.patch, failed testing.

darrenwh’s picture

Status: Needs work » Needs review
StatusFileSize
new542 bytes
new5.58 KB

Updated patch omitting test for iframe that has been removed and added interdiff.

darrenwh’s picture

Issue tags: +mssprintjan17

Status: Needs review » Needs work

The last submitted patch, 24: make_posts_responsive-2807735-24-D8.patch, failed testing.

it-cru’s picture

Tomorrow I tink I've add a InstagramFetcher and caching like in media_entity_twitter module. Patch will follow as soon as possible.

abaier’s picture

Great, thank you! I am following the progress and will provide feedback when I've tested it.

Quick question: The tests were all ran against 8.3. Should work fine with 8.2.5, though?!

it-cru’s picture

Status: Needs work » Needs review
StatusFileSize
new15.73 KB

Add a InstagramEmbedFetcher (some parameters missing yet: see - https://www.instagram.com/developer/embedding/#oembed) for getting embed for InstagramEmbedFormatter field formatter with caching.

Display of caption is configurable. InstagramEmbedFormatterTest not testet yet.

Status: Needs review » Needs work

The last submitted patch, 29: make_posts_responsive-2807735-29.patch, failed testing.

it-cru’s picture

Status: Needs work » Needs review
StatusFileSize
new16 KB

Add better error handling for InstagramEmbedFetcher. I think tests will currently fail.

Status: Needs review » Needs work

The last submitted patch, 31: make_posts_responsive-2807735-31.patch, failed testing.

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new17.68 KB

I fixed some coding styles, the tests and implemented the ContainerFactoryPluginInterface for InstagramEmbedFormatter.

The hide option should also work now.

Status: Needs review » Needs work

The last submitted patch, 33: make_posts_responsive-2807735-33.patch, failed testing.

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new27.21 KB
new14.19 KB

Another round of big changes. I adjusted the MediaEntity\Type\Instagram to also fetch everything from the oembed api. With that we lose the ability to get instagram tags. But we win, that all the other fields are now available without any credentials.

Status: Needs review » Needs work

The last submitted patch, 35: make_posts_responsive-2807735-35.patch, failed testing.

mtodor’s picture

Nice work everyone! This is realty nice improvement. I did code review and everything looks good, I have just few nitpicks.

  1. +++ b/src/InstagramEmbedFetcher.php
    @@ -0,0 +1,86 @@
    +  public function fetchInstagramEmbed($shortcode, $hidecaption = FALSE) {
    

    Nitpick: $hidecaption should be added in Interface as option too, or make it required argument.

  2. +++ b/src/InstagramEmbedFetcher.php
    @@ -0,0 +1,86 @@
    +    $client = \Drupal::httpClient();
    

    We should inject this service: 'http_client'

  3. +++ b/src/InstagramEmbedFetcher.php
    @@ -0,0 +1,86 @@
    +      watchdog_exception('media_entity_instagram', $e, "Could not retrieve Instagram post $shortcode.");
    

    I think it would be better to inject logger channel and use that, instead of watchdog_ function.

  4. +++ b/src/Plugin/Field/FieldFormatter/InstagramEmbedFormatter.php
    @@ -96,13 +128,9 @@ class InstagramEmbedFormatter extends FormatterBase {
    +      $this->t('Caption: @hidecaption', ['@hidecaption' => $settings['hidecaption'] ? 'Hidden' : 'Visible']),
    

    "Hidden" and "Visible" should be translated too.

  5. +++ b/src/InstagramEmbedFetcher.php
    @@ -0,0 +1,86 @@
    +    if ($this->cache && $cached_instagram_post = $this->cache->get($shortcode . "_" . $hidecaption)) {
    

    Nitpick: it would be nice to set cache key in custom variable and use it in both places + wrap $hidecaption in intval(), so that we have nicer format for key when $hidecaption is FALSE ("key_0" instead of "key_").

  6. +++ b/src/InstagramEmbedFetcher.php
    @@ -0,0 +1,86 @@
    +      'url' => 'http://instagr.am/p/' . $shortcode . '/',
    ...
    +      $response = $client->request('GET', 'http://api.instagram.com/oembed?' . $queryParameter);
    

    Small consideration. Maybe static urls can be set in protected static properties ('http://instagr.am/p/' and 'http://api.instagram.com/oembed'). Additionally, I'm not sure can we use https instead of http?

  7. Additionally, maybe we should add update hook. That should remove all width/height configuration options and add default "hidecaption", so that configuration is in sync with schema.

But overall, everything looks great and clean.

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new31.52 KB
new12.01 KB

Adjusted all the comments from #37

Also:

  • Implemented the maxWidth property from the instagram api.
  • Adjusted the README file

Status: Needs review » Needs work

The last submitted patch, 38: interdiff-2807735-35-38.patch, failed testing.

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new420.52 KB
new401.81 KB

The patch is now working pretty good. One thing i noticed, the layout of the posts changed. I have no clue why. Maybe these iframe method is deprecated.

New oembed implementation:
Oembed

Old iframe implementation:
iframe

it-cru’s picture

Perhaps Instagram change or optimize currently styling of embeded posts. We also see this or other layouts on our projects sometimes.

Also see https://www.instagram.com/developer/embedding/

chr.fritsch’s picture

New patch to get the correct extension from the filename

tjwelde’s picture

It would be nice to pass the shortcode to the template, too. That way, you have more options, to override it, for example for AMP or Facebook Instant Articles.
This patch adds that.

zuernbernhard’s picture

Patch works like a charm for us. Can we commit it to the module ?

zuernbernhard’s picture

Status: Needs review » Reviewed & tested by the community
it-cru’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new31.67 KB
new662 bytes

LoggerChannelFactory should be defined as LoggerChannelFactoryInterface

darrenwh’s picture

Status: Needs review » Reviewed & tested by the community

Looks goog

darrenwh’s picture

Looks good

bkosborne’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new715 bytes
new715 bytes

The oembed request should have a pretty short timeout on it. If the instagram oEmbed endpoint is unresponsive, you don't want to nuke your site performance waiting for eEmbed requests to timeout at the default time (prolly 30 seconds or more).

bkosborne’s picture

StatusFileSize
new31.73 KB
new715 bytes

Oops, wrong patch.

The last submitted patch, 49: make_posts_responsive-2807735-49.patch, failed testing.

  • chr.fritsch committed 89b1199 on 8.x-1.x
    Issue #2807735 by chr.fritsch, IT-Cru, bkosborne, darrenwh, tjwelde,...
chr.fritsch’s picture

Status: Needs review » Fixed

Thanks everyone for the hard work.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.