View source
<?php
class MigrateDestinationParagraphsItem extends MigrateDestinationEntity {
protected $field_name;
public function getFieldName() {
return $this->field_name;
}
public static function getKeySchema() {
return array(
'item_id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'ID of field collection item',
),
);
}
public function __construct($bundle, array $options = array()) {
parent::__construct('paragraphs_item', $bundle, $options);
$this->field_name = isset($options['field_name']) ? $options['field_name'] : '';
}
public static function options($language, $text_format) {
return compact('language', 'text_format');
}
public function fields(Migration $migration = NULL) {
$fields = array();
$fields['field_name'] = t('Field name');
$fields['archived'] = t('Archived status of the paragraph item');
$fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle, $migration);
$fields += migrate_handler_invoke_all('ParagraphsItem', 'fields', $this->entityType, $this->bundle, $migration);
return $fields;
}
public function bulkRollback(array $item_ids) {
migrate_instrument_start('paragraphs_item_delete_multiple');
$this
->prepareRollback($item_ids);
entity_delete_multiple('paragraphs_item', $item_ids);
$this
->completeRollback($item_ids);
migrate_instrument_stop('paragraphs_item_delete_multiple');
}
public function import(object $paragraphs_item, object $row) {
$migration = Migration::currentMigration();
if (isset($row->migrate_map_destid1)) {
if (isset($paragraphs_item->item_id)) {
if ($paragraphs_item->item_id != $row->migrate_map_destid1) {
throw new MigrateException(t("Incoming item_id !item_id and map destination item_id !destid1 don't match", array(
'!item_id' => $paragraphs_item->item_id,
'!destid1' => $row->migrate_map_destid1,
)));
}
}
else {
$paragraphs_item->item_id = $row->migrate_map_destid1;
$paragraphs_item->is_new = FALSE;
$paragraphs_item->is_new_revision = FALSE;
$values = db_select('paragraphs_item', 'p')
->fields('p', array(
'revision_id',
))
->condition('item_id', $paragraphs_item->item_id)
->execute()
->fetchAssoc();
if (empty($values)) {
throw new MigrateException(t("Incoming paragraphs ID !item_id no longer exists", array(
'!item_id' => $paragraphs_item->item_id,
)));
}
$paragraphs_item->revision_id = $values['revision_id'];
}
}
if ($migration
->getSystemOfRecord() == Migration::DESTINATION) {
if (!isset($paragraphs_item->item_id)) {
throw new MigrateException(t('System-of-record is DESTINATION, but no destination item_id provided'));
}
$raw_paragraph = $paragraphs_item;
$entity = paragraphs_item_load($paragraphs_item->item_id);
if (empty($entity)) {
throw new MigrateException(t('System-of-record is DESTINATION, but paragraphs item !item_id does not exist', array(
'!item_id' => $paragraphs_item->item_id,
)));
}
}
else {
$defaults = array(
'language' => $this->language,
'bundle' => $this->bundle,
'field_name' => $this->field_name,
'archived' => 0,
);
foreach ($defaults as $field => $value) {
if (!isset($paragraphs_item->{$field})) {
$paragraphs_item->{$field} = $value;
}
}
}
$this
->prepare($paragraphs_item, $row);
if ($migration
->getSystemOfRecord() == Migration::DESTINATION) {
foreach ($raw_paragraph as $field => $value) {
$entity->{$field} = $paragraphs_item->{$field};
}
}
else {
$entity = entity_create('paragraphs_item', (array) $paragraphs_item);
}
if (isset($entity->item_id) && $entity->item_id) {
$updating = TRUE;
}
else {
$updating = FALSE;
}
migrate_instrument_start('paragraphs_item_save');
$entity
->save(TRUE);
migrate_instrument_stop('paragraphs_item_save');
$this
->complete($entity, $row);
if (isset($entity->item_id) && $entity->item_id > 0) {
$return = array(
$entity->item_id,
);
if ($updating) {
$this->numUpdated++;
}
else {
$this->numCreated++;
}
}
else {
$return = FALSE;
}
return $return;
}
}