You are here

function replicate_field_collection_clone_items in Replicate field collection 7

Replicate a field collection field.

Parameters

object $entity: Entity to be modified.

string $entity_type: Entity type.

string $field_name: Name of the field.

string $language: Language of the field.

1 call to replicate_field_collection_clone_items()
replicate_field_collection_replicate_field_field_collection in ./replicate_field_collection.module
Implements hook_replicate_field_FIELD_TYPE().

File

./replicate_field_collection.module, line 48
Main methods of Replicate field collection module.

Code

function replicate_field_collection_clone_items(&$entity, $entity_type, $field_name, $language = LANGUAGE_NONE) {
  $field = $entity->{$field_name}[$language];
  if (is_array($field) && count($field) > 0) {

    // Clean the previous entities from the field, so the new
    // can be saved instead.
    unset($entity->{$field_name}[$language]);
    foreach ($field as $value) {
      $old_item = field_collection_field_get_entity($value);
      if (!empty($old_item)) {
        $new_item = replicate_clone_entity('field_collection_item', $old_item);
        $new_item
          ->setHostEntity($entity_type, $entity, $language);

        // Don't save the new field collection,
        // it will be saved along with the host entity.
      }
    }
  }
}