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']--;
      }
    }
  }
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alanpeart’s picture

Same 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.

mstrelan’s picture

Status: Active » Needs review
FileSize
1.19 KB

The 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.

charlie-s’s picture

+1, this works for me.

I think the code is straightforward and good. My only question – why use

<?php
$element_langcode = $form[$field_name]['#language'];
?>

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.

mstrelan’s picture

Probably 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?

mstrelan’s picture

Logged a ticket with Field Collection for recommended approach - #1863370: Fix #max_delta issue for all widgets

robcarr’s picture

Title: Using "Hide blank items" leads to misformed tablesd » Using "Hide blank items" leads to misformed tables
Status: Needs review » Reviewed & tested by the community

Patch 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?

samerali’s picture

I tried #2 at the latest dev version and it does not seem to work, is there any other solutions?

lazly’s picture

Hi!

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:

Notice: Undefined index: #bundle field_collection_table_pre_render_field() függvényben (/var/www/sites/all/modules/field_collection_table/field_collection_table.module 219 sor).
Notice: Undefined index: #entity_type field_collection_table_pre_render_field() függvényben (/var/www/sites/all/modules/field_collection_table/field_collection_table.module 219 sor).

So please, commit that patch! Thats 3 months old...

Thanks

lazly’s picture

use the field_collection-beta5 with field_collection_table-dev with patch from #2. After dont forget flush the caches ;)

k.skarlatos’s picture

Is there any plan to cut a new dev with this one included?

lunk rat’s picture

Patch 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!

astanley86’s picture

worked for me as well. I'm using the recommended versions of all modules.

NancyDru’s picture

Applied cleanly.

pebosi’s picture

#2 Works for me on latest dev.

Please commit.

mattsmith3’s picture

Edit: works for me. Please commit.

lazly’s picture

Please commit!

yogaf’s picture

Yep, #2 is working.
Please commit!!

alexey.gorb’s picture

Hey, #2 works for me as well, great =)
Thanks
Please commit

sinasalek’s picture

#2 patch works very well, thanks @mstrelan

robcarr’s picture

Patch 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.

lunk rat’s picture

How can I help get #2 committed?

drupestre’s picture

Hi,

Patch #2 worked for me too !
Tested on a fresh drupal728 installation.

Would you please commit it ?

Benoit

NancyDru’s picture

Given 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.

jgsantos’s picture

patch #2 worked for me too

szt’s picture

#2 works well! Commit, please!

szt’s picture

kevineinarsson’s picture

This patch needs to be rerolled for the latest version.

szt’s picture

Status: Reviewed & tested by the community » Needs work
recrit’s picture

Rolled 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.

recrit’s picture

Status: Needs work » Needs review

  • websystem committed 0d5de5d on 7.x-1.x authored by recrit
    Issue #1815496 by mstrelan, recrit: Using "Hide blank items" leads to...
kevineinarsson’s picture

Status: Needs review » Closed (fixed)

#29 has been patched to dev.

joelpittet’s picture

Status: Closed (fixed) » Fixed

Heads up @kevineinarsson The status will auto change to Closed (fixed) in 2 weeks. This helps people find recent commits.

Status: Fixed » Closed (fixed)

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