I have enabled "Hide blank items" on the field level for my Field Collection field. When adding a new row, the table is misformed, as can be seen in the attachment.
I have traced this back to the fact that the field_collection_table
widget is changed to field_collection_embed
in the field_collection_table_field_widget_form()
function.
However, the function field_collection_field_attach_form()
(provided by field_collection) doesn't pick this up and checks if the original widget setting was field_collection_embed
. Because of that, a key operation on #max_delta
is not performed and an empty row is added.
As a temporary measure, I copied field_collection_field_attach_form()
and edited this a bit, but I think it should be fixed in a better way:
/**
* Implements hook_field_attach_form().
*
* Corrects #max_delta when we hide the blank field collection item.
*
* @see field_add_more_js()
* @see field_collection_field_widget_form()
*/
function field_collection_table_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
foreach (field_info_instances($entity_type, $form['#bundle']) as $field_name => $instance) {
$field = field_info_field($field_name);
if ($field['type'] == 'field_collection' && $field['settings']['hide_blank_items']
&& field_access('edit', $field, $entity_type) && $instance['widget']['type'] == 'field_collection_table') {
$element_langcode = $form[$field_name]['#language'];
if ($form[$field_name][$element_langcode]['#max_delta'] > 0) {
$form[$field_name][$element_langcode]['#max_delta']--;
}
}
}
}
Comment | File | Size | Author |
---|---|---|---|
#29 | field_collection_table-1815496-29-fix_malformed_table.patch | 1014 bytes | recrit |
#2 | field_collection_table-1815496-2-fix_malformed_table.patch | 1.19 KB | mstrelan |
hide-blank-collections.png | 18.45 KB | Jorrit |
Comments
Comment #1
alanpeart CreditAttribution: alanpeart commentedSame problem for me. Adding a new row caused the table to be malformed (rows cannot be dragged).
Unticking "Hide blank items" seems to solve the problem without creating any other problems in my scenario.
Comment #2
mstrelan CreditAttribution: mstrelan commentedThe code in the OP works for me. I've attached it as a patch for review. Having not looked in to it too deeply I'm unable to comment as to whether this is a good approach or not.
Comment #3
charlie-s CreditAttribution: charlie-s commented+1, this works for me.
I think the code is straightforward and good. My only question – why use
when $langcode is a function argument? Is this because there is the potential for the field_collection and the field_collection item to be a different language? This question is coming from someone that only ever used 'und', btw.
Comment #4
mstrelan CreditAttribution: mstrelan commentedProbably because that's what the field_collection module does. See field_collection_field_attach_form().
I too only ever have needed to use 'und', so can't comment on that further.
I wonder if it would be suitable to patch field_collection to remove this condition -
$instance['widget']['type'] == 'field_collection_embed'
. What other widgets are out there and how would they be affected?Comment #5
mstrelan CreditAttribution: mstrelan commentedLogged a ticket with Field Collection for recommended approach - #1863370: Fix #max_delta issue for all widgets
Comment #6
robcarrPatch at #2 worked for me too. Thanks @mstrelan
Nobody else seems to have any thoughts on your other issue #1863370: Fix #max_delta issue for all widgets... maybe close that one?
Comment #7
samerali CreditAttribution: samerali commentedI tried #2 at the latest dev version and it does not seem to work, is there any other solutions?
Comment #8
lazly CreditAttribution: lazly commentedHi!
Please commit the path from #2 to the -dev version immidiatly! I worked 2 hours with it, because its undone. Sorry about if im not a gentleman, but now im a little angry... :(
So the problem: If you want upgrade from Field collection 7.x-1.0-beta4 --> Field collection 7.x-1.0-beta5 (or dev) then your Field Collection Table module will be gone... :( I tried upgrade the Field Collection Table too (to -dev version) but its unfixed. You cant read about it on the original description upper, and you can path it ( thanks your work mstrelan! :) )
Lines from the log:
So please, commit that patch! Thats 3 months old...
Thanks
Comment #9
lazly CreditAttribution: lazly commenteduse the field_collection-beta5 with field_collection_table-dev with patch from #2. After dont forget flush the caches ;)
Comment #10
k.skarlatos CreditAttribution: k.skarlatos commentedIs there any plan to cut a new dev with this one included?
Comment #11
lunk rat CreditAttribution: lunk rat commentedPatch in #2 did the trick for me. Using field_collection_table 7.x-1.0-beta1 and field_collection 7.x-1.0-beta5.
Now my headers stay put when adding another. Thanks mstrelan!
Comment #12
astanley86 CreditAttribution: astanley86 commentedworked for me as well. I'm using the recommended versions of all modules.
Comment #13
NancyDruApplied cleanly.
Comment #14
pebosi CreditAttribution: pebosi commented#2 Works for me on latest dev.
Please commit.
Comment #15
mattsmith3 CreditAttribution: mattsmith3 commentedEdit: works for me. Please commit.
Comment #16
lazly CreditAttribution: lazly commentedPlease commit!
Comment #17
yogaf CreditAttribution: yogaf commentedYep, #2 is working.
Please commit!!
Comment #18
alexey.gorb CreditAttribution: alexey.gorb commentedHey, #2 works for me as well, great =)
Thanks
Please commit
Comment #19
sinasalek CreditAttribution: sinasalek commented#2 patch works very well, thanks @mstrelan
Comment #20
robcarrPatch at #2 applied cleanly to latest DEV version. Didn't address the problem for me: the table is corrupted after selecting 'Add another item' with 'Hide blank items' option selected (Drupal and browser caches all cleared).
However, the patch seems to be good news for many others, so it's obviously addressing one problem with Field Collection [& Table]... should leave at RTBC.
Comment #21
lunk rat CreditAttribution: lunk rat commentedHow can I help get #2 committed?
Comment #22
drupestre CreditAttribution: drupestre commentedHi,
Patch #2 worked for me too !
Tested on a fresh drupal728 installation.
Would you please commit it ?
Benoit
Comment #23
NancyDruGiven that the last commit was over two years ago, I suspect that someone needs to follow the process detailed in Dealing with unsupported (abandoned) projects.
Comment #24
jgsantos CreditAttribution: jgsantos commentedpatch #2 worked for me too
Comment #25
szt CreditAttribution: szt commented#2 works well! Commit, please!
Comment #26
szt CreditAttribution: szt commentedA new hope!
Thanks Kristoffer :)
Comment #27
kevineinarsson CreditAttribution: kevineinarsson commentedThis patch needs to be rerolled for the latest version.
Comment #28
szt CreditAttribution: szt commentedComment #29
recrit CreditAttribution: recrit commentedRolled for latest 7.x-1.x / 7.x-1.0-beta3. This is the same as field_collection only for the field_collection_table widget.
Comment #30
recrit CreditAttribution: recrit commentedComment #32
kevineinarsson CreditAttribution: kevineinarsson commented#29 has been patched to dev.
Comment #33
joelpittetHeads up @kevineinarsson The status will auto change to Closed (fixed) in 2 weeks. This helps people find recent commits.