only in patch2: unchanged: --- a/core/modules/file/src/Plugin/migrate/destination/EntityFile.php +++ b/core/modules/file/src/Plugin/migrate/destination/EntityFile.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Field\Plugin\Field\FieldType\UriItem; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\StreamWrapper\LocalStream; use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface; @@ -77,6 +78,12 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ protected function getEntity(Row $row, array $old_destination_id_values) { + // For stub rows, there is no real file to deal with, let the stubbing + // process take its default path. + if ($row->isStub()) { + return parent::getEntity($row, $old_destination_id_values); + } + $destination = $row->getDestinationProperty($this->configuration['destination_path_property']); $entity = $this->storage->loadByProperties(['uri' => $destination]); if ($entity) { @@ -91,6 +98,12 @@ protected function getEntity(Row $row, array $old_destination_id_values) { * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { + // For stub rows, there is no real file to deal with, let the stubbing + // process create the stub entity. + if ($row->isStub()) { + return parent::import($row, $old_destination_id_values); + } + $file = $row->getSourceProperty($this->configuration['source_path_property']); $destination = $row->getDestinationProperty($this->configuration['destination_path_property']); $source = $this->configuration['source_base_path'] . $file; @@ -256,4 +269,30 @@ protected function urlencode($filename) { return $filename; } + /** + * {@inheritdoc} + */ + protected function processStubRow(Row $row) { + parent::processStubRow($row); + // Work-around for https://www.drupal.org/node/2603726 - this function can + // be removed when that's committed. + if (!$row->getDestinationProperty('uri')) { + $field_definitions = \Drupal::entityManager() + ->getFieldDefinitions($this->storage->getEntityTypeId(), + $this->getKey('bundle')); + $value = UriItem::generateSampleValue($field_definitions['uri']); + if (is_null($value)) { + throw new MigrateException($this->t('Stubbing failed, unable to generate value for field @name', + ['@name' => 'uri'])); + } + // generateSampleValue() wraps the value in an array. + $value = reset($value); + // Make it into a proper public file uri. + $value = 'public://' . substr($value, 9); + // Create a real file, so File::preSave() can do filesize() on it. + touch($value); + $row->setDestinationProperty('uri', $value); + } + } + }