I'm currently preventing display of empty field collection wrapper markup by loading the collection item entities in hook_node_view() and checking each item's field content according to my custom requirements.
This works well enough, but it wouldn't it be more desirable to avoid saving and referencing "empty" collections in the first place?
Is it possible to add a pre-create hook, which other modules can use to determine if a field collection item is "empty" (by whatever criteria are appropriate to the implementation). If all hook invocations return FALSE, do not create a new field collection item entity and do not reference it in the parent entity.
Also invoke the same hook at output - if all hook invocations return FALSE for all items in the collection, do not render any field collection wrapper markup.
Comment | File | Size | Author |
---|---|---|---|
#9 | field_collection_text_empty.zip | 1.23 KB | Vote_Sizing_Steve |
#1 | 1614578-field_collection-is_empty_alter-1.patch | 1.85 KB | theunraveler |
Comments
Comment #1
theunraveler CreditAttribution: theunraveler commentedHere is a patch that adds a new alter hook to the module called
hook_field_collection_is_empty_alter()
. It allows other modules to declare whether or not a field collection should be considered empty. Usage examples are in field_collection.api.php in this patch.Comment #2
theunraveler CreditAttribution: theunraveler commentedComment #3
RobW CreditAttribution: RobW commentedIf the goal is to get rid of empty wrapper divs on empty collections it seems like it would be easier to remove the wrapper markup from the module, so fields sees collections with no items as empty. I'm working on a patch which removes all theme markup from the module and puts it in tpl files in #1157794: Move markup to template files and improve theming in new 2.x branch, and there has been more focused work going on in #1276258: Completely hide empty field collections.
Does your function have utility beyond not rendering wrapper divs for empty collections?
Comment #4
theunraveler CreditAttribution: theunraveler commentedYes, it does. The best way to deal with this is not to just hide empty field collections, but actually prevent them from being saved in the first place. This patch allows you to do that by implementing a hook in your custom module.
Comment #5
skruf CreditAttribution: skruf commentedI would say this is defiantly needed as you need to be able to specify when a field collection is empty and to stop it from being saved it the DB. If for example you have a checkbox field as part of the collection then the field collection will never evaluate to empty as it always has a value.
Comment #6
RobW CreditAttribution: RobW commentedThanks for the explanation, just trying to reduce duplicate effort.
You might want to take a look at #1239946: Embedded field collection items with a default value result in new items on save. Seems like there could be some overlap there too.
Comment #7
fagoThanks - that makes sense, I've improved comments and variable names a bit and committed it.
Comment #8
fagoLet's all also implement baked in support for that, such that you can simply check the fields that must be filled for a collection to be considered non-empty.
http://drupal.org/node/1662998#comment-6281810
Comment #9
Vote_Sizing_Steve CreditAttribution: Vote_Sizing_Steve commentedHere's some code I'm using in my site which uses this patch:
... which seems to work great on my http://iVoteSize.com/en/node/add/ivotesize-survey and http://iVoteSize.com/en/user/[uid]/edit pages. Also attached is the more drupal-friendly code that I had a developer write.