Problem/Motivation

When creating a new media type, you have the option of allowing the source plugin to create the source field for you. When you do that, the source field is given the same label as the source plugin itself. So, for example, if the source plugin is "Remote video", the field label will also be "Remote video". You can change this after the fact, of course, but it's a little inconvenient, and kind of annoying and bizarre from a usability standpoint.

Proposed resolution

Change the media type form so that you can set the field label when creating a media type that will build its own source field.

Remaining tasks

Patch. Tests. Usability review. Commit.

User interface changes

The media type form will expose a new field.

API changes

None.

Data model changes

None.

Comments

phenaproxima created an issue. See original summary.

phenaproxima’s picture

I'm removing the parent issue; this change would benefit all media types and is not specific to oEmbed (although this idea came up during usability review of that patch).

phenaproxima’s picture

Status: Active » Needs review
StatusFileSize
new4.86 KB

Here's a failing test which we can use to build the rest of the patch.

Status: Needs review » Needs work

The last submitted patch, 3: 2965988-3.patch, failed testing. View results

jfrederick’s picture

Would it also make sense to be able to adjust the machine name of the auto-created field?

It is often helpful to have a similar Label as machine name. For example if I am making the label of the field be "Flickr Source", I may want the field machine name to be field_flickr_source, instead of field_media_image.

Anonymous’s picture

Issue tags: +DCScot18
StatusFileSize
new7.73 KB
new4.98 KB

I had a wee go at advancing phenaproxima's patch (#3) during the DrupalCamp Scotland 2018 code sprint. (Thanks to chr.fritsch for the suggestion.)

Attached is a new patch that appears to set the field label correctly, and both of the failing tests now pass. It'll need further work from someone who understands this part of Drupal more than I do, but hopefully it's going in the right direction. (This was my first real play with D8, so please be gentle...!)

chr.fritsch’s picture

Status: Needs work » Needs review

Lets trigger the testbot

Status: Needs review » Needs work

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

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new9.84 KB
new7.45 KB

I tried to fix all the tests.

I am not 100% sure about the implementation. Do we really need to save the label in the type configuration? I don't see a reason why we would need the label after the form submit. So maybe we should pass the label or the form_state to ::createSourceField() ?

Status: Needs review » Needs work

The last submitted patch, 9: 2965988-9.patch, failed testing. View results

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new9.47 KB
new824 bytes

Fixing the last failing test

phenaproxima’s picture

I'm not sure I agree with the idea of making the source field label part of the plugin configuration. It is only ever useful when creating a new media type; once that's done, the plugin doesn't need to know the source field label. Ideally, we would encapsulate this logic in MediaTypeForm, where it belongs...

phenaproxima’s picture

Status: Needs review » Needs work

It looks like @chr.fritsch and I are in agreement that we'd prefer to keep the source field label logic outside of the plugin configuration. So I'm kicking this back for that.

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new6.36 KB
new5.32 KB

Turns out that setting the field name in MediaTypeForm is much easier.

phenaproxima’s picture

Looks great. I think this is nearly RTBC, in fact.

  1. +++ b/core/modules/media/src/MediaSourceBase.php
    @@ -181,6 +181,13 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
    +      '#default_value' => $this->pluginDefinition['label'],
    

    Let's use $this->getPluginDefinition()['label'] here.

  2. +++ b/core/modules/media/src/MediaTypeForm.php
    @@ -342,6 +342,7 @@ public function save(array $form, FormStateInterface $form_state) {
           $source_field = $source->createSourceField($media_type);
    +      $source_field->set('label', $form_state->getValue(['source_configuration', 'source_field_label']), $source->getPluginDefinition()['label']);
    

    Let's chain these calls.

chr.fritsch’s picture

StatusFileSize
new6.45 KB
new1.67 KB

Fixed #15

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Honor is satisfied, and so am I. Thanks, @chr.fritsch!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/media/src/MediaSourceBase.php
    @@ -193,6 +200,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
    +      $form['source_field_label']['#access'] = FALSE;
    

    Why should we restrict access to the label on edit? Also if this is important it is not tested.

  2. +++ b/core/modules/media/src/MediaTypeForm.php
    @@ -341,7 +341,8 @@ public function save(array $form, FormStateInterface $form_state) {
    +        ->set('label', $form_state->getValue(['source_configuration', 'source_field_label']), $source->getPluginDefinition()['label']);
    

    ->set() only takes tow arguments. $form_state->getValue() can take two and that is probably what was meant to happen here but also I'm not sure it is needed because the field value is defaulted to this already.

  3. +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php
    @@ -21,6 +21,7 @@ public function testMediaTypeCreationFormWithDefaultField() {
    +    $sourceFieldLabel = 'Wambooli';
    

    Camel case is not used for local variables.

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new6.54 KB
new4.27 KB

Thanks for reviewing @alexpott

1. The new form field is only for setting the initial label. It makes no sense to have the form field after the creation. If you want to change the label later, you can use the normal field config form. I added a test.

2. Nice catch 👍Fixed it and I still think it's useful. If the form value is empty for some reason, we still have a valid value and not an empty label.

3. Fixed.

phenaproxima’s picture

Boy, that was embarrassing. That's what I get for RTBCing things before I've fully woken up. I've had a coffee and re-read the patch, and I think it now actually looks good.

+++ b/core/modules/media/src/MediaSourceBase.php
@@ -181,6 +181,13 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
+      '#description' => $this->t('Enter the label of the source field.'),

This description is a bit redundant. Should we remove it and maybe change #title to "Source field label"?

Status: Needs review » Needs work

The last submitted patch, 19: 2965988-19.patch, failed testing. View results

alexpott’s picture

I think we need a UX review here. Things like #2557299: Any AJAX call disregards machine name verification when AJAX is used and leads to a fatal error do not make reviewing the UX of the media type creation process simple but I think we should think about the default names of the media field more carefully because:

  • The new field appears on the media type creation form - which is the same as the edit form - but it is not present on the edit form. That's because it is only used during creation of the field added to the media type but that's confusing because a user doesn't learn where to change this. If we want to having this there then in my opinion we should also have it on the edit form and make it work by updating the field on media type save.
  • BUT the default field names are dubious because if I create a new media type with a type of image I can make it store the data in an existing field called field_media_video_file?!!??! I wish that the initial label and field name was Media/media
  • so not using the user created field prefix of field_

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.

mikelutz’s picture

I found this issue while running into frustrations around the machine names created during media type creation. This is the first site I've attempted to build from scratch using media reference fields in place of image fields that I may have used in the past.

Because image constraints (primarily minimum width and height, but fundamentally everything on the image field instance settings form) vary per (what would have been an) image field, I find it necessary to create a media type for each image field I'm replacing, which quickly becomes a dozen or so image media types. I would like to use some sort of sensible machine name for the source field for each type, but if I create one, I'm stuck with field_media_image_7, field_media_image_12, ect. Since I want them to be unique to this new media type, they aren't existing.

The only workaround I'm finding is to have a dummy media type in the beginning that I can use to pre-create the fields with useful machine names (field_header_image, field_profile_icon_image, etc), which then allows me to create new media type using the 'existing fields'. Once Development is done, I can delete the originally dummy media type.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.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.

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.

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.

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.