Currently, the field formatter for Field Collections always returns something. When a collection is empty, the formatters either return only the "add" link, when the user is allowed to add a item) or, worse, an empty element with an #attached
property.
From the formatter consumer perspective, there is always something to display for a field collection. So when the field's configuration display its label, it will be displayed above or inline of an invisible content.
The behavior for most formatters is to display nothing (no label) for empty fields. Field Collection should cleanly return nothing for empty fields when the user cannot add an item and/or provide a setting to force hiding empty collections.
Comments
Comment #1
pbuyle CreditAttribution: pbuyle commentedThe attached patch provides the second option.
Comment #2
BenK CreditAttribution: BenK commentedSubscribing... this would be a nice feature.
Comment #3
RobW CreditAttribution: RobW commentedGreat usability/conformity catch, testing the patch now.
Comment #4
sachbearbeiter CreditAttribution: sachbearbeiter commentedsub
Comment #5
tim.plunkettOne small nitpick. Otherwise, I think this is reasonable.
Checkboxes shouldn't end in full stops
Comment #6
amandine_m CreditAttribution: amandine_m commentedSuscribe
Comment #7
rp7 CreditAttribution: rp7 commentedsubscribe
Comment #8
pbuyle CreditAttribution: pbuyle commentedHere is a re-roll of the patch.
Comment #9
marvix CreditAttribution: marvix commenteddid this worked for any, for me the label still showing when there is no field value!!
Comment #10
pbuyle CreditAttribution: pbuyle commented@marvix When a new node with a Field collection field is displayed, the
$items
argument passed tofield_collection_field_formatter_view()
contains a empty element instead of being empty. Once the node is edited, the$items
argument is an empty array. Tha attached patch now support both cases.Comment #11
marvix CreditAttribution: marvix commenteddid not worked !
Comment #12
rootical CreditAttribution: rootical commentedThis one doesn`t work.
Comment #13
dgorton CreditAttribution: dgorton commentedWorks for me - but it took forever to find the checkbox you need to check. Just in case this helps others - the final step after applying the patch is to go to the display settings page on your content type:
admin/structure/types/manage/YOUR_CONTENT_TYPE/display
Then, you need to click on the cog on the right-hand side next to the field collection in question. That will open a dialogue with a checkbox: "Hide empty collection - If enabled, nothing will be displayed for an empty collection (not even the add link)."
Works great once the box is checked!
Comment #14
tim.plunkettMissing a k
$items is never null, it will at least be array(), so you can remove this and just use the foreach
non-empty, and end with a full stop
Why are you bothering with a foreach when you check $items[0] directly?
NULL is redundant, just return;
I'm not sure this looks right at all. The first thing field_collection_field_formatter_view() does is call field_collection_field_get_entity() on the $item, which this patch completely bypasses.
Comment #15
bsztreha CreditAttribution: bsztreha commentedSubscribe
Comment #16
zany CreditAttribution: zany commentedsubscribe
Comment #17
rootical CreditAttribution: rootical commenteddo not subscribe but use "following" button!
Comment #18
Cyclodex CreditAttribution: Cyclodex commentedThe patch works but you are right, there are some unclear and wrong lines.
I am surprised no one fixed these yet.
If I have some time I will try to provide a corrected patch...
I like this feature!
Comment #19
zany CreditAttribution: zany commentedI added a check for user_is_logged_in() before hiding an empty collection. I want to hide the heading on an empty block, but I want logged in user to be able to use the add/remove-links. Maybe this could be rolled in as an option. I guess the check should be based on permission though.
Comment #20
idflood CreditAttribution: idflood commentedHere is a new patch based on #10 with the proposed changes in #14. I've completely removed the foreach, the condition looks like this now:
It's working perfectly on my setup, so thanks @mongolito404 and @tim.plunkett : )
Comment #21
hbalagtas CreditAttribution: hbalagtas commentedHi, I've tried this patch and while it does remove the empty field on a field collection, but if that field collection has another field collection attached to it then the empty collection is there and is saved again.
Comment #22
tim.plunkettComment #23
larowlanWorks a treat. Great work!
Comment #24
andypostThis option make sense only when Add link displayed, Fields Only formater works fine without patch.
The subject mostly caused by $element['#attached'] & suffix/prefix added by field_collection_field_formatter_links()
Also field_collection_field_formatter_settings_summary() should display something about this setting - "Hide empty" for example
Comment #25
jimmynash CreditAttribution: jimmynash commentedThe patch in #20 worked for me when used in conjunction with the patch in http://drupal.org/node/1339868#comment-6000492
Comment #26
klucid CreditAttribution: klucid commentedI have a quick and easy work around for this issue if the patch didn't work for you.
If you make the field within the field collection required, it will not display. If you don't want the field to be required, make sure the field collection itself is not required under you manage fields settings for that content type.
FYI, I think you still need to delete all of the old empty fields after setting it to required.
Hope that helps!
Comment #27
klucid CreditAttribution: klucid commentedEdit: It actually didn't work. I tested a few times, but now it's requiring the field. Sorry to jump the gun.
Comment #28
dman CreditAttribution: dman commented#20 works fine, but as zany noted, it also removes the [Add] links if any.
I want :
non-editors to not see any title with no content
editors to see the empty zone with an add link.
The access check in field_collection_field_formatter_links() looks like a lot of work, so I'm not keen to copy it into field_formatter view just to support this logic.
#1157794: Move markup to template files and improve theming in new 2.x branch would mean we could take care of this display logic ourselves.
still needs work. Tricky.
Comment #29
fagoI agree this option is unnecessary. If the add-link is disabled and there are no collections, this should be what happens anyway. If not, that's the bug that needs to be fixed.
Comment #30
rvilarHere is a patch that works like fago has commentted. I've modified hook_field_formatter_view() function to not attach any information to $elements array if there are any items to display and we haven't access to add new items.
Comment #31
dman CreditAttribution: dman commentedThis *sounds* better, but I can't visually see what's happening by looking at the patch. ( I guess I should build a test)
So it only adds the 'add' link if the user has add rights? Cool.
Do I get a region title too for where the 'add' happens? It was unstated in #28 - so maybe undefined.
if so it's closer to the desired UI.
Comment #32
andypostThis patch make more sense.
But more saner way is to remove #suffix/prefix mess if @fago is not oposit
trailing white-space
same
Comment #33
30equals CreditAttribution: 30equals commentedRemoved trailing whitespaces from patch as git apply was failing with #30.
Comment #34
30equals CreditAttribution: 30equals commentedFyi, i applied the patch, and i really don't see any improvements with my empty field collections in a content type which already had an embedded field collection..? They're still there.
Comment #35
gillarf CreditAttribution: gillarf commentedSame here - no change after applying this patch
Comment #36
RobW CreditAttribution: RobW commentedI'm working on a patch that fixes this, among other things, in #1157794: Move markup to template files and improve theming in new 2.x branch. The behavior I have is:
Does that sound right to everyone here?
Also, marked #1621554: Fields rendering empty <div> and #1570836: Only display the wrapper if the field has items and/or links as duplicates of this issue.
Comment #37
dman CreditAttribution: dman commentedMakes sense!
I know the empty+edit link is a tricky special case, but is certainly the best case needed for a good UI.
Comment #38
gillarf CreditAttribution: gillarf commented@RobW that's exactly right as far as I can see
And a great idea to make a template, especially if you can override the template for each field collection separately. (field-collection--my-field-collection.tpl.php)
Comment #39
RobW CreditAttribution: RobW commentedTemplate inheritance proposal is here: http://drupal.org/node/1157794#comment-6270780. Field--field-collection is still output by field module, but there are some issues with bundle names and field names being the same that I'm going to try and fix (see the issue mentioned in the next comment after the one I linked to). If you have any theme suggestions (BAM! Drupal pun) or ideas let me know in the other thread.
Comment #40
RobW CreditAttribution: RobW commentedThe patch in #1157794: Move markup to template files and improve theming in new 2.x branch is working well, and takes the saner approach of eliminating the suffix and prefix as mentioned above. Is anyone opposed to marking this as a duplicate and moving further work over there?
Comment #41
barraponto CreditAttribution: barraponto commentedSince #1157794: Move markup to template files and improve theming in new 2.x branch is meant for Field Collection 7.x-2.x I decided to go forward with the approach in this patch since it fixes a bug right now.
Rerolled against latest EntityAPI-dev, fixed an undefined index warning.
Comment #42
Macronomicus CreditAttribution: Macronomicus commentedYes! Patch in #41 did the trick...
This issue is compounded if you've the collection set to display within a fieldgroup tab, you get an empty tab where you would want no tab, if there is no content.
Comment #43
sachbearbeiter CreditAttribution: sachbearbeiter commentedin my use case #41 worked - thanks ...
Comment #44
AaronBauman#41 worked for me.
Thank you for continuing to pursue a fix on the 1.x branch.
Comment #45
joep.hendrix CreditAttribution: joep.hendrix commented#41 works for me, please commit.
Thanks for the good work!
Comment #46
azinck CreditAttribution: azinck commented#41 works for me. RTBC
Comment #47
sclsweb CreditAttribution: sclsweb commented#41 does not work for me. (Nothing broken but nothing fixed, either -- no apparent change.)
I patched using Field Collection 7.x-1.0-beta5. This is my first time using a patch, so apologies if I should have been using dev(?). Let me know if there is something additional I should do to get the patch working.
My use case is a content type with 3 non-required field collections attached. Each field collection can have unlimited items added.
Desired behavior:
If patching does not work out, I could add a separate content type for field collections 1 & 2, but I'm hoping not to have to re-architect content types for something that will probably be fixed in upcoming releases(?). Any advice would be appreciated.
Comment #48
barraponto CreditAttribution: barraponto commentedcan you please try patching a dev release?
Comment #49
Anonymous (not verified) CreditAttribution: Anonymous commentedThis is not working for me either. Dev version + #41.
I create a node w/ a field collection and a blank item is inserted for me.
Edit: Sorry, my issue is related to a boolean value on the field collection. If it's not checked, the 'Off' value still loads into the field collection, rendering the item, regardless if the off value is 0. I'm going to look into fixing it through my node fields.
Comment #50
udog CreditAttribution: udog commentedI have the same issue: Boolean fields are rendered with their default values even though the field collection to which they belong has not been created.
Comment #51
vinmassaro CreditAttribution: vinmassaro commentedPatch applies cleanly to against 7.x-1.x-dev but the empty field collection is still there. I resorted to a prerender approach in the meantime:
Comment #52
jamesgrobertson CreditAttribution: jamesgrobertson commentedI applied the patch in #41 to the 7.x-1.x-dev branch, and it seems to have solved my problem. I also implemented the hook_field_collection_is_empty_alter() function from #1614578: Allow other modules to determine if collection item is empty, avoid saving empty item or rendering empty collection in a custom module for good measure.
Comment #53
massanishi CreditAttribution: massanishi commented#41 worked hiding on mine. Thanks!
Comment #54
barraponto CreditAttribution: barraponto commented#41: hide-empty-field-collections-1276258-33.patch queued for re-testing.
Comment #55
dalin#41 works great for me. It seems that some people are having issues with Boolean fields still however that would be a different bug entirely (this will happen if the Boolean field is outside of the field collection as well).
There's one report of things not working:
https://drupal.org/node/1276258#comment-7276424
but there's no further details.
Given that this patch has been around for a year I think it's best to RTBC this. Any edge case configurations could be dealt with in follow-up issues.
Comment #56
Jonah Fenn CreditAttribution: Jonah Fenn commentedPatch from #41 worked for us. It would be excellent if we could get this patch rolled in to a release.
Thanks for all of your efforts!
Comment #57
zilla CreditAttribution: zilla commentedditto on fenns above - i stumbled onto this thread while developing a site, there's one field collection item added to a content type and i just noticed this very week when logged in as a test user that "field collection" still shows a label for an empty field collection of any kind, even where user has no ability to add.
Comment #58
Jonah Fenn CreditAttribution: Jonah Fenn commentedLet me know if there are any additional things we at Oregon State can do to help push this out. If you need more testers I'll see if I can tap deeper into our community here to get some help.
Comment #59
mavimo CreditAttribution: mavimo commentedSame patch of #41 but applied to a field_collection with patch from Entity translation support #1344672: Field Collection: Field translation (entity_translation) support. #53.
Drush make:
Comment #61
t_en CreditAttribution: t_en commentedPatch in #41 still applies to 7.x-1x-dev:
The patch works correctly on our Drupal 7.23 (on php 5.3.10). The patch also applies to the stable 7.x-1.0-beta5, and works.
Comment #62
steinmb CreditAttribution: steinmb commentedDrupal 7.23 clean install
PHP 5.4.15
Field_collection HEAD, clean install.
Tested with a multivalue field colletion containing a multivalue text field.
With patch applied
Does it thing, though do not feel that
<div class="description field-collection-description"></div>
belong there, does not make any sense to me.
With patch applied but field label enabled
Not 100% sure about the end result here. In my mind should field label not be printed if the field is empty. I do not however think this is a reason to keep this from RTBC. The real fix is worked on in #1157794: Move markup to template files and improve theming in new 2.x branch but they introduce to many changes to how this is themed that it will not happen in 7.x-1 but in 7.x-2.
btw: If any of the maintainers are reading this, could you pls open a 7.x-2.x branch?
Comment #63
fagoHow is that related?
Else #41 looks good.
Lets' discuss in the respective issue.
Comment #64
barraponto CreditAttribution: barraponto commentedRe-rolled patch without the unrelated fix (left for another issue).
I guess under some circunstances
$item['revision_id']
wasn't set and it screamed some issues back at me. Of course, it might be related to the stack at the time (a year ago). In any case, I agree it's unrelated to the current issue.Comment #65
sydneyshan CreditAttribution: sydneyshan commentedJust tested #64 on a build I'm doing here and it had the desired outcome. Great job, guys!
Comment #66
nadavoid CreditAttribution: nadavoid commentedTested and confirmed working as expected.
Comment #67
joep.hendrix CreditAttribution: joep.hendrix commented#64 works, thanks!
I had to manually apply the changes though against the 2013-Oct-10 version.
Comment #68
knalstaaf CreditAttribution: knalstaaf commentedCommit this please. It's an ugly issue.
Comment #69
knalstaaf CreditAttribution: knalstaaf commentedI would rather call it a bug report (considering Drupal standards) than a feature request, but change it back if deemed appropriate.
Hiding a field (collection) when empty shouldn't be something that requires patching (let stand manually, as described in #67). It's expected by default.
Comment #70
bojanz CreditAttribution: bojanz commented#64 works fine on my build as well.
Comment #71
extect CreditAttribution: extect commentedWorking good. Please commit.
Comment #72
FiNeX CreditAttribution: FiNeX commentedI also confirm patch #64.
Comment #73
betz CreditAttribution: betz commentedsame here, works smoothly.
Comment #74
aasarava CreditAttribution: aasarava commented+1 on #64
Comment #75
jdanthinne CreditAttribution: jdanthinne commentedPlease commit #64…
Comment #76
fagoThanks, committed.
Comment #77
draxxdk CreditAttribution: draxxdk commented+1 on #64
Thanks for the patch
Comment #79
tuthanh CreditAttribution: tuthanh commented#64 patch works for me.
Thanks alot :)
Comment #80
caw67 CreditAttribution: caw67 commentedpatchc #64 works!
why dont you commit it?
Comment #81
gateway69 CreditAttribution: gateway69 commentedSorry to kick this thread also, but it would be nice to have this committed :)
Comment #82
dman CreditAttribution: dman commentedGuys, it is committed, March 14, 2014
https://www.drupal.org/commitlog/commit/16250/c05116101a1bb5061047e96b44...
Unfortunately the last stable release on d.o was March 13. So that means that it's only in 7.x-1.x-dev still, not the 'recommended' release.
What you want to ask is "Aren't we due for a new stable release please?"
Comment #83
gateway69 CreditAttribution: gateway69 commenteddman! thanks for pointing that out, but yea pushing some fixed into a stable would be preferred.. thoughts?
Comment #84
BillyTom CreditAttribution: BillyTom commentedI am using Field Collection 7.x-1.0-beta8 and now no label is beeing shown when I view a node with an empty field collection as an anomymous user. However if I am logged in there is still the label and the "add new" link. I know this is going to confuse the editors.
The patch #64 is supposed to hide the label but not the link, is that correct?
Comment #85
nadavoid CreditAttribution: nadavoid commentedBillyTom: To remove the "add new" link, there is a setting when configuring the field. Once the link is gone and there is nothing left to display as part of the field, the label should disappear again too.
Comment #86
dman CreditAttribution: dman commentedOK yay!
With field_collection 7.x-1.0-beta8 Nov 5 out, this issue is actually released!
Comment #87
BillyTom CreditAttribution: BillyTom commented@nadavoid: I totally forgot to check the display settings, thanks!
Comment #88
gagarine CreditAttribution: gagarine commentedI opened a follow up issue concerning boolean fields #2402511: Hide empty field collections with empty boolean fields
Comment #89
knalstaaf CreditAttribution: knalstaaf commentedI'm afraid this is still an issue with the textarea, created a follow-up as well: #2421501: Label of textarea shows despite textarea being empty
Comment #90
marcmueller CreditAttribution: marcmueller as a volunteer commentedMy temporary solution here:Nevermind.
Comment #91
AndraeRay CreditAttribution: AndraeRay as a volunteer commentedThis is an update of #90, the html structure seems to have changed.
This is my temporary solution.
Initially when the field collection is empty, the label does not show. But once you add a value, save it, and then later remove the value it still shows the label.
I am thinking of doing an unset on pre_save if fields are empty.
Comment #92
AnaSwin CreditAttribution: AnaSwin commentedHello!
I don't know if it can help now, but I have found an alternative solution with hooks. I use HOOK_preprocess_field and I look for ['element']['#field_type'] === 'field_collection'. If an element of this type have an empty ['element']['#items'], I apply the same parameters than an hidden label : ['element']['#label_display'] to hidden, ['label_hidden'] to true, ['label'] to null and add 'field-label-hidden' to ['classes_array'].
Field Collection Version used : 7.x-1.0-beta12
Drupal Version : 7.53 (yes not updaded yet :p)
I hope it can help someone if the HTML structure have changed! Thanks for your works :)
Good luck!
Comment #93
kosher CreditAttribution: kosher commentedI am using 7.x-1.0-beta12, and I am not seeing an option to remove the label of a field collection if it is empty.
I have looked at the patch, and much of it looks to have been implemented in the beta12 version, but there are some lines of code from the original (pre-patch) that have been changed.
Anyone else having issues? Anyone with a solution?
Comment #94
kosher CreditAttribution: kosher commentedI am trying to use the suggestions in #92.
I have:
But it is not working. Any suggestions?
Comment #95
kosher CreditAttribution: kosher commentedI ended up figuring out how to hide the labels for the empty field collections.
This is what I did: