EntityFile.php in Drupal 10
File
core/modules/file/src/Plugin/migrate/destination/EntityFile.php
View source
<?php
namespace Drupal\file\Plugin\migrate\destination;
use Drupal\Core\Field\Plugin\Field\FieldType\UriItem;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
class EntityFile extends EntityContentBase {
protected function getEntity(Row $row, array $old_destination_id_values) {
if ($row
->isStub()) {
return parent::getEntity($row, $old_destination_id_values);
}
$destination = $row
->getDestinationProperty('uri');
if (empty($destination)) {
throw new MigrateException('Destination property uri not provided');
}
$entity = $this->storage
->loadByProperties([
'uri' => $destination,
]);
if ($entity) {
return reset($entity);
}
else {
return parent::getEntity($row, $old_destination_id_values);
}
}
protected function processStubRow(Row $row) {
if (!$row
->getDestinationProperty('uri')) {
$field_definitions = $this->entityFieldManager
->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');
}
$value = reset($value);
$value = 'public://' . preg_replace('|^[a-z]+://|i', '', $value);
$value = mb_substr($value, 0, $field_definitions['uri']
->getSetting('max_length'));
touch($value);
$row
->setDestinationProperty('uri', $value);
}
parent::processStubRow($row);
}
}
Classes
Name |
Description |
EntityFile |
Plugin annotation
@MigrateDestination(
id = "entity:file"
) |