1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Support for the Migrate API.
|
6
|
*
|
7
|
* Your field collection migration should be run after the host entity
|
8
|
* migration. For example, if the collection is attached to nodes via a field
|
9
|
* named 'field_attached_data', and if the nodes are being imported by
|
10
|
* ArticleMigration, your collection migration class constructor should look
|
11
|
* like:
|
12
|
*
|
13
|
* @code
|
14
|
* $this->dependencies = array('Article');
|
15
|
*
|
16
|
* $this->destination = new MigrateDestinationFieldCollection(
|
17
|
* 'field_attached_data',
|
18
|
* array('host_entity_type' => 'node')
|
19
|
* );
|
20
|
*
|
21
|
* $this->addFieldMapping('host_entity_id', 'source_article_id')
|
22
|
* ->sourceMigration('Article');
|
23
|
* @endcode
|
24
|
*
|
25
|
* @see http://drupal.org/node/1900640
|
26
|
*/
|
27
|
|
28
|
/**
|
29
|
* Destination class implementing migration into field_collection.
|
30
|
*/
|
31
|
class MigrateDestinationFieldCollection extends MigrateDestinationEntity {
|
32
|
/**
|
33
|
* The type of entity hosting this collection field (e.g., node).
|
34
|
*
|
35
|
* @var string
|
36
|
*/
|
37
|
protected $hostEntityType;
|
38
|
|
39
|
static public function getKeySchema() {
|
40
|
return array(
|
41
|
'item_id' => array(
|
42
|
'type' => 'int',
|
43
|
'unsigned' => TRUE,
|
44
|
'not null' => TRUE,
|
45
|
'description' => 'ID of field collection item',
|
46
|
),
|
47
|
);
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Basic initialization.
|
52
|
*
|
53
|
* @param string $bundle
|
54
|
* Bundle name.
|
55
|
* @param array $options
|
56
|
* (optional) Options applied to collections.
|
57
|
*/
|
58
|
public function __construct($bundle, array $options = array()) {
|
59
|
parent::__construct('field_collection_item', $bundle, $options);
|
60
|
$this->hostEntityType = $options['host_entity_type'];
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Returns a list of fields available to be mapped for this collection
|
65
|
* (bundle).
|
66
|
*
|
67
|
* @return array
|
68
|
* Keys: machine names of the fields (to be passed to addFieldMapping).
|
69
|
* Values: Human-friendly descriptions of the fields.
|
70
|
*/
|
71
|
public function fields() {
|
72
|
$fields = migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle);
|
73
|
$fields['item_id'] = t('Field collection entity ID');
|
74
|
$fields['host_entity_id'] = t('Field collection host ID');
|
75
|
return $fields;
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Import a single field collection item.
|
80
|
*
|
81
|
* @param $collection
|
82
|
* Collection object to build. Pre-filled with any fields mapped in the
|
83
|
* migration.
|
84
|
* @param $row
|
85
|
* Raw source data object - passed through to prepare/complete handlers.
|
86
|
*
|
87
|
* @return array|false
|
88
|
* Array of key fields (item_id only in this case) of the collection that
|
89
|
* was saved or FALSE on failure.
|
90
|
*/
|
91
|
public function import(stdClass $collection, stdClass $row) {
|
92
|
$updating = FALSE;
|
93
|
if (isset($row->migrate_map_destid1)) {
|
94
|
// We're updated an existing entity - start from the previous data.
|
95
|
// entity_load() returns an array, so we get the field collection entity
|
96
|
// with array_shift().
|
97
|
if ($entity = array_shift(entity_load('field_collection_item', array($row->migrate_map_destid1), array(), TRUE))) {
|
98
|
$entity_old = clone $entity;
|
99
|
$updating = TRUE;
|
100
|
}
|
101
|
}
|
102
|
|
103
|
if (!$updating) {
|
104
|
// Skip the collection if it has no host.
|
105
|
if (empty($collection->host_entity_id)) {
|
106
|
throw new MigrateException('Could not find host entity of the field collection to import.');
|
107
|
}
|
108
|
$entity = entity_create('field_collection_item', array('field_name' => $this->bundle));
|
109
|
$updating = FALSE;
|
110
|
$host_entity = entity_load_single($this->hostEntityType, $collection->host_entity_id);
|
111
|
entity_get_controller($this->hostEntityType)->resetCache();
|
112
|
|
113
|
if (isset($row->language)) {
|
114
|
$entity->setHostEntity($this->hostEntityType, $host_entity, $row->language, TRUE);
|
115
|
}
|
116
|
else {
|
117
|
$entity->setHostEntity($this->hostEntityType, $host_entity);
|
118
|
}
|
119
|
}
|
120
|
|
121
|
unset($collection->host_entity_id);
|
122
|
|
123
|
foreach ((array) $collection as $field => $value) {
|
124
|
$entity->{$field} = $value;
|
125
|
}
|
126
|
|
127
|
$this->prepare($entity, $row);
|
128
|
|
129
|
// Restore fields from original field_collection_item if updating
|
130
|
if ($updating) {
|
131
|
foreach ($entity as $field => $value) {
|
132
|
if ('field_' != substr($field, 0, 6)) {
|
133
|
continue;
|
134
|
}
|
135
|
elseif (property_exists($entity_old, $field) && !property_exists($collection, $field)) {
|
136
|
$entity->$field = $entity_old->$field;
|
137
|
}
|
138
|
}
|
139
|
}
|
140
|
|
141
|
migrate_instrument_start('field_collection_save');
|
142
|
$status = entity_save('field_collection_item', $entity);
|
143
|
migrate_instrument_stop('field_collection_save');
|
144
|
|
145
|
if (in_array($this->hostEntityType, array('node', 'field_collection_item')) || ($status !== FALSE)) {
|
146
|
$this->complete($entity, $row);
|
147
|
if ($updating) {
|
148
|
$this->numUpdated++;
|
149
|
}
|
150
|
else {
|
151
|
$this->numCreated++;
|
152
|
}
|
153
|
return array($entity->item_id);
|
154
|
}
|
155
|
else {
|
156
|
return FALSE;
|
157
|
}
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* Delete a migrated collection.
|
162
|
*
|
163
|
* @param $key
|
164
|
* Array of fields representing the key.
|
165
|
*/
|
166
|
public function rollback(array $key) {
|
167
|
$item_id = reset($key);
|
168
|
|
169
|
$this->prepareRollback($item_id);
|
170
|
$field_collection_item = field_collection_item_load($item_id);
|
171
|
// If the collection wasn't imported then we can't roll it back, so check if
|
172
|
// the loaded object is an instance of the FieldCollectionItemEntity class.
|
173
|
if ($field_collection_item instanceof FieldCollectionItemEntity) {
|
174
|
$field_collection_item->delete();
|
175
|
}
|
176
|
|
177
|
$this->completeRollback($item_id);
|
178
|
return TRUE;
|
179
|
}
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Implements migrate hook_migrate_api().
|
184
|
*/
|
185
|
function field_collection_migrate_api() {
|
186
|
$api = array(
|
187
|
'api' => 2,
|
188
|
);
|
189
|
return $api;
|
190
|
}
|