/**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     if ($row->isStub()) {
         throw new MigrateException('Config entities can not be stubbed.');
     }
     $ids = $this->getIds();
     $id_key = $this->getKey('id');
     if (count($ids) > 1) {
         // Ids is keyed by the key name so grab the keys.
         $id_keys = array_keys($ids);
         if (!$row->getDestinationProperty($id_key)) {
             // Set the id into the destination in for form "val1.val2.val3".
             $row->setDestinationProperty($id_key, $this->generateId($row, $id_keys));
         }
     }
     $entity = $this->getEntity($row, $old_destination_id_values);
     $entity->save();
     if (count($ids) > 1) {
         // This can only be a config entity, content entities have their id key
         // and that's it.
         $return = array();
         foreach ($id_keys as $id_key) {
             $return[] = $entity->get($id_key);
         }
         return $return;
     }
     return array($entity->id());
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 protected function getEntity(Row $row, array $old_destination_id_values)
 {
     if ($row->isStub()) {
         $row->setDestinationProperty('name', $this->t('Stub name for source tid:') . $row->getSourceProperty('tid'));
     }
     return parent::getEntity($row, $old_destination_id_values);
 }
 /**
  * Posts are either type recpie or restaurant_review.
  * Recpies contain <div class="recipe">, everything else is considered type
  * restaurant_review.
  */
 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property)
 {
     $return = array();
     $caption_tags = $this->getTags($value)['caption']['values'];
     if (is_array($caption_tags)) {
         foreach ($caption_tags as $image) {
             // Search for imported File:
             $q = db_select('migrate_map_donkeymedia_file', 'm')->fields('m', array('sourceid1', 'destid1'))->condition('m.sourceid1', $image['attachment'])->execute();
             $map = $q->fetchObject();
             if ($map) {
                 $dest = array('target_id' => $map->destid1, 'alt' => 'Sorry. No describing text available.', 'title' => 'No title');
                 // Alt Tag.
                 if (!empty($image['alt'])) {
                     $dest['alt'] = $image['alt'];
                 }
                 // Title.
                 if (!empty($image['caption'])) {
                     $dest['title'] = $image['caption'];
                 }
                 $return[] = $dest;
             } else {
                 echo 'Source Id: "' . $image['attachment'] . '" not Found in File Map table (migrate_map_donkeymedia_file)' . "\n";
                 throw new MigrateSkipProcessException();
             }
         }
     }
     if (!empty($return)) {
         $row->setDestinationProperty($destination_property, $return);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     if ($bundle_key = $this->getKey('bundle')) {
         $bundle = $row->getDestinationProperty($bundle_key);
     } else {
         $bundle = $this->storage->getEntityTypeId();
     }
     // Some migrations save additional data of an existing entity and only
     // provide the reference to the entity, in those cases, we can not run the
     // processing below. Migrations that need that need to provide the bundle.
     if ($bundle) {
         $field_definitions = $this->entityManager->getFieldDefinitions($this->storage->getEntityTypeId(), $bundle);
         foreach ($field_definitions as $field_name => $field_definition) {
             $field_type = $field_definition->getType();
             if ($this->migrateEntityFieldPluginManager->getDefinition($field_type, FALSE)) {
                 $destination_value = $this->migrateEntityFieldPluginManager->createInstance($field_type)->import($field_definition, $row->getDestinationProperty($field_name));
                 // @TODO: check for NULL return? Add an unset to $row? Maybe needed in
                 // exception handling? Propagate exception?
                 $row->setDestinationProperty($field_name, $destination_value);
             }
         }
     }
     $entity = $this->getEntity($row, $old_destination_id_values);
     return $this->save($entity, $old_destination_id_values);
 }
 /**
  * Tests the entity display import method.
  */
 public function testImport()
 {
     $values = array('entity_type' => 'entity_type_test', 'bundle' => 'bundle_test', 'view_mode' => 'view_mode_test', 'field_name' => 'field_name_test', 'options' => array('test setting'));
     $row = new Row();
     foreach ($values as $key => $value) {
         $row->setDestinationProperty($key, $value);
     }
     $entity = $this->getMockBuilder('Drupal\\Core\\Entity\\Entity\\EntityViewDisplay')->disableOriginalConstructor()->getMock();
     $entity->expects($this->once())->method('setComponent')->with('field_name_test', array('test setting'))->will($this->returnSelf());
     $entity->expects($this->once())->method('save')->with();
     $plugin = new TestPerComponentEntityDisplay($entity);
     $this->assertSame($plugin->import($row), array('entity_type_test', 'bundle_test', 'view_mode_test', 'field_name_test'));
     $this->assertSame($plugin->getTestValues(), array('entity_type_test', 'bundle_test', 'view_mode_test'));
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 protected function processStubRow(Row $row)
 {
     // We stub the uri value ourselves so we can create a real stub file for it.
     if (!$row->getDestinationProperty('uri')) {
         $field_definitions = $this->entityManager->getFieldDefinitions($this->storage->getEntityTypeId(), $this->getKey('bundle'));
         $value = UriItem::generateSampleValue($field_definitions['uri']);
         if (empty($value)) {
             throw new MigrateException('Stubbing failed, unable to generate value for field uri');
         }
         // generateSampleValue() wraps the value in an array.
         $value = reset($value);
         // Make it into a proper public file uri, stripping off the existing
         // scheme if present.
         $value = 'public://' . preg_replace('|^[a-z]+://|i', '', $value);
         $value = Unicode::substr($value, 0, $field_definitions['uri']->getSetting('max_length'));
         // Create a real file, so File::preSave() can do filesize() on it.
         touch($value);
         $row->setDestinationProperty('uri', $value);
     }
     parent::processStubRow($row);
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = [])
 {
     $effects = [];
     // Need to set the effects property to null on the row before the ImageStyle
     // is created, this prevents improper effect plugin initialization.
     if ($row->getDestinationProperty('effects')) {
         $effects = $row->getDestinationProperty('effects');
         $row->setDestinationProperty('effects', []);
     }
     /** @var \Drupal\Image\Entity\ImageStyle $style */
     $style = $this->getEntity($row, $old_destination_id_values);
     // Iterate the effects array so each effect plugin can be initialized.
     // Catch any missing plugin exceptions.
     foreach ($effects as $effect) {
         try {
             $style->addImageEffect($effect);
         } catch (PluginNotFoundException $e) {
             throw new MigrateException($e->getMessage(), 0, $e);
         }
     }
     $style->save();
     return array($style->id());
 }
Example #8
0
 /**
  * Do an import using the destination.
  *
  * @param array $row_values
  *   An array of row values.
  * @param string $destination_path
  *   The destination path to copy to.
  * @param string $source_base_path
  *   The source base path.
  * @return array
  *   An array of saved entities ids.
  *
  * @throws \Drupal\migrate\MigrateException
  */
 protected function doImport($row_values, $destination_path, $source_base_path = '')
 {
     $row = new Row($row_values, []);
     $row->setDestinationProperty('uri', $destination_path);
     $this->destination->configuration['source_base_path'] = $source_base_path;
     // Importing asserts there are no errors, then we just check the file has
     // been copied into place.
     return $this->destination->import($row, array());
 }
Example #9
0
 /**
  * Populates as much of the stub row as possible.
  *
  * @param \Drupal\migrate\Row $row
  *   The row of data.
  */
 protected function processStubRow(Row $row)
 {
     $bundle_key = $this->getKey('bundle');
     if ($bundle_key && empty($row->getDestinationProperty($bundle_key))) {
         if (empty($this->bundles)) {
             throw new MigrateException('Stubbing failed, no bundles available for entity type: ' . $this->storage->getEntityTypeId());
         }
         $row->setDestinationProperty($bundle_key, reset($this->bundles));
     }
     // Populate any required fields not already populated.
     $fields = $this->entityManager->getFieldDefinitions($this->storage->getEntityTypeId(), $bundle_key);
     foreach ($fields as $field_name => $field_definition) {
         if ($field_definition->isRequired() && is_null($row->getDestinationProperty($field_name))) {
             // Use the configured default value for this specific field, if any.
             if ($default_value = $field_definition->getDefaultValueLiteral()) {
                 $values[] = $default_value;
             } else {
                 // Otherwise, ask the field type to generate a sample value.
                 $field_type = $field_definition->getType();
                 /** @var \Drupal\Core\Field\FieldItemInterface $field_type_class */
                 $field_type_class = $this->fieldTypeManager->getPluginClass($field_definition->getType());
                 $values = $field_type_class::generateSampleValue($field_definition);
                 if (is_null($values)) {
                     // Handle failure to generate a sample value.
                     throw new MigrateException('Stubbing failed, unable to generate value for field ' . $field_name);
                 }
             }
             $row->setDestinationProperty($field_name, $values);
         }
     }
 }
Example #10
0
 /**
  * Process the stub values.
  *
  * @param \Drupal\migrate\Row $row
  *   The row of data.
  */
 protected function processStubRow(Row $row)
 {
     $bundle_key = $this->getKey('bundle');
     if ($bundle_key && empty($row->getDestinationProperty($bundle_key))) {
         $row->setDestinationProperty($bundle_key, reset($this->bundles));
     }
 }
Example #11
0
 /**
  * Test entity load failure.
  *
  * @covers ::getEntity
  */
 public function testGetEntityLoadFailure()
 {
     $destination = $this->getEntityRevisionDestination([]);
     $entity_type = $this->prophesize('\\Drupal\\Core\\Entity\\EntityTypeInterface');
     $entity_type->getKey('id')->willReturn('nid');
     $entity_type->getKey('revision')->willReturn('vid');
     $this->storage->getEntityType()->willReturn($entity_type->reveal());
     // Return a failed load and make sure we don't fail and we return FALSE.
     $this->storage->load(1)->shouldBeCalled()->willReturn(FALSE);
     $row = new Row(['nid' => 1, 'vid' => 2], ['nid' => 1, 'vid' => 2]);
     $row->setDestinationProperty('nid', 1);
     $this->assertFalse($destination->getEntity($row, []));
 }
 /**
  * Test importing and rolling back translated entities.
  */
 public function testTranslated()
 {
     // Create a destination.
     $this->createDestination(['translations' => TRUE]);
     // Create some pre-existing entities.
     $this->storage->create(['id' => 1, 'langcode' => 'en'])->save();
     $this->storage->create(['id' => 2, 'langcode' => 'fr'])->save();
     $translated = $this->storage->create(['id' => 3, 'langcode' => 'en']);
     $translated->save();
     $translated->addTranslation('fr')->save();
     // Pre-assert that things are as expected.
     $this->assertTranslations(1, 'en');
     $this->assertTranslations(2, 'fr');
     $this->assertTranslations(3, 'en', ['fr']);
     $this->assertFalse($this->storage->load(4));
     $destination_rows = [['id' => 1, 'langcode' => 'en', 'action' => MigrateIdMapInterface::ROLLBACK_PRESERVE], ['id' => 2, 'langcode' => 'en', 'action' => MigrateIdMapInterface::ROLLBACK_DELETE], ['id' => 3, 'langcode' => 'fr', 'action' => MigrateIdMapInterface::ROLLBACK_PRESERVE], ['id' => 4, 'langcode' => 'fr', 'action' => MigrateIdMapInterface::ROLLBACK_DELETE]];
     $rollback_actions = [];
     // Import some rows.
     foreach ($destination_rows as $idx => $destination_row) {
         $row = new Row();
         foreach ($destination_row as $key => $value) {
             $row->setDestinationProperty($key, $value);
         }
         $this->destination->import($row);
         // Check that the rollback action is correct, and save it.
         $this->assertEquals($destination_row['action'], $this->destination->rollbackAction());
         $rollback_actions[$idx] = $this->destination->rollbackAction();
     }
     $this->assertTranslations(1, 'en');
     $this->assertTranslations(2, 'fr', ['en']);
     $this->assertTranslations(3, 'en', ['fr']);
     $this->assertTranslations(4, 'fr');
     // Rollback the rows.
     foreach ($destination_rows as $idx => $destination_row) {
         if ($rollback_actions[$idx] == MigrateIdMapInterface::ROLLBACK_DELETE) {
             $this->destination->rollback($destination_row);
         }
     }
     // No change, update of existing translation.
     $this->assertTranslations(1, 'en');
     // Remove added translation.
     $this->assertTranslations(2, 'fr');
     // No change, update of existing translation.
     $this->assertTranslations(3, 'en', ['fr']);
     // No change, can't remove default translation.
     $this->assertTranslations(4, 'fr');
 }
Example #13
0
 /**
  * Tests setting/getting multiple destination IDs.
  */
 public function testMultipleDestination()
 {
     $row = new Row($this->testValues, $this->testSourceIds);
     // Set some deep nested values.
     $row->setDestinationProperty('image/alt', 'alt text');
     $row->setDestinationProperty('image/fid', 3);
     $this->assertTrue($row->hasDestinationProperty('image'));
     $this->assertFalse($row->hasDestinationProperty('alt'));
     $this->assertFalse($row->hasDestinationProperty('fid'));
     $destination = $row->getDestination();
     $this->assertEquals('alt text', $destination['image']['alt']);
     $this->assertEquals(3, $destination['image']['fid']);
     $this->assertEquals('alt text', $row->getDestinationProperty('image/alt'));
     $this->assertEquals(3, $row->getDestinationProperty('image/fid'));
 }