Opened 11 years ago
Last modified 4 years ago
#28053 accepted defect (bug)
Fields with same name added to new media modal using 'attachment_fields_to_edit' do not pass as an array when saving.
Reported by: | husobj | Owned by: | joedolson |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 3.5 |
Component: | Media | Keywords: | has-patch |
Focuses: | javascript, administration | Cc: |
Description
If you use the 'attachment_fields_to_edit' to add a set of checkboxes to the media modal attachment details (to implement a better UI for taxonomies for example) the save method of media.view.AttachmentCompat doesn't cater for fields with the same name to be passed as an array, it just passes the last field of that name that is found.
When getting the fields to save, if multiple fields with the same name are found they should be passed as an array.
I have attached a visual of the custom UI with checkboxes and a patch for testing.
Attachments (7)
Change History (33)
#2
@
11 years ago
Updated patch checks for fields intended to be arrays be checking their name ends in "[]" e.g. "myfield[]"
These are then concatenated into a comma separated value with field name "myfield" (without the square brackets) which is the same format in which taxonomy terms are handles when currently associating taxonomies with attachments.
Works for the development I'm doing building a better taxonomy UI for the media modal but obviously wouldn't handle field values that contain commas (which should not be an issue for terms).
#3
@
11 years ago
- Keywords reporter-feedback added
- Version changed from 3.9 to 3.5
Can you drop in the boilerplate for creating the nested checkboxes you have there?
#4
@
10 years ago
Download this plugin which sets up attachment tag and category taxonomies.
https://github.com/benhuson/attachment-taxonomy-support/tree/dev
In Admin > Media set up some tags and categories.
If you edit a media item (from the list view, not the modal popup in 4.0) you'll get the default taxonomy UI meta boxes which work fine.
If you get into the media modal either via the WP 4.0 Grid view or clicking on "Add Media" when editing a post, you'll see the modal now shows a field for tags and category checkboxes.
The tags field is named in the format "attachments[{attachment_id}][attachment_tag]" and this autosaves fine when the field is updated.
With the checkboxes, I was expecting to be able to name them all in the format "attachments[{attachment_id}][attachment_category][]" and the "[]" on the end would signify the values get submitted as an array and saved (like the checkboxes in the standard categories meta box).
That currently isn't working and is the use case I am trying to fix.
This ticket was mentioned in Slack in #core by lindsaymac. View the logs.
9 years ago
#7
@
9 years ago
This is my boilerplate code used that will replicate the problem with multiple field values being saved "one at a time" versus all in one shot.
@
9 years ago
media-views.js and attachment-compat.js patch for multiple value input as javascript array object patch
@
9 years ago
[fixed tabs] media-views.js and attachment-compat.js patch for multiple value input as javascript array object patch
@
9 years ago
[formatting fix] media-views.js and attachment-compat.js patch for multiple value input as javascript array object patch
#10
@
9 years ago
- Keywords has-patch added
Hi,
So here goes my core patch done on WordCamp Porto to help fix this problem.
Attachment https://core.trac.wordpress.org/attachment/ticket/28053/media-models.patch added
Please do test it, feel free to contribute if needed.
All the best,
David
#13
in reply to:
↑ 12
@
8 years ago
Replying to drosendo:
Hi,
Any feedback on this? is my patch viable?
Thanks,
David
Can you explain how your patch is more efficiently coded than the one already provided in this ticket? The previously submitted one looks for the default scenario for checkbox naming where if you are provided a list of checkboxes as options for a single "question", you use brackets [] in your naming convention. Because this is default behavior, we just look for that to know if we are looking at a multiple checkbox select option or not.
#14
follow-up:
↓ 15
@
8 years ago
Hi @LindsayBSC ,
I don't fully understand your question, but how is my better? I'm really not discussing witch one is better, I believe both are viable solutions, but since none have been approved over the past 2 years since reported. I just rather use my code, I believe it to be more "readable/clean" and I do think that it covers all scenarios. Correct me if I'm wrong.
Thanks,
David
#15
in reply to:
↑ 14
;
follow-up:
↓ 16
@
8 years ago
Replying to drosendo:
Hi @LindsayBSC ,
I don't fully understand your question, but how is my better? I'm really not discussing witch one is better, I believe both are viable solutions, but since none have been approved over the past 2 years since reported. I just rather use my code, I believe it to be more "readable/clean" and I do think that it covers all scenarios. Correct me if I'm wrong.
Thanks,
David
I was asking why you felt the patch needed rewriting - which, even though you said you didn't want to discuss why, you actually did explain.
So you feel your patch is more readable than the provided previous patch? I am asking for my own reference to understand if/when/how my code could be improved.
#16
in reply to:
↑ 15
;
follow-up:
↓ 17
@
8 years ago
Replying to LindsayBSC:
I was asking why you felt the patch needed rewriting - which, even though you said you didn't want to discuss why, you actually did explain.
So you feel your patch is more readable than the provided previous patch? I am asking for my own reference to understand if/when/how my code could be improved.
Hi,
Actually I now recall why I did my patch, I believe your patch doesn't include multiple select fields, and only handles checkboxes.
Am I incorrect?
Thanks,
David
#17
in reply to:
↑ 16
@
8 years ago
It works for any data sent as an array so it would completely depends on how you collect your form values. If you are using select boxes then are you using a selectbox javascript plugin for this?
Replying to drosendo:
Replying to LindsayBSC:
I was asking why you felt the patch needed rewriting - which, even though you said you didn't want to discuss why, you actually did explain.
So you feel your patch is more readable than the provided previous patch? I am asking for my own reference to understand if/when/how my code could be improved.
Hi,
Actually I now recall why I did my patch, I believe your patch doesn't include multiple select fields, and only handles checkboxes.
Am I incorrect?
Thanks,
David
This ticket was mentioned in Slack in #core by user-267. View the logs.
8 years ago
#19
@
8 years ago
- Keywords reporter-feedback removed
- Owner set to joemcgill
- Status changed from new to reviewing
Visual of media modal attachment details with checkboxes UI