biblio_field_collection.inc in Bibliography Module 7.3
File
includes/migrate/plugins/destinations/biblio_field_collection.inc
View source
<?php
class BiblioMigrateDestinationFieldCollection extends MigrateDestinationEntity {
protected $hostEntityType;
public static function getKeySchema() {
return array(
'cid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'ID of field collection item',
),
);
}
public function __construct($bundle, array $options = array()) {
parent::__construct('field_collection_item', $bundle, $options);
$this->hostEntityType = $options['host_entity_type'];
}
public function fields() {
$fields = migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle);
$fields['host_entity_id'] = t('Field collection host ID');
return $fields;
}
public function import(stdClass $collection, stdClass $row) {
$entity = entity_create('field_collection_item', array(
'field_name' => $this->bundle,
));
$updating = FALSE;
$host_entity = entity_load_single($this->hostEntityType, $collection->host_entity_id);
$entity
->setHostEntity($this->hostEntityType, $host_entity);
unset($collection->host_entity_id);
foreach ((array) $collection as $field => $value) {
$entity->{$field} = $value;
}
$this
->prepare($entity, $row);
migrate_instrument_start('field_collection_save');
$status = $entity
->save();
migrate_instrument_stop('field_collection_save');
if ($entity->item_id) {
$this
->complete($entity, $row);
if ($updating) {
$this->numUpdated++;
}
else {
$this->numCreated++;
}
return array(
$entity->item_id,
);
}
else {
return FALSE;
}
}
public function rollback(array $key) {
$item_id = reset($key);
$this
->prepareRollback($item_id);
$field_collection_item = field_collection_item_load($item_id);
if ($field_collection_item instanceof FieldCollectionItemEntity) {
$field_collection_item
->delete();
}
$this
->completeRollback($item_id);
return TRUE;
}
}