Problem/Motivation

With javascript disabled is not possible to save new media types because (as far as I investigated) the ajax reload of the form fills the source for the media type but this doesn't happen when no ajax is loaded.

The error on form save is

Error: Call to a member function getName() on null in Drupal\media\MediaSourceBase->prepareFormDisplay() (line 352 of core/modules/media/src/MediaSourceBase.php).

So even the source_field is created and stored but since the source_configuration is still empty it tries to load objects that do not have the configuration updated. With JS enabled this happen on the ajax reload of the form when the user choose a source (File, Image, Video, etc).

I actually bumped into this trying to write tests for
#2988433: Automatically create and configure Media Library view and form displays
since we want to submit the form to trigger the behaviour but using a JS test would be slow and not needed.

Proposed resolution

Fix the case for a non-js submit.

Remaining tasks

  • Fix the form
  • Write a test

User interface changes

None

API changes

None

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rodrigoaguilera created an issue. See original summary.

rodrigoaguilera’s picture

In the case the source field needs to be created the MediaType entity is saved twice because the source field creation depends on the MediaType to be stored and the MediaType needs to be updated with the name of the new field created.

But I can't think of a better way to do this.

Status: Needs review » Needs work

The last submitted patch, 2: 3018539-test-only-2.patch, failed testing. View results

rodrigoaguilera’s picture

Assigned: rodrigoaguilera » Unassigned
Status: Needs work » Needs review
Issue tags: +BarcelonaMediaSprint

Cool :)
Back to needs review

phenaproxima’s picture

Issue tags: +blocker

This blocks #2988433: Automatically create and configure Media Library view and form displays, which is a Media Library stable blocker.

phenaproxima’s picture

Thanks, @rodrigoaguilera! And for writing the test first, you rock :)

I found an alternative approach that will not involve saving the media type twice -- hopefully this satisfies all.

phenaproxima’s picture

Title: Trying to create a media type without JS enabled throws error and leaves media type incomplete. » Media types cannot be created without JavaScript

The last submitted patch, 6: 3018539-6-FAIL.patch, failed testing. View results

seanB’s picture

Status: Needs review » Reviewed & tested by the community

Looks good. Thanks!

rodrigoaguilera’s picture

Cool, much more elegant :)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/media/src/MediaTypeForm.php
    @@ -304,6 +304,18 @@ function ($item) {
    +    // If the media source has not been chosen yet, turn the submit button into
    +    // a normal button which doesn't submit the form, but instead rebuilds it
    +    // with the media source's configuration form visible. Normally this would
    +    // be done during an AJAX request, but that requires JavaScript. Without
    +    // JavaScript, it's possible to submit the form without a media source,
    +    // which causes a fatal error because ::save() rightfully assumes that the
    +    // media source has been chosen, and that a source field has been set up.
    +    if (empty($this->getEntity()->get('source'))) {
    +      $actions['submit']['#type'] = 'button';
    +    }
    

    This looks like a great solution. I think we can improve the comment by making the sentences shorter and more about what occurs rather than what occurs without the fix. Something like:

        // If the media source has not been chosen yet, turn the submit button into
        // a button. This rebuilds the form with the media source's configuration
        // form visible instead of saving the media type. This allows users to
        // create a Media type without JavaScript enabled. With JavaScript enabled
        // this occurs during an AJAX request.
        // @see \Drupal\media\MediaTypeForm::ajaxHandlerData()
    
  2. +++ b/core/modules/media/tests/src/Functional/MediaTypeCreationTest.php
    @@ -0,0 +1,42 @@
    + * Ensures that media UI works correctly.
    

    Tests the media UI without JavaScript.
    Just so nobody helpful converts this to a JS test.

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
2.73 KB
1.71 KB

Thanks! Fixed.

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Sending this little tyke back to RTBC because all that changed in the last patch is comments.

phenaproxima’s picture

Issue tags: +Media Initiative
phenaproxima’s picture

Title: Media types cannot be created without JavaScript » Media types cannot be created in the UI without JavaScript
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 3ba6bc1168 to 8.7.x and 897f0e461e to 8.6.x. Thanks!

  • alexpott committed 3ba6bc1 on 8.7.x
    Issue #3018539 by phenaproxima, rodrigoaguilera, alexpott: Media types...

  • alexpott committed 897f0e4 on 8.6.x
    Issue #3018539 by phenaproxima, rodrigoaguilera, alexpott: Media types...

Status: Fixed » Closed (fixed)

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