Problem/Motivation

FileMediaFormatterBase doesn't allow to specifie preload attribute on video and audio file. This attribute allow to control media loading behavior.
(See : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video)

Proposed resolution

  • Add new "preload" select field into FileMediaFormatterBase with three options :
    • None : The author thinks that the browser should NOT load the video when the page loads,
    • Auto : The author thinks that the browser should load the entire video when the page loads,
    • Metadata : The author thinks that the browser should load only metadata when the page loads.
  • Default value must to be "metadata"

Remaining tasks

  • Create patch to allow this
  • Update tests for FileVideoFormatter and FileAudioFormatter to check preload attribute exist
  • Update schema for the configuration file of the File module

User interface changes

  • New field into configuration form of FileVideoFormatter and FileAudioFormatter
  • Update summary of Fieldformatter

API changes

None

Data model changes

  • update field.formatter.settings.file_video mapping.

Issue fork drupal-3048458

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pclaitte created an issue. See original summary.

pclaitte’s picture

StatusFileSize
new2.48 KB
duaelfr’s picture

Status: Active » Needs work

Good job for your first issue! :)
I have a few comments, though.

  1. +++ w/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php
    @@ -66,6 +67,17 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
    +          'none' => $this->t('one : The author thinks that the browser should NOT load the video when the page loads'),
    

    There is a "N" missing at the beginning of the translatable string.

  2. +++ w/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php
    @@ -66,6 +67,17 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
    +        '#options' => [
    +          'none' => $this->t('one : The author thinks that the browser should NOT load the video when the page loads'),
    +          'auto' => $this->t('Auto : The author thinks that the browser should load the entire video when the page loads'),
    +          'metadata' => $this->t('Metadata : The author thinks that the browser should load only metadata when the page loads'),
    +        ],
    

    Options values should follow these rules:

    • no space before the colon symbol in english
    • no capitalized letter after the colon symbol
    • each string should end with a dot
pclaitte’s picture

Status: Needs work » Needs review
vadim.hirbu’s picture

StatusFileSize
new2.49 KB

Updated latest patch according latest comments.

duaelfr’s picture

Issue tags: +Needs tests

The code looks good.
We should increase the test coverage to ensure that this is working properly and that we will not break it in the future :)

pclaitte’s picture

StatusFileSize
new3.31 KB
  • Add test : Add assert into FileVideoFormatterTest.php (checks preload attribute exists with default value)
  • Fix coding standards
pclaitte’s picture

Issue tags: -Needs tests
pclaitte’s picture

Status: Needs review » Reviewed & tested by the community

Installed and tested on another Drupal 8 instance. It works weel.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 7: preload_attribute-1-3048458-6.patch, failed testing. View results

idebr’s picture

  1. +++ w/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php
    @@ -66,6 +67,17 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
    +        '#options' => [
    +          'none' => $this->t('None: the author thinks that the browser should NOT load the video when the page loads.'),
    +          'auto' => $this->t('Auto: the author thinks that the browser should load the entire video when the page loads.'),
    +          'metadata' => $this->t('Metadata: the author thinks that the browser should load only metadata when the page loads.'),
    +        ],
    

    I suggest using more objective text explaining what the option does. From the referenced page:

    preload
    This enumerated attribute is intended to provide a hint to the browser about what the author thinks will lead to the best user experience with regards to what content is loaded before the video is played. It may have one of the following values:

    • none: Indicates that the video should not be preloaded.
    • metadata: Indicates that only video metadata (e.g. length) is fetched.
    • auto: Indicates that the whole video file can be downloaded, even if the user is not expected to use it.

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video

  2. Since the patch is changing existing configuration options, it should include an upgrade to add the new option to existing configuration.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jnettik’s picture

Status: Needs work » Needs review
StatusFileSize
new52.88 KB
new4.62 KB
new6.52 KB

A few thoughts on this issue:

  1. Why include the super verbose option labels at all? It makes the select list super long (see screenshot) and doesn't seem to be the correct place to document things like that. IMO we should either leave it off (the docs aren't hard to find), or put a link to the Mozilla docs in the description text for the full field.
  2. Should this be moved up a level to FileMediaFormatterBase, since this attribute applies to both video and audio tags? Other attributes like loop or autoplay live there.
  3. In my testing, since the field formatter provides a default value (metadata), once it's applied the setting is used by existing files. Sequential config exports will then add the new setting to existing config. Is an update still necessary?

