View source
<?php
namespace Drupal\media_migration\Plugin\migrate\source\d7;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\State\StateInterface;
use Drupal\media_migration\FileDealerManagerInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FilePlain extends FieldableEntity implements ContainerFactoryPluginInterface {
use MediaMigrationDatabaseTrait;
protected $sourceHasFileEntities;
protected $fileDealerManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_type_manager, FileDealerManagerInterface $file_dealer_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_type_manager);
$this->sourceHasFileEntities = $this
->getDatabase()
->schema()
->fieldExists('file_managed', 'type');
$this->fileDealerManager = $file_dealer_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('state'), $container
->get('entity_type.manager'), $container
->get('plugin.manager.file_dealer'));
}
public function query() {
[
'mime' => $mime,
'scheme' => $scheme,
] = $this->configuration;
$query = $this
->getFilePlainBaseQuery(NULL, FALSE);
$query
->fields('fm');
$query
->orderBy('fm.timestamp', 'ASC');
if ($this->sourceHasFileEntities) {
$query
->condition('fm.type', [
'undefined',
'',
], 'IN');
}
if ($mime) {
$query
->where("{$this->getMainMimeTypeExpression()} = :mime", [
':mime' => $mime,
]);
}
if ($scheme) {
$query
->where("{$this->getSchemeExpression()} = :scheme", [
':scheme' => $scheme,
]);
}
return $query;
}
protected function prepareQuery() {
parent::prepareQuery();
$this->query
->addTag('migrate__media_migration');
$this->query
->addTag('migrate__media_migration__file_plain');
$this->query
->addTag('migrate__media_migration__media_content');
$this->query
->addTag("migrate__media_migration__source__{$this->pluginId}");
return $this->query;
}
public function prepareRow(Row $row) {
[
'mime' => $mime,
'scheme' => $scheme,
] = $row
->getSource();
if (!($dealer_plugin = $this->fileDealerManager
->createInstanceFromSchemeAndMime($scheme, $mime))) {
return FALSE;
}
$row
->setSourceProperty('bundle', $dealer_plugin
->getDestinationMediaTypeId());
$dealer_plugin
->prepareMediaEntityRow($row, $this
->getDatabase());
return parent::prepareRow($row);
}
public function fields() {
$fields = [
'fid' => $this
->t('The file identifier'),
'source_field_type' => $this
->t('The type of the field where the file is referenced.'),
'uid' => $this
->t('The user identifier'),
'filename' => $this
->t('The file name'),
'uri' => $this
->t('The URI of the file'),
'filemime' => $this
->t('The file mimetype'),
'filesize' => $this
->t('The file size'),
'status' => $this
->t('The file status'),
'timestamp' => $this
->t('The time that the file was added'),
'created' => $this
->t('The created timestamp - (if file_admin module is present in Drupal 7)'),
'published' => $this
->t('The published timestamp - (if file_admin module is present in Drupal 7)'),
'promote' => $this
->t('The promoted flag - (if file_admin module is present in Drupal 7)'),
'sticky' => $this
->t('The sticky flag - (if file_admin module is present in Drupal 7)'),
'vid' => $this
->t('The vid'),
'alt' => $this
->t('The alternate text for the image (if this is a value of an image field)'),
'title' => $this
->t('The title text for the image (if this is a value of an image field)'),
'mime' => $this
->t('The main MIME type of the file'),
'scheme' => $this
->t('The uri scheme of the file (the ID of the stream wrapper)'),
'description' => $this
->t('The file description of the image (if this is a value of a file field)'),
'display' => $this
->t('The display value of the image (if this is a value of a file field)'),
];
return $fields;
}
public function getIds() {
$ids['fid']['type'] = 'integer';
return $ids;
}
}