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.

Comments

theunraveler’s picture

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

theunraveler’s picture

Status: Active » Needs review
RobW’s picture

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

theunraveler’s picture

Yes, 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.

skruf’s picture

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

RobW’s picture

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

fago’s picture

Status: Needs review » Fixed

Thanks - that makes sense, I've improved comments and variable names a bit and committed it.

fago’s picture

Let'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

Vote_Sizing_Steve’s picture

StatusFileSize
new1.23 KB

Here's some code I'm using in my site which uses this patch:

function ivotesize_field_collection_is_empty_alter( &$Empty, $Item ){
	if( 
			in_array( 
				$Item->field_name, array(
					'field_voter_measurements',
					'field_measurements',
					'field_questions' ))){
		$Element = array(
			text => 'text',
			node_reference => 'nid' );
		foreach( 
				field_info_instances( 
					'field_collection_item', 
					$Item->field_name ) as $Instance ){
			if( isset( $Instance[ widget ])){
				foreach( $Item->{ $Instance[ field_name ]} as $Translation ){
					foreach( $Translation as $Translation_Part ){
						if( !Empty( $Translation_Part[ $Element[ $Instance[ widget ][ module ]]])){
							$Empty = false;
							return; }}}}
      $Empty = true; }}}

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

Status: Fixed » Closed (fixed)

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