I've attached a patch with my suggested changes (minus the description, which I'd be more than happy to add).

Status: Needs review » Needs work

The last submitted patch, 13: preload_attribute-3048458-12.patch, failed testing. View results

jnettik’s picture

Status: Needs work » Needs review
StatusFileSize
new4.62 KB

Fixed failing test

pclaitte’s picture

Status: Needs review » Reviewed & tested by the community

I agree with @jnttik on alls points, or almost :)
I think that a link to Mozilla documentation can be usefull for casual Drupal users.

The Patch #15 works perfectly on fresh install of the last Drupal version.

pclaitte’s picture

Title: Add support of "preload" attribute into FileVideoFormatter » Add support of "preload" attribute into FileMediaFormatterBase
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

What about existing configuration? We need to populate this with a default value - looks like the choice is "metadata". So we need a post update function to use the ConfigEntityUpdater class to add this configuration. We also need a test for it working.

+++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php
@@ -56,6 +57,17 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
+        '#type' => 'select',

Given there are only three options should this be radios? And maybe a #description is warranted that explains what this setting is about. I don't find the words that self explanatory.

pclaitte’s picture

Given there are only three options should this be radios?

Yes it can be better. We can transform select to radio list. With this, we allow us to be more verbose for describe the behavior of the new option.

And maybe a #description is warranted that explains what this setting is about. I don't find the words that self explanatory.

I suggest to take the words of the Mozilla documentation, it's clear. So, we will have a description bloc like this :
This attribute is intended to provide a hint to the browser about what content is loaded before the video is played.

The values list with their descriptions:

  • none: Indicates that the video should not be preloaded.
  • metadata: Indicates that only video metadata (e.g. length) is fetched.
  • auto: Indicates that the whole video file can be downloaded, even if the user is not expected to use it.

Empty value is possible but i don't recommend to allow this because default value can be different between browsers as specified into documentation.

And maybe a conditional activation of preload option. If autoplay option is checked, preload must to be disabled because in this case, browser must to download video or sound for play it.

What about existing configuration? We need to populate this with a default value - looks like the choice is "metadata".

I'm not sure to understand your advise on the configuration, how can we have existing configuration with a new option ? If it about the existing video fields, we are not sure that user want the "metadata" value and we can't to change it arbitrary. If option is not set on the existing video or audio fields, i think preload attribute doesn't to be present.

alexpott’s picture

@pclaitte there is existing configuration which does not have the preload configuration. We need a post update function to find this configuration and set it to the default value. If the configuration is optional then we need to handle it differently in the form. With the patch as it stands if you open an existing field formatter config form and, without making any changes, press save it'll change the configuration and that shouldn't happen.

pclaitte’s picture

@alexpott ok i understand. Another way would be to add a defaut empty value into the choices list which, if set, doesn't add the preload attribute. Maybe is it simplest? What do you think about it ?

(sorry for my english)

pclaitte’s picture

StatusFileSize
new42.22 KB

Below, an example with radio buttons and conditional activation

Capture

pclaitte’s picture

Issue summary: View changes

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pcambra made their first commit to this issue’s fork.

pcambra’s picture

Status: Needs work » Needs review
Issue tags: - +Needs tests

@alexpott I'd love to get this in, I've added @pclaitte changes onto an MR and the following changes:

- Widget is now radios as suggested
- Added short descriptions for the 3 options.
- Added a post update with a config updater

I think the update needs a test but I'd appreciate some guidance on where to find something similar (this has been my first encounter with the config entity updater)

pcambra’s picture

Assigned: pclaitte » pcambra
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs upgrade path

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

This seems like it's going to need an upgrade path for existing settings.

jose reyero’s picture

Patch looks good to me, already pretty polished...

What I'm wondering about is: how "safe" is to force a default preload attribute value on sites that didn't have any?

I mean, if I had a page full of videos with no 'preload' attribute, maybe just an image placeholder, and suddenly I get that preload=metadata default.... will browsers just start preloading when they didn't before?

The point is: does it make sense to force a default value? or to force any value at all? Should we skip the update script until the user actually edits the formatter?

