class MigrateDestinationFieldCollection in Field collection 7
Destination class implementing migration into field_collection.
Hierarchy
- class \MigrateDestination
Expanded class hierarchy of MigrateDestinationFieldCollection
File
- ./
field_collection.migrate.inc, line 36 - Support for the Migrate API.
View source
class MigrateDestinationFieldCollection extends MigrateDestinationEntity {
/**
* The type of entity hosting this collection field (e.g., node).
*
* @var string
*/
protected $hostEntityType;
/**
*
*/
public static function getKeySchema() {
return array(
'item_id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'ID of field collection item',
),
);
}
/**
* Basic initialization.
*
* @param string $bundle
* Bundle name.
* @param array $options
* (optional) Options applied to collections.
*/
public function __construct($bundle, array $options = array()) {
parent::__construct('field_collection_item', $bundle, $options);
$this->hostEntityType = $options['host_entity_type'];
}
/**
* Returns a list of fields available to be mapped for this collection
* (bundle).
*
* @return array
* Keys: machine names of the fields (to be passed to addFieldMapping).
* Values: Human-friendly descriptions of the fields.
*/
public function fields() {
$fields = migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle);
$fields['item_id'] = t('Field collection entity ID');
$fields['host_entity_id'] = t('Field collection host ID');
return $fields;
}
/**
* Import a single field collection item.
*
* @param object $collection
* Collection object to build. Pre-filled with any fields mapped in the
* migration.
* @param object $row
* Raw source data object - passed through to prepare/complete handlers.
*
* @return array|bool
* Array of key fields (item_id only in this case) of the collection that
* was saved or FALSE on failure.
*/
public function import(stdClass $collection, stdClass $row) {
$updating = FALSE;
if (isset($row->migrate_map_destid1)) {
// We're updated an existing entity - start from the previous data.
// entity_load() returns an array, so we get the field collection entity
// with reset().
$result = entity_load('field_collection_item', array(
$row->migrate_map_destid1,
), array(), TRUE);
$entity = reset($result);
if ($entity) {
$entity_old = clone $entity;
$updating = TRUE;
}
}
if (!$updating) {
// Skip the collection if it has no host.
if (empty($collection->host_entity_id)) {
throw new MigrateException('Could not find host entity of the field collection to import.');
}
$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_get_controller($this->hostEntityType)
->resetCache();
if (isset($row->language)) {
$entity
->setHostEntity($this->hostEntityType, $host_entity, $row->language, TRUE);
}
else {
$entity
->setHostEntity($this->hostEntityType, $host_entity);
}
}
unset($collection->host_entity_id);
foreach ((array) $collection as $field => $value) {
$entity->{$field} = $value;
}
$this
->prepare($entity, $row);
// Restore fields from original field_collection_item if updating.
if ($updating) {
foreach ($entity as $field => $value) {
if ('field_' != substr($field, 0, 6)) {
continue;
}
if (property_exists($entity_old, $field) && !property_exists($collection, $field)) {
$entity->{$field} = $entity_old->{$field};
}
}
}
migrate_instrument_start('field_collection_save');
$status = entity_save('field_collection_item', $entity);
migrate_instrument_stop('field_collection_save');
if ($status !== FALSE || in_array($this->hostEntityType, array(
'node',
'field_collection_item',
))) {
$this
->complete($entity, $row);
if ($updating) {
$this->numUpdated++;
}
else {
$this->numCreated++;
}
return array(
$entity->item_id,
);
}
return FALSE;
}
/**
* Delete a migrated collection.
*
* @param $key
* Array of fields representing the key.
*/
public function rollback(array $key) {
$item_id = reset($key);
$this
->prepareRollback($item_id);
$field_collection_item = field_collection_item_load($item_id);
// If the collection wasn't imported then we can't roll it back, so check if
// the loaded object is an instance of the FieldCollectionItemEntity class.
if ($field_collection_item instanceof FieldCollectionItemEntity) {
$field_collection_item
->delete();
}
$this
->completeRollback($item_id);
return TRUE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MigrateDestination:: |
protected | property | Maintain stats on the number of destination objects created or updated. | |
MigrateDestination:: |
protected | property | ||
MigrateDestination:: |
public | function | ||
MigrateDestination:: |
public | function | ||
MigrateDestination:: |
public | function | Reset numCreated and numUpdated back to 0. | |
MigrateDestinationEntity:: |
protected | property | The bundle (node type, vocabulary, etc.) of the destination. | |
MigrateDestinationEntity:: |
protected | property | The entity type (node, user, taxonomy_term, etc.) of the destination. | |
MigrateDestinationEntity:: |
protected | property | Default language for text fields in this destination. | |
MigrateDestinationEntity:: |
protected | property | Default input format for text fields in this destination. | |
MigrateDestinationEntity:: |
public static | function | Flattens an array of allowed values. | |
MigrateDestinationEntity:: |
public | function | Give handlers a shot at modifying the object (or taking additional action) after saving it. | |
MigrateDestinationEntity:: |
public | function | Give handlers a shot at cleaning up after an entity has been rolled back. | |
MigrateDestinationEntity:: |
public static | function | Perform field validation against the field data in an entity. Wraps field_attach_validate to handle exceptions cleanly and provide maximum information for identifying the cause of validation errors. | |
MigrateDestinationEntity:: |
public | function | ||
MigrateDestinationEntity:: |
public | function | ||
MigrateDestinationEntity:: |
public | function | ||
MigrateDestinationEntity:: |
public | function | ||
MigrateDestinationEntity:: |
public | function | Give handlers a shot at modifying the object before saving it. | |
MigrateDestinationEntity:: |
public | function | Give handlers a shot at cleaning up before an entity has been rolled back. | |
MigrateDestinationEntity:: |
public | function |
Derived classes must implement __toString(). Overrides MigrateDestination:: |
|
MigrateDestinationFieldCollection:: |
protected | property | The type of entity hosting this collection field (e.g., node). | |
MigrateDestinationFieldCollection:: |
public | function |
Returns a list of fields available to be mapped for this collection
(bundle). Overrides MigrateDestination:: |
|
MigrateDestinationFieldCollection:: |
public static | function | ||
MigrateDestinationFieldCollection:: |
public | function |
Import a single field collection item. Overrides MigrateDestination:: |
|
MigrateDestinationFieldCollection:: |
public | function | Delete a migrated collection. | |
MigrateDestinationFieldCollection:: |
public | function |
Basic initialization. Overrides MigrateDestinationEntity:: |