Problem/Motivation

Embedding the iframe in the page can result in a fairly large amount of assets and http requests that may not be needed if the user doesn't watch the video

Proposed resolution

Add a lazy load formatter that displays a thumbnail that when clicked, swaps the thumbnail for the video, triggering the iframe load and bandwidth etc.

This is similar to the colorbox formatter.

Before

After

Screenshot showing play button styling/treatment

Remaining tasks

Get test green
Review

User interface changes

None

API changes

New formatter

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

larowlan created an issue. See original summary.

larowlan’s picture

larowlan’s picture

Status: Active » Needs review
Sam152’s picture

This looks awesome. Lets add at least one entry to FieldOutputTest and then we're good to go.

+++ b/tests/src/FunctionalJavascript/LazyLoadFormatterTest.php
@@ -0,0 +1,52 @@
+use Drupal\local_testing\LocalTestingTrait;
...
+  use LocalTestingTrait;

Left over?

Sam152’s picture

Head is fixed. #2888653: Fix HEAD

dmegatool’s picture

I applied against the dev version but getting some php error... Is this working ? Do I need to apply the "Fix Head" patch too ?

Sam152’s picture

The other patch is already merged. You should be able to do a pull and then apply the patch. It's helpful to post the error you are getting if you're asking for help in fixing it :)

dmegatool’s picture

Edit : Unrelated error. Decluttering the thread...

Sam152’s picture

Video embed field isn't in your callstack, so unlikely to be the issue. Have you removed the "photoswipe" without uninstalling it first?

dmegatool’s picture

I thought the patch could have broken something with formatters... I ended up removing and reinstalling photoswipe and the error is gone, weird. Got it working now ! Gotta fix my css but this look very promising. Thank you very much for the help ;)

dmegatool’s picture

That patch is perfectly working work me, in the all the views I had the video embed formatter... Works across the board in all my tests. Thanks again !

Sam152’s picture

Clearly lots of demand for a formatter like this. I'll look at #4 and get this in when I can.

Sam152’s picture

Fixing #4 and resolving some issues I had with manual testing.

Sam152’s picture

Explaining some of the changes.

  1. +++ b/css/video_embed_field.responsive-video.css
    @@ -28,6 +28,10 @@
    +.video-embed-field-lazy img {
    +  width: 100%;
    +}
    

    This was required because for image styles smaller than the width of the container the video was in, the play button would appear off centre. Stretching the image seems appropriate, the container can either be made smaller or the image style can be made larger.

  2. +++ b/src/Plugin/Field/FieldFormatter/LazyLoad.php
    @@ -121,7 +121,6 @@ class LazyLoad extends FormatterBase implements ContainerFactoryPluginInterface
                 'video_embed_field/lazy-load',
    -            'video_embed_field/responsive-video',
    
    +++ b/video_embed_field.libraries.yml
    @@ -2,14 +2,15 @@ colorbox:
     lazy-load:
       js:
         js/video-embed-field.lazyLoad.js: {}
    ...
    +    - video_embed_field/responsive-video
    

    Lazyload doesn't work without responsive-video, so enforcing this at the library level instead of adding both.

  3. +++ b/video_embed_field.libraries.yml
    @@ -2,14 +2,15 @@ colorbox:
    -    - core/jquery
    -    - core/jquery.once
    +    - system/drupal.system
    

    Viewing as logged out user, it said "Drupal" wasn't defined. This seems like the right library to make sure all the stuff we need is there.

Sam152’s picture

Re-enabling tests I disabled in the last patch.

Sam152’s picture

Status: Needs review » Fixed

  • Sam152 committed 037b6d4 on 8.x-1.x authored by larowlan
    Issue #2888635 by Sam152, larowlan: Add a lazy load formatter
    

Status: Fixed » Closed (fixed)

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

drc0’s picture

Hello, is that possible for the lazy load formatter to replace the image thumbnail with the actual video iframe?

edit:
as usual check for js errors before issuing a comment, the formatter works as intended!