For background, I've found this pretty interesting post about how browsers behave, but note it is 10 years old, couldn't find and updated one... https://www.stevesouders.com/blog/2013/04/12/html5-video-preload/

pcambra’s picture

Status: Needs work » Needs review

Setting to NR for more input as per #35

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs upgrade path +Needs upgrade path tests

See the upgrade path but it needs test coverage. Could be something simple as

Before this value was null
Run updates
This value is now false

Didn’t test the MR

pcambra’s picture

Status: Needs work » Needs review

@smustgrave please see #35 regarding update path.

smustgrave’s picture

Status: Needs review » Needs work

Yes the update hook is there but there is no test coverage of it.

See BlockContentUpdateTest for example

pcambra’s picture

@smustgrave the question in #35 refers to whether we should have the update hook in the first place.

smustgrave’s picture

So if I do a config export.
Go into a field or display using this formatterBase.
If I make 0 changes but save the field and do drush export again and it's showing this new setting then yes it will need an update path.

If no then it will not.

The update hook should set to disabled value though.

That help some?

penyaskito’s picture

Status: Needs work » Needs review

@smustgrave "Should we skip the update script until the user actually edits the formatter?" That's exactly what #35 is explaining.

It's ok to assume changes can happen when I save the formatter, but NOT when I just upgrade my site. That could have unexpected consequences, and that's what #35 is asking for feedback from @alexpott.

smustgrave’s picture

Actually the opposite if I save a formatter without making a change and I get changes in my config now that should not be expected.

Same for how we handle other field changes but will let alexpott respond.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

catch’s picture

I only scanned the MR quickly, but I couldn't see the a presave hook to update shipped config (i.e. default config in modules), that should happen as well as the update function.

We need to update the configuration to something in both post update and presave, so that sites don't get unexpected config changes in runtime - better if they get them during a core update which is when they'll be looking for them.

So.. I think it comes down to what a non-intrusive update looks like - i.e. should it be 'metadata' or 'none', or do a need an additional option for 'unspecified' to preserve current behaviour and update sites to that?

Didn't look around for docs on browser behaviour yet so not sure what the answer is, but definitely we should update to something here.

mrshowerman made their first commit to this issue’s fork.

mrshowerman’s picture

StatusFileSize
new7.9 KB

Rebased 10.1.x into MR; providing patch for 9.5 in case anyone needs it.
Leaving in NW as per #45.

catch’s picture

heddn’s picture

#3309016: Add image preload option to help boost actual and perceived performance is doing some similar things for images. It also has some of the bits around update hooks that are being asked about in #40 and below. For already committed code, look at what ResponsiveImageConfigUpdater is doing.

Can we update the IS to differentiate between what _this_ issue is doing vs #3309016?

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mrshowerman changed the visibility of the branch 11.x to hidden.

mrshowerman’s picture

Assigned: pcambra » Unassigned
StatusFileSize
new7.84 KB

Created MR against 11.x. Adding patch for 10.3.x.

wmfinck’s picture

Has this not been implemented? If so, how do I find it? I do not want to apply a patch for something that won't show up in production. But I am sorry for wasting anyone's time if I am missing something...

I have no preload options at /admin/structure/media/manage/audio/display

Not being able to prevent preloading of media in D10 is a deal-breaker for me, I cannot use Drupal media. I create sites with audio library pages using views, and when a page of multiple audio files is displayed, sometimes dozens, I cannot have them all downloading.

For this reason, I am compelled to retain the method I used in Drupal 7, which was to create fields with preconfigured HTML5 code using tokens. Doing that I need a file field and two text fields for every audio file (the easiest way without making a custom module, as I am not a developer).

pcambra changed the visibility of the branch 3048458-preload-attribute to hidden.

pcambra changed the visibility of the branch 3048458-add-support-of to hidden.

mrshowerman changed the visibility of the branch 3048458-preload-attribute to active.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

sleitner made their first commit to this issue’s fork.

sleitner’s picture

Status: Needs work » Needs review
Related issues: +#2954834: Add poster image to HTML5 media videos

After adding a poster image to video files in #2954834: Add poster image to HTML5 media videos with requireKey: false, the preload option do not need an updater and updater test coverage as well?

sleitner changed the visibility of the branch 3048458-php84 to hidden.

sleitner’s picture