Projet

Général

Profil

Paste
Télécharger (5,66 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_collection / field_collection.migrate.inc @ 5e632cae

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
// Avoid issues when migrate module is disabled.
29
if (!class_exists('MigrateDestinationEntity')) {
30
  return;
31
}
32

    
33
/**
34
 * Destination class implementing migration into field_collection.
35
 */
36
class MigrateDestinationFieldCollection extends MigrateDestinationEntity {
37
  /**
38
   * The type of entity hosting this collection field (e.g., node).
39
   *
40
   * @var string
41
   */
42
  protected $hostEntityType;
43

    
44
  public static function getKeySchema() {
45
    return array(
46
      'item_id' => array(
47
        'type' => 'int',
48
        'unsigned' => TRUE,
49
        'not null' => TRUE,
50
        'description' => 'ID of field collection item',
51
      ),
52
    );
53
  }
54

    
55
  /**
56
   * Basic initialization.
57
   *
58
   * @param string $bundle
59
   *   Bundle name.
60
   * @param array $options
61
   *   (optional) Options applied to collections.
62
   */
63
  public function __construct($bundle, array $options = array()) {
64
    parent::__construct('field_collection_item', $bundle, $options);
65
    $this->hostEntityType = $options['host_entity_type'];
66
  }
67

    
68
  /**
69
   * Returns a list of fields available to be mapped for this collection
70
   * (bundle).
71
   *
72
   * @return array
73
   *   Keys: machine names of the fields (to be passed to addFieldMapping).
74
   *   Values: Human-friendly descriptions of the fields.
75
   */
76
  public function fields() {
77
    $fields = migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle);
78
    $fields['item_id'] = t('Field collection entity ID');
79
    $fields['host_entity_id'] = t('Field collection host ID');
80
    return $fields;
81
  }
82

    
83
  /**
84
   * Import a single field collection item.
85
   *
86
   * @param \stdClass $collection
87
   *   Collection object to build. Pre-filled with any fields mapped in the
88
   *   migration.
89
   * @param \stdClass $row
90
   *   Raw source data object - passed through to prepare/complete handlers.
91
   *
92
   * @return array|bool
93
   *   Array of key fields (item_id only in this case) of the collection that
94
   *   was saved or FALSE on failure.
95
   */
96
  public function import(stdClass $collection, stdClass $row) {
97
    $updating = FALSE;
98
    if (isset($row->migrate_map_destid1)) {
99
      // We're updated an existing entity - start from the previous data.
100
      // entity_load() returns an array, so we get the field collection entity
101
      // with reset().
102
      $result = entity_load('field_collection_item', array($row->migrate_map_destid1), array(), TRUE);
103
      $entity = reset($result);
104
      if ($entity) {
105
        $entity_old = clone $entity;
106
        $updating = TRUE;
107
      }
108
    }
109

    
110
    if (!$updating) {
111
      // Skip the collection if it has no host.
112
      if (empty($collection->host_entity_id)) {
113
        throw new MigrateException('Could not find host entity of the field collection to import.');
114
      }
115
      $entity = entity_create('field_collection_item', array('field_name' => $this->bundle));
116
      $updating = FALSE;
117
      $host_entity = entity_load_single($this->hostEntityType, $collection->host_entity_id);
118
      entity_get_controller($this->hostEntityType)->resetCache();
119

    
120
      if (isset($row->language)) {
121
        $entity->setHostEntity($this->hostEntityType, $host_entity, $row->language, TRUE);
122
      }
123
      else {
124
        $entity->setHostEntity($this->hostEntityType, $host_entity);
125
      }
126
    }
127

    
128
    unset($collection->host_entity_id);
129

    
130
    foreach ((array) $collection as $field => $value) {
131
      $entity->{$field} = $value;
132
    }
133

    
134
    $this->prepare($entity, $row);
135

    
136
    // Restore fields from original field_collection_item if updating.
137
    if ($updating) {
138
      foreach ($entity as $field => $value) {
139
        if ('field_' != substr($field, 0, 6)) {
140
          continue;
141
        }
142

    
143
        if (property_exists($entity_old, $field) && !property_exists($collection, $field)) {
144
          $entity->$field = $entity_old->$field;
145
        }
146
      }
147
    }
148

    
149
    migrate_instrument_start('field_collection_save');
150
    $status = entity_save('field_collection_item', $entity);
151
    migrate_instrument_stop('field_collection_save');
152

    
153
    if ($status !== FALSE || in_array($this->hostEntityType, array('node', 'field_collection_item'))) {
154
      $this->complete($entity, $row);
155
      if ($updating) {
156
        $this->numUpdated++;
157
      }
158
      else {
159
        $this->numCreated++;
160
      }
161
      return array($entity->item_id);
162
    }
163

    
164
    return FALSE;
165
  }
166

    
167
  /**
168
   * Delete a migrated collection.
169
   *
170
   * @param $key
171
   *   Array of fields representing the key.
172
   */
173
  public function rollback(array $key) {
174
    $item_id = reset($key);
175

    
176
    $this->prepareRollback($item_id);
177
    $field_collection_item = field_collection_item_load($item_id);
178
    // If the collection wasn't imported then we can't roll it back, so check if
179
    // the loaded object is an instance of the FieldCollectionItemEntity class.
180
    if ($field_collection_item instanceof FieldCollectionItemEntity) {
181
      $field_collection_item->delete();
182
    }
183

    
184
    $this->completeRollback($item_id);
185
    return TRUE;
186
  }
187

    
188
}
189

    
190
/**
191
 * Implements migrate hook_migrate_api().
192
 */
193
function field_collection_migrate_api() {
194
  return array(
195
    'api' => 2,
196
  );
197
}