/** * {@inheritdoc} */ protected function getEntity(Row $row, array $old_destination_id_values) { if ($row->stub()) { $row->setDestinationProperty('name', $this->t('Stub name for source tid:') . $row->getSourceProperty('tid')); } return parent::getEntity($row, $old_destination_id_values); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { if ($row->stub()) { 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()); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { if ($row->stub() && ($state = $this->state->get('comment.maintain_entity_statistics', 0))) { $this->state->set('comment.maintain_entity_statistics', 0); } $return = parent::import($row, $old_destination_id_values); if ($row->stub() && $state) { $this->state->set('comment.maintain_entity_statistics', $state); } return $return; }
/** * {@inheritdoc} */ public function transform($value, MigrateExecutable $migrate_executable, Row $row, $destination_property) { $migration_ids = $this->configuration['migration']; if (!is_array($migration_ids)) { $migration_ids = array($migration_ids); } $scalar = FALSE; if (!is_array($value)) { $scalar = TRUE; $value = array($value); } $self = FALSE; /** @var \Drupal\migrate\Entity\MigrationInterface[] $migrations */ $migrations = $this->migrationStorage->loadMultiple($migration_ids); $destination_ids = NULL; $source_id_values = array(); foreach ($migrations as $migration_id => $migration) { if ($migration_id == $this->migration->id()) { $self = TRUE; } if (isset($this->configuration['source_ids'][$migration_id])) { $configuration = array('source' => $this->configuration['source_ids'][$migration_id]); $source_id_values[$migration_id] = $this->processPluginManager->createInstance('get', $configuration, $this->migration)->transform(NULL, $migrate_executable, $row, $destination_property); } else { $source_id_values[$migration_id] = $value; } // Break out of the loop as soon as a destination ID is found. if ($destination_ids = $migration->getIdMap()->lookupDestinationID($source_id_values[$migration_id])) { break; } } if (!$destination_ids && ($self && empty($this->configuration['no_stub']) || isset($this->configuration['stub_id']) || count($migrations) == 1)) { // If the lookup didn't succeed, figure out which migration will do the // stubbing. if ($self) { $migration = $this->migration; } elseif (isset($this->configuration['stub_id'])) { $migration = $migrations[$this->configuration['stub_id']]; } else { $migration = reset($migrations); } $destination_plugin = $migration->getDestinationPlugin(); // Only keep the process necessary to produce the destination ID. $process = array_intersect_key($migration->get('process'), $destination_plugin->getIds()); // We already have the source id values but need to key them for the Row // constructor. $source_ids = $migration->getSourcePlugin()->getIds(); $values = array(); foreach (array_keys($source_ids) as $index => $source_id) { $values[$source_id] = $source_id_values[$migration->id()][$index]; } $stub_row = new Row($values, $source_ids); $stub_row->stub(TRUE); // Do a normal migration with the stub row. $migrate_executable->processRow($stub_row, $process); $destination_ids = array(); try { $destination_ids = $destination_plugin->import($stub_row); } catch (MigrateException $e) { } } if ($destination_ids) { if ($scalar) { if (count($destination_ids) == 1) { return reset($destination_ids); } } else { return $destination_ids; } } throw new MigrateSkipRowException(); }