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
Comment #2
phenaproximaI'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).
Comment #3
phenaproximaHere's a failing test which we can use to build the rest of the patch.
Comment #5
jfrederick commentedWould 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 offield_media_image.Comment #6
Anonymous (not verified) commentedI 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...!)
Comment #7
chr.fritschLets trigger the testbot
Comment #9
chr.fritschI 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() ?
Comment #11
chr.fritschFixing the last failing test
Comment #12
phenaproximaI'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...
Comment #13
phenaproximaIt 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.
Comment #14
chr.fritschTurns out that setting the field name in MediaTypeForm is much easier.
Comment #15
phenaproximaLooks great. I think this is nearly RTBC, in fact.
Let's use
$this->getPluginDefinition()['label']here.Let's chain these calls.
Comment #16
chr.fritschFixed #15
Comment #17
phenaproximaHonor is satisfied, and so am I. Thanks, @chr.fritsch!
Comment #18
alexpottWhy should we restrict access to the label on edit? Also if this is important it is not tested.
->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.
Camel case is not used for local variables.
Comment #19
chr.fritschThanks 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.
Comment #20
phenaproximaBoy, 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.
This description is a bit redundant. Should we remove it and maybe change #title to "Source field label"?
Comment #22
alexpottI 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:
Media/mediaso not using the user created field prefix of
field_Comment #24
mikelutzI 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.