Skip to content

Commit

Permalink
Initial things together; however, going to rework some more.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Sep 17, 2024
1 parent a94ac05 commit d6d2ee0
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 17 deletions.
37 changes: 20 additions & 17 deletions modules/dgi_migrate_foxml_standard_mods/migrations/dgis_tn_file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ process:
- plugin: dgi_migrate.method
source: '@_latest'
method: getUri
_path:
- plugin: format_date
source: '@created'
from_format: U
to_format: 'Y-m'
# _path:
# - plugin: format_date
# source: '@created'
# from_format: U
# to_format: 'Y-m'
filemime:
- plugin: dgi_migrate.subproperty
property: MIMETYPE
Expand All @@ -89,19 +89,22 @@ process:
- '@_safe_pid'
- '@_ext'
delimiter: '.'
_dest_uri:
- plugin: concat
source:
- constants/file_dest
- '@_path'
- '@filename'
delimiter: '/'
# _dest_uri:
# - plugin: concat
# source:
# - constants/file_dest
# - '@_path'
# - '@filename'
# delimiter: '/'
# uri:
# - plugin: dgi_migrate.naive_file_copy
# file_exists: rename
# source:
# - '@_source_uri'
# - '@_dest_uri'
uri:
- plugin: dgi_migrate.naive_file_copy
file_exists: rename
source:
- '@_source_uri'
- '@_dest_uri'
- plugin: dgi_migrate.foxml_file
pid:
filesize:
- plugin: callback
source: '@uri'
Expand Down
108 changes: 108 additions & 0 deletions src/Plugin/migrate/process/FoxmlFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Drupal\dgi_migrate\Plugin\migrate\process;

use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* General FOXML file handling plugin.
*
* Accepts:
* - method: One of "copy" (to copy the file) or "direct" (to directly use the
* file). Defaults to "copy". Can be set with the DGI_MIGRATE_FILE_METHOD
* environment variable.
* - pid: Property in the row containing the PID, to build out a destination if
* copying.
* - destination: Property in the row containing the destination, to build out
* a destination path if copying.
* - date: Property in the row containing a date with which to build a path in
* the destination, when copying.
* - mimetype; Property in the row containing the MIME type of the file, to
* determine an extension.
*
* @MigrateProcessPlugin(
* id = "dgi_migrate.foxml_file"
* )
*/
class FoxmlFile extends ProcessPluginBase {

/**
* Constructor.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected MigrationInterface $migration,
protected TransliterationInterface $transliteration,
protected MigrateProcessInterface $naiveCopyPlugin,
protected MigrateProcessInterface $extensionPlugin,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$configuration['method'] ??= getenv('DGI_MIGRATE_FILE_METHOD') ?: 'copy';
assert(in_array($configuration['method'], ['copy', 'direct']));
/** @var \Drupal\migrate\Plugin\MigratePluginManagerInterface $process_plugin_manager */
$process_plugin_manager = $container->get('plugin.manager.migrate.process');
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('transliteration'),
$process_plugin_manager->createInstance('dgi_migrate.naive_file_copy'),
$process_plugin_manager->createInstance('dgi_migrate.process.extension_from_mimetype'),
);
}

/**
* {@inheritDoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
return match ($this->configuration['method']) {
'copy' => $this->naiveCopyPlugin->transform([
$value,
$this->getDestinationPath($migrate_executable, $row, $destination_property),
], $migrate_executable, $row, $destination_property),
'direct' => $value,
};
}

/**
* Build out path.
*/
protected function getDestinationPath(MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) : string {
$pid = $row->get($this->configuration['pid']);
// Adapted from https://api.drupal.org/api/drupal/core%21modules%21migrate%21src%21Plugin%21migrate%21process%21MachineName.php/class/MachineName/10
$safe_pid = preg_replace(
'/_+/',
'_',
preg_replace(
'/[^a-z0-9_]+/',
'_',
strtolower(
$this->transliteration->transliterate($pid, LanguageInterface::LANGCODE_DEFAULT, '_'),
),
)
);

$dest_dir = $row->get($this->configuration['destination']);
$path = date('Y-m', $row->get($this->configuration['date']));
$ext = $this->extensionPlugin->transform($row->get($this->configuration['mimetype']), $migrate_executable, $row, $destination_property);

return "{$dest_dir}/{$path}/{$safe_pid}.{$ext}";
}

}

0 comments on commit d6d2ee0

Please sign in to comment.