1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Scenarios hooks implemented by the DF Tools Translation module. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Drupal\Core\Entity\RevisionLogInterface; |
9
|
|
|
use Drupal\Core\TypedData\TypedDataInterface; |
10
|
|
|
use Drupal\migrate\MigrateExecutable; |
11
|
|
|
use Drupal\migrate\MigrateMessage; |
12
|
|
|
use Drupal\migrate\Row; |
13
|
|
|
use Drupal\migrate\Plugin\Migration; |
14
|
|
|
use Drupal\migrate_source_csv\CSVFileObject; |
15
|
|
|
use Drupal\migrate_tools\DrushLogMigrateMessage; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Implements hook_scenarios_migration_finished(). |
19
|
|
|
* |
20
|
|
|
* @todo Can we refactor this to use derived migration plugins? |
21
|
|
|
*/ |
22
|
|
|
function df_tools_translation_scenarios_migration_finished(Migration $migration) { |
23
|
|
|
// The Locale module splits its translation functions into separate include |
24
|
|
|
// files, based on utility. |
25
|
|
|
// To ensure that each function we require is available, load its respective |
26
|
|
|
// include file. |
27
|
|
|
\Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc'); |
28
|
|
|
|
29
|
|
|
// Initialize variables required to parse migration. |
30
|
|
|
/** @var Drupal\migrate_source_csv\Plugin\migrate\source\CSV $source */ |
31
|
|
|
$source = $migration->getSourcePlugin(); |
32
|
|
|
|
33
|
|
|
/** @var Drupal\migrate_source_csv\CSVFileObject $iterator */ |
34
|
|
|
$iterator = $source->initializeIterator(); |
35
|
|
|
$filename = $iterator->getPath() . '/' . $iterator->getFilename(); |
36
|
|
|
|
37
|
|
|
/** @var \Drupal\migrate\Plugin\migrate\destination\EntityContentBase $destination_plugin */ |
38
|
|
|
$destination_plugin = $migration->getDestinationPlugin(); |
39
|
|
|
$entity_type = explode(':', $destination_plugin->getPluginId())[1]; |
40
|
|
|
$storage = \Drupal::entityTypeManager()->getStorage($entity_type); |
41
|
|
|
|
42
|
|
|
// Determine the ID required to lookup entities in this migration. |
43
|
|
|
$id_map = []; |
44
|
|
|
$ids = array_keys($source->getIds()); |
45
|
|
|
$processes = $migration->getProcess(); |
46
|
|
|
|
47
|
|
|
// Prefer UUIDs to other fields. |
48
|
|
|
if (in_array('UUID', $ids, TRUE)) { |
49
|
|
|
$id_map = ['uuid', 'UUID']; |
50
|
|
|
} |
51
|
|
|
else { |
52
|
|
|
foreach ($processes as $field => $process) { |
53
|
|
|
if (isset($process[0]['source']) && $process[0]['source'] == $ids[0]) { |
54
|
|
|
$id_map = [$field, $ids[0]]; |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Get a list of all currently installed languages. |
61
|
|
|
$languageManager = \Drupal::languageManager(); |
62
|
|
|
// We only care about langcodes. |
63
|
|
|
$langcodes = array_keys($languageManager->getLanguages()); |
64
|
|
|
|
65
|
|
|
// Create a migrate executable, which we use later to set row values. |
66
|
|
|
if (function_exists('drush_log')) { |
67
|
|
|
$log = new DrushLogMigrateMessage(); |
68
|
|
|
} |
69
|
|
|
else { |
70
|
|
|
$log = new MigrateMessage(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$executable = new MigrateExecutable($migration, $log); |
74
|
|
|
|
75
|
|
|
// Process translations for each langcode. |
76
|
|
|
foreach ($langcodes as $langcode) { |
77
|
|
|
$new_filename = str_replace('.csv', ".$langcode.csv", $filename); |
78
|
|
|
|
79
|
|
|
if (file_exists($new_filename)) { |
80
|
|
|
$file = df_tools_translation_initialize_csv($new_filename); |
81
|
|
|
$count = $file->count(); |
82
|
|
|
$file->rewind(); |
83
|
|
|
$iterator->rewind(); |
84
|
|
|
|
85
|
|
|
// Update translations for each Entity. |
86
|
|
|
for ($i = 0; $i < $count; ++$i) { |
87
|
|
|
// Get the current row. |
88
|
|
|
$source_row = $iterator->current(); |
89
|
|
|
$row = new Row($file->current(), $source->getIds()); |
|
|
|
|
90
|
|
|
|
91
|
|
|
// Search for the Entity based on its identifier. |
92
|
|
|
$field = $id_map[0]; |
93
|
|
|
|
94
|
|
|
// We use the source file here as a UUID is not always used, and field |
95
|
|
|
// values (title) may be translated into different languages. |
96
|
|
|
$value = $source_row[$id_map[1]]; |
97
|
|
|
$ids = \Drupal::entityQuery($entity_type) |
98
|
|
|
->condition($field, $value) |
99
|
|
|
->execute(); |
100
|
|
|
|
101
|
|
|
// If a migration failed the Entity won't exist, check for that. |
102
|
|
|
if (empty($ids)) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Load the Entity, so we can grab field values and add a translation. |
107
|
|
|
$id = reset($ids); |
|
|
|
|
108
|
|
|
|
109
|
|
|
/** @var Drupal\Core\Entity\ContentEntityBase $entity */ |
110
|
|
|
$entity = $storage->load($id); |
111
|
|
|
|
112
|
|
|
if (!$entity->hasTranslation($langcode)) { |
113
|
|
|
$entity_values = $entity->toArray(); |
114
|
|
|
$translation = $entity->addTranslation($langcode, $entity_values); |
115
|
|
|
} |
116
|
|
|
else { |
117
|
|
|
$translation = $entity->getTranslation($langcode); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Properly set the content translation metadata, if applicable. |
121
|
|
|
if ($translation->hasField('content_translation_source')) { |
122
|
|
|
/** @var \Drupal\content_translation\ContentTranslationManager $manager */ |
123
|
|
|
$manager = \Drupal::service('content_translation.manager'); |
124
|
|
|
$manager->getTranslationMetadata($translation) |
125
|
|
|
->setSource($entity->language()->getId()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Collect a list of process plugins that either have no source, or |
129
|
|
|
// have a source that our translated CSV also has. |
130
|
|
|
$process_plugins = []; |
131
|
|
|
|
132
|
|
|
foreach ($migration->getProcess() as $field_name => $process) { |
133
|
|
|
$untranslatable = ['uuid', 'type', 'path']; |
134
|
|
|
|
135
|
|
|
if (!in_array($field_name, $untranslatable)) { |
136
|
|
|
if (!isset($process[0]['source']) || $row->hasSourceProperty($process[0]['source'])) { |
137
|
|
|
$process_plugins[$field_name] = $process; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
// Process the row, then use its destination values as field values |
142
|
|
|
// for our translation. |
143
|
|
|
$executable->processRow($row, $process_plugins); |
144
|
|
|
|
145
|
|
|
foreach ($row->getDestination() as $field_name => $values) { |
146
|
|
|
$field = $translation->$field_name; |
147
|
|
|
|
148
|
|
|
if ($field instanceof TypedDataInterface) { |
149
|
|
|
$field->setValue($values); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Save the translation. |
154
|
|
|
if ($translation instanceof RevisionLogInterface) { |
155
|
|
|
$translation->setRevisionLogMessage(NULL); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$translation->save(); |
159
|
|
|
|
160
|
|
|
// Move to the next row. |
161
|
|
|
$file->next(); |
162
|
|
|
$iterator->next(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
locale_translate_delete_translation_files(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|