View source
<?php
namespace Drupal\media_migration;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldConfigInterface;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\media\MediaSourceInterface;
use Drupal\media_migration\Plugin\migrate\source\d7\ConfigSourceBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class MediaDealerBase extends PluginBase implements ContainerFactoryPluginInterface {
protected $mediaSourceManager;
protected $fieldTypeManager;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, PluginManagerInterface $media_source_manager, FieldTypePluginManagerInterface $field_type_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->mediaSourceManager = $media_source_manager;
$this->fieldTypeManager = $field_type_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.media.source'), $container
->get('plugin.manager.field.field_type'), $container
->get('entity_type.manager'));
}
public function getDestinationMediaTypeIdBase() {
return $this->pluginDefinition['destination_media_type_id_base'];
}
public function getDestinationMediaTypeId() {
return implode('_', array_filter([
$this
->getDestinationMediaTypeIdBase(),
$this->configuration['scheme'] === 'public' ? NULL : $this->configuration['scheme'],
]));
}
public function getDestinationMediaTypeLabel() {
return implode(' ', array_filter([
$this
->getDestinationMediaTypeSourceFieldLabel(),
$this->configuration['scheme'] === 'public' ? NULL : "({$this->configuration['scheme']})",
]));
}
public function getDestinationMediaTypeSourceFieldLabel() {
return ucfirst(preg_replace('/[\\W|_]+/', ' ', strtolower($this
->getDestinationMediaTypeIdBase())));
}
public function getDestinationMediaSourceFieldName() {
return implode('_', array_filter([
'field',
'media',
str_replace(':', '_', $this
->getDestinationMediaSourcePluginId()),
$this->configuration['scheme'] === 'public' ? NULL : $this->configuration['scheme'],
]));
}
public function getDestinationMediaSourcePluginId() {
return $this->pluginDefinition['destination_media_source_plugin_id'];
}
public function alterMediaTypeMigrationDefinition(array &$migration_definition, Connection $connection) : void {
}
public function alterMediaSourceFieldStorageMigrationDefinition(array &$migration_definition, Connection $connection) : void {
}
public function alterMediaSourceFieldInstanceMigrationDefinition(array &$migration_definition, Connection $connection) : void {
}
public function alterMediaSourceFieldWidgetMigrationDefinition(array &$migration_definition, Connection $connection) : void {
}
public function alterMediaFieldFormatterMigrationDefinition(array &$migration_definition, Connection $connection) : void {
}
public function alterMediaEntityMigrationDefinition(array &$migration_definition, Connection $connection) : void {
}
public function prepareMediaSourceFieldStorageRow(Row $row, Connection $connection) : void {
$dummy_field_storage = $this
->getMediaSourceFieldStorage();
$additional_properties = [
'field_type' => $dummy_field_storage
->getType(),
'settings' => $dummy_field_storage
->getSettings(),
];
$additional_properties['settings']['uri_scheme'] = $this->configuration['scheme'];
foreach ($additional_properties as $source_property => $source_value) {
$row
->setSourceProperty($source_property, $source_value);
}
}
public function prepareMediaSourceFieldInstanceRow(Row $row, Connection $connection) : void {
$field_instance_default = $this
->getMediaSourceFieldInstance();
$settings = $field_instance_default
->getSettings();
$default_extensions = $settings['file_extensions'] ?? '';
$discovered_extensions = $row
->getSourceProperty('file_extensions') ?? '';
$merged_file_extensions = implode(' ', array_filter(array_unique(array_merge(explode(' ', $default_extensions), explode(ConfigSourceBase::MULTIPLE_SEPARATOR, $discovered_extensions)))));
$settings['file_extensions'] = $merged_file_extensions;
$row
->setSourceProperty('settings', $settings);
}
public function prepareMediaSourceFieldWidgetRow(Row $row, Connection $connection) : void {
$source_field_definition = $this->fieldTypeManager
->getDefinition($this
->getMediaSourceFieldStorage()
->getType(), FALSE) ?? [];
$default_widget = $source_field_definition['default_widget'] ?? NULL;
if ($default_widget) {
$row
->setSourceProperty('options', [
'type' => $default_widget,
'weight' => 0,
]);
}
}
public function prepareMediaSourceFieldFormatterRow(Row $row, Connection $connection) : void {
$source_field_definition = $this->fieldTypeManager
->getDefinition($this
->getMediaSourceFieldStorage()
->getType(), FALSE) ?? [];
$default_formatter = $source_field_definition['default_formatter'] ?? NULL;
if ($default_formatter) {
$row
->setSourceProperty('options', [
'type' => $default_formatter,
'weight' => 0,
'label' => 'visually_hidden',
]);
}
}
public function prepareMediaTypeRow(Row $row, Connection $connection) : void {
}
public function prepareMediaEntityRow(Row $row, Connection $connection) : void {
}
protected function getMediaSourceFieldInstance() {
$preexisting_field_instance = $this->entityTypeManager
->getStorage('field_config')
->load(implode('.', [
'media',
$this
->getDestinationMediaTypeId(),
$this
->getDestinationMediaSourceFieldName(),
]));
if ($preexisting_field_instance) {
assert($preexisting_field_instance instanceof FieldConfigInterface);
return $preexisting_field_instance;
}
if (!($storage = $this
->getMediaSourceFieldStorage())) {
return NULL;
}
$field_config = FieldConfig::create([
'field_storage' => $storage,
'bundle' => $this
->getDestinationMediaTypeId(),
'label' => $this
->getDestinationMediaTypeLabel(),
'required' => TRUE,
]);
if (!$field_config instanceof FieldConfigInterface) {
return NULL;
}
$default_settings = $this->fieldTypeManager
->getDefaultFieldSettings($field_config
->getType());
$extensions = explode(' ', $default_settings['file_extensions'] ?? '');
switch ($this
->getDestinationMediaSourcePluginId()) {
case 'audio_file':
$extensions = [
'mp3',
'wav',
'aac',
];
break;
case 'image':
break;
case 'video_file':
$extensions = [
'mp4',
];
break;
case 'file':
$extensions = [
'txt',
'doc',
'docx',
'pdf',
];
break;
}
$default_settings['file_extensions'] = implode(' ', $extensions);
$field_config
->set('settings', $default_settings);
return $field_config;
}
protected function getMediaSourceFieldStorage() {
$source_field_name = $this
->getDestinationMediaSourceFieldName();
$preexisting_field_storage = $this->entityTypeManager
->getStorage('field_storage_config')
->load(implode('.', [
'media',
$source_field_name,
]));
if ($preexisting_field_storage) {
assert($preexisting_field_storage instanceof FieldStorageConfigInterface);
return $preexisting_field_storage;
}
$source_plugin_id = $this
->getDestinationMediaSourcePluginId();
try {
$media_source_plugin = $this->mediaSourceManager
->createInstance($source_plugin_id);
} catch (PluginException $e) {
return NULL;
}
assert($media_source_plugin instanceof MediaSourceInterface);
$source_plugin_definition = $media_source_plugin
->getPluginDefinition();
$field_type = reset($source_plugin_definition['allowed_field_types']);
$field_storage = FieldStorageConfig::create([
'entity_type' => 'media',
'field_name' => $source_field_name,
'type' => $field_type,
]);
assert($field_storage instanceof FieldStorageConfigInterface);
$field_storage
->set('settings', $this->fieldTypeManager
->getDefaultStorageSettings($field_storage
->getType()));
return $field_storage;
}
protected function getImageFieldData(Connection $connection, $field_names_only = TRUE) : array {
$image_field_query = $connection
->select('field_config', 'fs')
->fields('fs', [
'field_name',
])
->condition('fs.type', 'image')
->condition('fs.active', 1)
->condition('fs.deleted', 0)
->condition('fs.storage_active', 1)
->condition('fi.deleted', 0);
$image_field_query
->join('field_config_instance', 'fi', 'fs.id = fi.field_id');
if ($field_names_only) {
return array_keys($image_field_query
->execute()
->fetchAllAssoc('field_name'));
}
$image_field_query
->addField('fs', 'data', 'field_storage_data');
$image_field_query
->addField('fi', 'data', 'field_instance_data');
$image_fields_data = [];
foreach ($image_field_query
->execute()
->fetchAll(\PDO::FETCH_ASSOC) as $item) {
foreach ([
'field_storage_data',
'field_instance_data',
] as $data_key) {
$item[$data_key] = unserialize($item[$data_key]);
}
$image_fields_data[] = $item;
}
return $image_fields_data;
}
protected function getImageData(Connection $connection, $file_id) : array {
foreach ($this
->getImageFieldData($connection) as $field_name) {
$field_table_name = "field_data_{$field_name}";
$data_query = $connection
->select($field_table_name, $field_name);
$data_query
->addField($field_name, "{$field_name}_alt", 'alt');
$data_query
->addField($field_name, "{$field_name}_title", 'title');
$data_query
->addField($field_name, "{$field_name}_height", 'height');
$data_query
->addField($field_name, "{$field_name}_width", 'width');
$data_query
->condition("{$field_name}_fid", $file_id);
if (!empty($results = $data_query
->execute()
->fetchAll(\PDO::FETCH_ASSOC))) {
$result = reset($results);
return array_filter($result);
}
}
return [];
}
}