This is a port of #2813685: Make the label form element non-required in the entity form, which didn't make it in time for media in core.

The reasoning behind this is:
- The MediaSourceBase class automatically provides a default name for entities whose source plugins didn't provide any specific name, so there is no real need for the form element to be required.
- Probably 80% of the use cases will want this populated automatically, so we could even make it hidden by default and let users who need it present on the form to manually enable it.

Comments

marcoscano created an issue. See original summary.

marcoscano’s picture

Status: Active » Needs review
StatusFileSize
new2.61 KB
marcoscano’s picture

Assigned: marcoscano » Unassigned
slashrsm’s picture

+++ b/core/modules/media/src/Entity/Media.php
@@ -366,15 +366,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
-      ->setRequired(TRUE)

I think that the field needs to stay required (this won't affect only the form element but also the entity validation). Entity can't be without the name.

Recently, on a client project, we solved this by making the form element non-required in a form alter. Then we added an entity builder callback that checked if the name was provided and triggered the auto logic if not. This ensured that the name was always there for the entity validation. Maybe core could use a similar approach?

phenaproxima’s picture

Issue tags: -D8Media +Media Initiative

Re-tagging.

xjm’s picture

Issue summary: View changes
StatusFileSize
new41.24 KB
new35.78 KB

Coming here from #2831940: Create file field widget on top of media entity. Rather than making the field optional and defaulting to some arcane combination of tokens and UUID, I'd suggest doing something like what we do for field names or block machine names, where the name defaults to the name of the thing in the source field (for local media, the filename). The UI could work like these do, with a generated default but a link to customize it:

For remote media, maybe the plugin could provide its own default name.

marcoscano’s picture

Just a small precision: the uuid-based default name is only used as a very last resource.
"Normal" plugins should provide sensible default names. Image/File from core already provide the filename as the default name, and remote-media source should also use some metadata attribute (such as the video title, author, etc) as a default option.

The default fallback will only be used if some contrib/custom plugin doesn't provide a specific name, which shouldn't happen that often.

wim leers’s picture

Category: Feature request » Task
Status: Needs review » Needs work
Issue tags: +Usability

#4 I agree that the field should remain required. The solution you describe seems simple & pragmatic.

#6: That feels like a nice to have, for the reasons given in #7.

#7: this totally makes sense :)

I don't think this is a feature, I think this is pretty much required for a remotely usable Media system.

xjm’s picture

I don't think we should form alter the field to be not required. I think we should present the user with the default name and give them the opportunity to change it, like we do elsewhere in core. We already have UI patterns for this.

pk188’s picture

Status: Needs work » Needs review
StatusFileSize
new2.6 KB

Re rolled the patch as it failed to apply.

pk188’s picture

Status: Needs review » Needs work

Agreed with #8. So changing status again to "needs work".

wim leers’s picture

#9 AFAICT we don't have a pattern for this. The screenshots in #6 do not apply to the use case here. The two examples you give merely generate a default machine name that the user can choose to edit. The user still must type an actual name.
In the case of Media, no default name can be generated until a file is uploaded (or for remote media: until a URL is pasted). That means no name until that action has been performed. Which is why @slashrsm in #4 explains the name is optional in his project, and if it isn't provided, a name is generated based on the uploaded file.

berdir’s picture

Exactly, this is different from anything we have so far with machine names or so.

Something I experimented a bit in Pathauto (with mixed success/user feedback but that's also because it still is required there) is moving the name/label field at the bottom of the form, so users provide file/URL (pattern in pathauto) *first*. In that case, we might be able to add some custom JS/ajx that fills the name field with the default suggestion. But we might want to make small improvements here instead of blocking everything on some idea that might be more complicated to implement?

wim leers’s picture

Something I experimented a bit in Pathauto (with mixed success/user feedback but that's also because it still is required there) is moving the name/label field at the bottom of the form, so users provide file/URL (pattern in pathauto) *first*.

That's exactly what I was thinking too :)

Something like:

File/URL field: [………………………]

Some other field: [………………………]

Auto-generated name: "llamas are awesome"    change

With that entire last bit in smaller font size, perhaps even faded to the background a bit, to make it visually clear it's A) not important, B) optional.

But we might want to make small improvements here instead of blocking everything on some idea that might be more complicated to implement?

+1

xjm’s picture

The two examples you give merely generate a default machine name that the user can choose to edit. The user still must type an actual name.

Right, but in this case, the default name comes from the uploaded file. So the machine name JS would follow that, rather than a user-entered text field. It's the same in that the user is providing a file which provides the default, and then they have an option to change it. But #14 sounds kind of like what I'm suggesting anyway.

With that entire last bit in smaller font size, perhaps even faded to the background a bit, to make it visually clear it's A) not important, B) optional.

This is what I mean about using the existing machine name pattern. The default is in a small font size. You can click the link to change it. Just like with the machine names.

xjm’s picture

The uploaded file is a required field, just as the Label for the field and block above is a required field. The machine name link does not appear until you fill in the required field. It's the same pattern.

wim leers’s picture

Hm, it's indeed more similar than I first thought. But the examples in #6 do not have '#required' => TRUE, which you've been stressing.

Also, I thought I remembered that the ability to interact with input[type=file] was limited in JavaScript. Turns out it's A) mostly about manipulating those inputs via JS, B) the necessary APIs have indeed only been added in the last 5-7 years: https://www.html5rocks.com/en/tutorials/file/dndfiles/ — which means I'm remembering limitations that aren't limitations anymore :)

It seems like we're pretty well aligned here! :)

marcoscano’s picture

So far we were relying on an empty name field to trigger the whole default-name-generation logic. This ensured any user input wasn't overriden by this logic. With the JS/AJAX approach from #6 and #14, this might be very hard to maintain. Are we going to disregard any existing user input and re-calculate the default suggested name after any change on the source field?

In other words:
- User uploads file final_report_version_2017JAN27_2_final.pdf.
- We auto-generate a media name called final_report_version_2017JAN27_2_final.pdf.
- The user doesn't like the default name, clicks on "edit" to change it, and then gives the media item the name: My Great Report, and saves the form.
- Later on the user edits this media item and uploads another file final_report_version_2017JAN27_2_final(2).pdf, after deleting the previous one.
-> How are we going to detect if this file upload should or should not generate (and fill) the media name?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

seanb’s picture

We have metadata mapping to allow changes in the source field to change existing data. In #2933012: Expose the filename as metadata for file based media sources, a patch was added to add a name metadata attribute for file based media sources.

For remote video's (that already have the name field hidden by default) we've added to the field map to fix this in #2983325: Use title -> name mapping for the "Remote video" type.

We could do the same for all other media types once #2933012: Expose the filename as metadata for file based media sources lands.

marcoscano’s picture

+1 to #21, as long as we don't change existing sites :) (which I don't think was the idea anyway, but just in case :P)

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new11.36 KB

So #2933012: Expose the filename as metadata for file based media sources has landed. Time to continue here.

This is a first patch to hide the media name by default for new sites and map the media source name/title to the media name base field. Let's see how many tests will need fixing.

Status: Needs review » Needs work

The last submitted patch, 24: 2882473-24.patch, failed testing. View results

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new12.98 KB
new1.62 KB

Fixed the tests. Not sure if we need more tests for the standard configuration? We have tests with media types showing the name field in the form, we also have the display test running against the standard profile showing the default name is no longer present for the image/file media types.

phenaproxima’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests
  1. +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
    @@ -68,9 +68,8 @@ public function testMediaDisplay() {
    -    $image_media_name = 'Fantastic image asset!';
    -    $page->fillField('name[0][value]', $image_media_name);
    -    $page->attachFileToField('files[field_media_image_0]', $this->root . '/core/modules/media/tests/fixtures/example_1.jpeg');
    

    We should add an assertion that the media name appears on the next page -- we need to be certain that the default name is actually used.

  2. +++ b/core/profiles/demo_umami/config/install/media.type.remote_video.yml
    @@ -13,4 +13,5 @@ source_configuration:
    -field_map: {  }
    +field_map:
    +  title: name
    

    Remote video works very differently under the hood than file-based media items, so ideally we would test this change for every media type that ships with core, individually. But we can do that in a follow-up; I think, for this issue, let's just make sure remote video is explicitly tested, since it's the outlier.

chr.fritsch’s picture

In Drupal\Tests\standard\Functional\StandardTest we are checking this:

      $published_checkbox = $assert_session->fieldExists('Published', $form)->getOuterHtml();
      if ($page->findField('Name')) {
        $name_field = $assert_session->fieldExists('Name', $form)->getOuterHtml();
        $this->assertTrue(strpos($form_html, $test_source_field) > strpos($form_html, $name_field));
      }
      $this->assertTrue(strpos($form_html, $vertical_tabs) > strpos($form_html, $test_source_field));

Now we don't have a name field anymore, so we could change this to $this->assertSession()->fieldNotExists()

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new32.31 KB
new19.75 KB

#27

1. Fixed.
2. Fixed. Added MediaStandardProfileTest to test the standard profile form configuration, creation, field mapping and display configuration. We could expand on that in the future. See if we can move/remove the media specific code in Drupal\Tests\standard\Functional\StandardTest and see what we want to do with Drupal\Tests\media\FunctionalJavascript\MediaDisplayTest since there is some overlap.

#28

Fixed for now, but as mentioned above we might want to move those tests to the media module.

marcoscano’s picture

While I think an ideal solution for some types (specifically the remote video) would be to pre-populate the field while still allowing it to be edited, I understand this is not in scope of this issue, and this is definitely a good step in the direction of making all media forms more usable.

So +1 from me.

phenaproxima’s picture

Status: Needs review » Needs work

Nice work!

  1. +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
    @@ -0,0 +1,338 @@
    +  protected $profile = 'standard';
    

    This needs @inheritdoc.

  2. +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
    @@ -0,0 +1,338 @@
    +    $this->assertSame(1, count($page->findAll('css', 'article.media--type-audio > div')));
    

    I think we can use $assert_session->elementCount() here. The selector also seems a little bit brittle; can we use a CSS class to target that inner element?

  3. +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
    @@ -0,0 +1,338 @@
    +    $assert_session->elementExists('css', 'article.media--type-audio .field--name-field-media-audio-file audio > source');
    +    $audio_source = $page->find('css', 'article.media--type-audio .field--name-field-media-audio-file audio > source');
    

    We can get the source attribute in one shot: $assert_session->elementExists()->getAttribute('src').

  4. +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
    @@ -0,0 +1,338 @@
    +    // Check if the default media name is updated as expected.
    +    $assert_session->elementTextContains('css', 'h1', $test_filename_updated);
    

    Why aren't we doing all the other assertions here too?

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new35.91 KB
new12.82 KB

#31

1. Fixed.
2. We want to assert there is only 1 child element, so more specific CSS might pass while it shouldn't. While thinking about it, testing for DIV elements only could also pass while it shouldn't. Changed it to * to match alle child elements.
3. Fixed, removed the $page->find() part.
4. Fixed, you are right, there is value in asserting all other elements updated as expected.

chr.fritsch’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Thanks @seanB.

I am removing the "Needs tests" tag, because we have plenty of new tests.

I think this is ready to go!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 32: 2882473-32.patch, failed testing. View results

chr.fritsch’s picture

Status: Needs work » Reviewed & tested by the community

Testbot hickup

chr.fritsch’s picture

Status: Reviewed & tested by the community » Needs work

Sorry, I have to reject this. The patch currently does not remove the name field from the media library form displays.

seanb’s picture

Assigned: Unassigned » seanb
Issue tags: +DrupalEurope

Working on this

seanb’s picture

Assigned: seanb » Unassigned
Status: Needs work » Needs review
StatusFileSize
new40.5 KB
new4.58 KB
new149.22 KB

Also removed the name from the media library form as mentioned in #36. When we do this all media types except 'image' will not have any fields a user needs to fill. I chose to add the media name as an item element when the name field is hidden, so the users see a confirmation the right file with the right name has been uploaded.

Static name element

Status: Needs review » Needs work

The last submitted patch, 38: 2882473-38.patch, failed testing. View results

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new40.63 KB
new1.88 KB

This should fix the tests.

sreher’s picture

I am reviewing this on DrupalEurope and I am mentored by @ericheydrich

sreher’s picture

Status: Needs review » Reviewed & tested by the community

What I did:
* enable media and media library module
* add media field to the content type

Upload media to the article
* image - alt field required
* pdf - no field

Upload a remote video to media library
* youtube - no field
* mp4 - no field

I checked:
* Medial Library in the backend
* Entity Reference Field

The name of the file will be used as the name of the media entity. The name is not editable.

It is possible to change the visibility of the name field. You can enable it in the media types manage form display settings. After that the name is editable.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 40: 2882473-40.patch, failed testing. View results

seanb’s picture

Status: Needs work » Needs review

Triggering the test again, not sure what happened.

phenaproxima’s picture

This patch looks great to me.

My only concern with MediaStandardProfileTest is that there are multiple test methods, and each one is installing Standard. That is going to be one hell of a slow test class.

Maybe we could refactor it so that we have one public test method (and thus Standard is only installed once), yet keep the code clean by making all the current test methods into private helpers which are called from the main test method. Thoughts?

chr.fritsch’s picture

StatusFileSize
new41 KB
new2.47 KB

Moved all the testcases into one method.

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

I think this is done, then!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

I've manually tested this. It makes uploading files to media much much nicer. Not having to provide a name when the file already has a name makes sense.

The added test coverage looks very complete. Nice work.

 ___________________________________________________________________________________________________________________
< Committed 4f658fa and pushed to 8.7.x. Thanks! >
 -------------------------------------------------------------------------------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
alexpott’s picture

  • alexpott committed 4f658fa on 8.7.x
    Issue #2882473 by seanB, chr.fritsch, marcoscano, pk188, xjm,...

Status: Fixed » Closed (fixed)

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

weynhamz’s picture

Sorry for buzzing on a closed issue. I am interested in the method in comment #4, is there a patch already?

Recently, on a client project, we solved this by making the form element non-required in a form alter. Then we added an entity builder callback that checked if the name was provided and triggered the auto logic if not. This ensured that the name was always there for the entity validation. Maybe core could use a similar approach?