Does your module support repeating date field within a field_collection_item entity.

http://drupal.org/project/field_collection

Comments

joelstein’s picture

Status: Active » Postponed (maintainer needs more info)

I don't know... I've never tried it. Have you?

chalee’s picture

No I haven't tried it. Description on the module page says:
"...When you edit an instance, it breaks the new instance into its own node, and sets the exception in the original node."

Since it says 'node' I thought it would not work with other entity types in this case the field_collection_item. When I edit the date instance I want it to create a new field_collection_item instead of a node.

joelstein’s picture

Thanks for the clarification. I don't know, and know very little about the Field Collection module. Feel free to try it out and report back here what you find.

chalee’s picture

I tried it. It doesn't work i.e. the links to edit/delete instance do not show. The following error shows:

Notice: Undefined property: FieldCollectionItemEntity::$type in date_repeat_type() (line 62 of C:\wamp\www\drupal\sites\all\modules\date\date.module)
joelstein’s picture

Status: Postponed (maintainer needs more info) » Active

Okay, I understand now what Field Collection is. It's its own entity type, and you can add field collections as fields to a content type. Thus, you can have a field collection called something like "common fields", then add into that collection a date field. Now that you have a field collection, you can add that "common fields" field to your content type, and then when you are editing the node, all the collection's fields are part of the node, as well. Pretty slick little module.

Date Repeat Instance worked with the assumption that date repeats would be attached to nodes (directly), thus letting you create a new node. That presents two problems when trying to integrate it with Field Collection. The first is simply a compatibility issue. Date Repeat Instance needs to handle date repeats without the assumption that they will always be attached to nodes, checking the entity type first, which would remove the error you reported in #4. I've committed that change here:

http://drupal.org/commitlog/commit/17454/9c74cd9349c75aabc0bbe9e25587922...

However, the other issue stems from the fact that field collections are their own entities. So, in order to detach a date instance, I'd not only need to clone the corresponding node to which its attached, but also clone the field collection. Not a terribly difficult thing to accomplish... I just have to become more familiar with how Field Collection works.

chalee’s picture

I agree with everything except the last part that says:
"So, in order to detach a date instance, I'd not only need to clone the corresponding node to which its attached, but also clone the field collection"
I don't think you should clone the host entity (node or field_collection_item since field collection can be embedded inside another field collection). For example in my use case I'm using field_collection to add task records to a project record. The task is scheduled every month for example. As you can see I only want to break off the task instance not the project.

If you think there could be other use cases where you need to clone the host entity as well then I suggest you make it optional per each entity bundle.

joelstein’s picture

So, in your example, you would detach a repeating date to create a new instance of the field collection within your node?

Sounds complicated. I'll leave this open, in case somebody wants to submit a patch. I probably won't, though.

chalee’s picture

@joelstein: You don't clone the 'field collection' field but the field_collection_item entity. It's not complicated at all. All you do is clone the entity to which the repeating date field is residing. Here's the code snippet that will clone all field collection items of a single field collection field. Assuming we have a node $node with a field collection field called field_fc which has two fields field_a and field_b.

$fc_items =  field_get_items('node',$node,'field_fc');
	$fc_item_ids = array();
	foreach ($fc_items as $fc_item){
		$fc_item_ids[] = $fc_item['value'];
	}
	$fc_item_entities = entity_load('field_collection_item',$fc_item_ids, array(), TRUE);
	
	foreach ($fc_item_entities as $fc_item_entity){
		$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_fc'));
		$field_collection_item->setHostEntity('node', $node);
		$field_collection_item->field_a[LANGUAGE_NONE][]['value'] = $fc_item_entity->field_a['und']['0']['value'];
		$field_collection_item->field_b[LANGUAGE_NONE][]['value'] = $fc_item_entity->field_b['und']['0']['value'];
		$field_collection_item->save(); 	
	}
chalee’s picture

@joelstein: Did you make an progress on this issue yet?