public function ImportController::__construct in Term CSV Export Import 8.3
Same name and namespace in other branches
- 8 src/Controller/ImportController.php \Drupal\term_csv_export_import\Controller\ImportController::__construct()
- 8.2 src/Controller/ImportController.php \Drupal\term_csv_export_import\Controller\ImportController::__construct()
File
- src/
Controller/ ImportController.php, line 31
Class
- ImportController
- Class ImportController.
Namespace
Drupal\term_csv_export_import\ControllerCode
public function __construct($data, $vocabulary) {
$this->vocabulary = $vocabulary;
$temp = fopen('php://memory', 'rw');
fwrite($temp, $data);
rewind($temp);
$csvArray = [];
while (!feof($temp)) {
if ($csvRow = fgetcsv($temp)) {
$csvArray[] = $csvRow;
}
}
fclose($temp);
$keys_noid = [
'name',
'status',
'description__value',
'description__format',
'weight',
'parent_name',
];
$keys_id = [
'tid',
'uuid',
'name',
'status',
'revision_id',
'description__value',
'description__format',
'weight',
'parent_name',
'parent_tid',
];
$keys = [];
$may_need_revision = TRUE;
if (!array_diff($keys_noid, $csvArray[0])) {
\Drupal::messenger()
->addWarning(t('The header keys were not included in the import.'));
$keys = $csvArray[0];
if (isset($keys['revision_id'])) {
// This is not an export from an earlier version.
$may_need_revision = FALSE;
}
unset($csvArray[0]);
}
foreach ($csvArray as $csvLine) {
$num_of_lines = count($csvLine);
$needs_revision = FALSE;
if (in_array($num_of_lines, [
9,
10,
11,
]) && ($may_need_revision || empty(trim($csvLine[1])))) {
// Export may have fake or no uuids from d7. generate some that are
// real-ish.
if (empty(trim($csvLine[1])) || strpos($csvLine[1], 'fake_tax_uuid') !== FALSE) {
$uuid_service = \Drupal::service('uuid');
$csvLine[1] = $uuid_service
->generate();
}
// This export may be from an earlier version. Check for revision_id.
if (!is_numeric(trim($csvLine[4])) && in_array($num_of_lines, [
9,
10,
])) {
// The default revision_id in 8.7 is the tid.
array_splice($csvLine, 4, 0, $csvLine[0]);
$needs_revision = TRUE;
$num_of_lines += 1;
}
}
if (empty($keys)) {
if (in_array($num_of_lines, [
10,
11,
])) {
$keys = $keys_id;
}
elseif (in_array($num_of_lines, [
6,
7,
])) {
$keys = $keys_noid;
}
else {
\Drupal::messenger()
->addError(t('Line with "@part" could not be parsed. Incorrect number of values: @count.', [
'@part' => implode(',', $csvLine),
'@count' => count($csvLine),
]));
continue;
}
if (in_array($num_of_lines, [
7,
11,
])) {
$keys[] = 'fields';
}
}
if ($needs_revision && !in_array('revision_id', $keys)) {
array_splice($keys, 4, 0, 'revision_id');
}
$this->data[] = array_combine($keys, $csvLine);
}
}