Problem/Motivation

Thanks for the excellent module! My content authors are so happy.

It would be nice for the edit button to be available even if the parent entity (e.g. node) is not yet saved.

Currently the edit button is not added to media if the parent entity has not yet been saved. This logic appears in media_library_edit_field_widget_form_alter:

$entity = $context['items']->getEntity();
if (!$entity || !$entity->id()) {
 // Bail out if parent entity is not yet saved.
 return;
}
$edit_url_query_params = [
  'media_library_edit' => 'ajax',
  'media_library_edit_field_name' => $context['items']->getFieldDefinition()->getName(),
  'media_library_edit_entity_type' => $entity->getEntityTypeId(),
  'media_library_edit_entity_id' => $entity->id(),
];

However, after #3242982: Follow-up: On fields with multiple media items, other items disappear after saving, these query parameters are no longer needed or used. And that's the only place the $entity variable is used. Therefore we should be able to allow the edit button even if the parent entity is not yet saved.

Steps to reproduce

  1. Click "Add new content" and pick a content type that has a media field using the media library widget.
  2. Before saving the node, add media to the media field
  3. The edit pencil is not available
  4. Save the node
  5. Edit the node
  6. The edit pencil is available.

I want the edit to be available in step 3. The node is not yet saved, but we should still be able to edit the media.

Proposed resolution

Remove the dead code.
Profit.

Remaining tasks

Patch to follow quickly.
Tell me if I'm missing something! I'm new to the module. :)

User interface changes

Edit pencil available even if parent entity is not saved.

API changes

node

Data model changes

none

Comments

danflanagan8 created an issue. See original summary.

danflanagan8’s picture

Assigned: danflanagan8 » Unassigned
Status: Active » Needs review
StatusFileSize
new1.42 KB

Here's the patch that (hopefully) makes the magic happen. It appears to work for me locally, but like I said, I'm new to the module. There may be something I'm missing. :)

andybroomfield’s picture

Thanks for the patch @danflanagan8 I've installed this and verified that I can now see the edit media button, and edit media before the node has been saved. When I do save the node the media is present as expected.

bisonbleu’s picture

Status: Needs review » Reviewed & tested by the community

Just tested this on simplytest.me with 9.4 alpha 1. Patch works as advertised. Thanks @danflanagan8!

  • ahebrank committed 895b0bc on 8.x-2.x authored by danflanagan8
    Issue #3278706 by danflanagan8: Add edit button to media even if parent...
ahebrank’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for the cleanup. I'll do a patch release for this shortly.

Status: Fixed » Closed (fixed)

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