Skip to content

Commit d6d2ee0

Browse files
committed
Initial things together; however, going to rework some more.
1 parent a94ac05 commit d6d2ee0

File tree

2 files changed

+128
-17
lines changed

2 files changed

+128
-17
lines changed

modules/dgi_migrate_foxml_standard_mods/migrations/dgis_tn_file.yml

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ process:
6666
- plugin: dgi_migrate.method
6767
source: '@_latest'
6868
method: getUri
69-
_path:
70-
- plugin: format_date
71-
source: '@created'
72-
from_format: U
73-
to_format: 'Y-m'
69+
# _path:
70+
# - plugin: format_date
71+
# source: '@created'
72+
# from_format: U
73+
# to_format: 'Y-m'
7474
filemime:
7575
- plugin: dgi_migrate.subproperty
7676
property: MIMETYPE
@@ -89,19 +89,22 @@ process:
8989
- '@_safe_pid'
9090
- '@_ext'
9191
delimiter: '.'
92-
_dest_uri:
93-
- plugin: concat
94-
source:
95-
- constants/file_dest
96-
- '@_path'
97-
- '@filename'
98-
delimiter: '/'
92+
# _dest_uri:
93+
# - plugin: concat
94+
# source:
95+
# - constants/file_dest
96+
# - '@_path'
97+
# - '@filename'
98+
# delimiter: '/'
99+
# uri:
100+
# - plugin: dgi_migrate.naive_file_copy
101+
# file_exists: rename
102+
# source:
103+
# - '@_source_uri'
104+
# - '@_dest_uri'
99105
uri:
100-
- plugin: dgi_migrate.naive_file_copy
101-
file_exists: rename
102-
source:
103-
- '@_source_uri'
104-
- '@_dest_uri'
106+
- plugin: dgi_migrate.foxml_file
107+
pid:
105108
filesize:
106109
- plugin: callback
107110
source: '@uri'
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Drupal\dgi_migrate\Plugin\migrate\process;
4+
5+
use Drupal\Component\Transliteration\TransliterationInterface;
6+
use Drupal\Core\Language\LanguageInterface;
7+
use Drupal\migrate\MigrateExecutableInterface;
8+
use Drupal\migrate\Plugin\MigrateProcessInterface;
9+
use Drupal\migrate\Plugin\MigrationInterface;
10+
use Drupal\migrate\ProcessPluginBase;
11+
use Drupal\migrate\Row;
12+
use Symfony\Component\DependencyInjection\ContainerInterface;
13+
14+
/**
15+
* General FOXML file handling plugin.
16+
*
17+
* Accepts:
18+
* - method: One of "copy" (to copy the file) or "direct" (to directly use the
19+
* file). Defaults to "copy". Can be set with the DGI_MIGRATE_FILE_METHOD
20+
* environment variable.
21+
* - pid: Property in the row containing the PID, to build out a destination if
22+
* copying.
23+
* - destination: Property in the row containing the destination, to build out
24+
* a destination path if copying.
25+
* - date: Property in the row containing a date with which to build a path in
26+
* the destination, when copying.
27+
* - mimetype; Property in the row containing the MIME type of the file, to
28+
* determine an extension.
29+
*
30+
* @MigrateProcessPlugin(
31+
* id = "dgi_migrate.foxml_file"
32+
* )
33+
*/
34+
class FoxmlFile extends ProcessPluginBase {
35+
36+
/**
37+
* Constructor.
38+
*/
39+
public function __construct(
40+
array $configuration,
41+
$plugin_id,
42+
$plugin_definition,
43+
protected MigrationInterface $migration,
44+
protected TransliterationInterface $transliteration,
45+
protected MigrateProcessInterface $naiveCopyPlugin,
46+
protected MigrateProcessInterface $extensionPlugin,
47+
) {
48+
parent::__construct($configuration, $plugin_id, $plugin_definition);
49+
}
50+
51+
/**
52+
* {@inheritDoc}
53+
*/
54+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
55+
$configuration['method'] ??= getenv('DGI_MIGRATE_FILE_METHOD') ?: 'copy';
56+
assert(in_array($configuration['method'], ['copy', 'direct']));
57+
/** @var \Drupal\migrate\Plugin\MigratePluginManagerInterface $process_plugin_manager */
58+
$process_plugin_manager = $container->get('plugin.manager.migrate.process');
59+
return new static(
60+
$configuration,
61+
$plugin_id,
62+
$plugin_definition,
63+
$migration,
64+
$container->get('transliteration'),
65+
$process_plugin_manager->createInstance('dgi_migrate.naive_file_copy'),
66+
$process_plugin_manager->createInstance('dgi_migrate.process.extension_from_mimetype'),
67+
);
68+
}
69+
70+
/**
71+
* {@inheritDoc}
72+
*/
73+
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
74+
return match ($this->configuration['method']) {
75+
'copy' => $this->naiveCopyPlugin->transform([
76+
$value,
77+
$this->getDestinationPath($migrate_executable, $row, $destination_property),
78+
], $migrate_executable, $row, $destination_property),
79+
'direct' => $value,
80+
};
81+
}
82+
83+
/**
84+
* Build out path.
85+
*/
86+
protected function getDestinationPath(MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) : string {
87+
$pid = $row->get($this->configuration['pid']);
88+
// Adapted from https://api.drupal.org/api/drupal/core%21modules%21migrate%21src%21Plugin%21migrate%21process%21MachineName.php/class/MachineName/10
89+
$safe_pid = preg_replace(
90+
'/_+/',
91+
'_',
92+
preg_replace(
93+
'/[^a-z0-9_]+/',
94+
'_',
95+
strtolower(
96+
$this->transliteration->transliterate($pid, LanguageInterface::LANGCODE_DEFAULT, '_'),
97+
),
98+
)
99+
);
100+
101+
$dest_dir = $row->get($this->configuration['destination']);
102+
$path = date('Y-m', $row->get($this->configuration['date']));
103+
$ext = $this->extensionPlugin->transform($row->get($this->configuration['mimetype']), $migrate_executable, $row, $destination_property);
104+
105+
return "{$dest_dir}/{$path}/{$safe_pid}.{$ext}";
106+
}
107+
108+
}

0 commit comments

Comments
 (0)