public function TaxonomiesController::importTaxonomies in Structure Sync 8
Same name and namespace in other branches
- 2.x src/Controller/TaxonomiesController.php \Drupal\structure_sync\Controller\TaxonomiesController::importTaxonomies()
Function to import taxonomy terms.
When this function is used without the designated form, you should assign an array with a key value pair for form with key 'style' and value 'full', 'safe' or 'force' to apply that import style.
File
- src/
Controller/ TaxonomiesController.php, line 163
Class
- TaxonomiesController
- Controller for syncing taxonomy terms.
Namespace
Drupal\structure_sync\ControllerCode
public function importTaxonomies(array $form, FormStateInterface $form_state = NULL) {
StructureSyncHelper::logMessage('Taxonomy import started');
// Check if the import style has been defined in the form (state) and else
// get it from the form array.
if (is_object($form_state) && $form_state
->hasValue('import_voc_list')) {
$taxonomiesSelected = $form_state
->getValue('import_voc_list');
$taxonomiesSelected = array_filter($taxonomiesSelected, 'is_string');
}
if (array_key_exists('style', $form)) {
$style = $form['style'];
}
else {
StructureSyncHelper::logMessage('No style defined on taxonomy import', 'error');
return;
}
StructureSyncHelper::logMessage('Using "' . $style . '" style for taxonomy import');
// Get taxonomies from config.
$taxonomiesConfig = $this->config
->get('taxonomies');
$taxonomies = [];
if (isset($taxonomiesSelected)) {
foreach ($taxonomiesConfig as $taxKey => $taxValue) {
if (in_array($taxKey, $taxonomiesSelected)) {
$taxonomies[$taxKey] = $taxValue;
}
}
}
else {
$taxonomies = $taxonomiesConfig;
}
// Sorts taxonomies so that all parent terms come before -- and therefore
// are created before -- their respective child terms
foreach ($taxonomies as $taxonomy => $terms) {
$parents = [];
foreach ($terms as $key => $term_data) {
$parents[$key] = $term_data['parent'];
}
array_multisort($parents, SORT_ASC, $taxonomies[$taxonomy]);
}
if (array_key_exists('drush', $form) && $form['drush'] === TRUE) {
$context = [];
$context['drush'] = TRUE;
switch ($style) {
case 'full':
self::deleteDeletedTaxonomies($taxonomies, $context);
self::importTaxonomiesFull($taxonomies, $context);
self::taxonomiesImportFinishedCallback(NULL, NULL, NULL);
break;
case 'safe':
self::importTaxonomiesSafe($taxonomies, $context);
self::taxonomiesImportFinishedCallback(NULL, NULL, NULL);
break;
case 'force':
self::deleteTaxonomies($context);
self::importTaxonomiesForce($taxonomies, $context);
self::taxonomiesImportFinishedCallback(NULL, NULL, NULL);
break;
}
return;
}
// Import the taxonomies with the chosen style of importing.
switch ($style) {
case 'full':
$batch = [
'title' => $this
->t('Importing taxonomies...'),
'operations' => [
[
'\\Drupal\\structure_sync\\Controller\\TaxonomiesController::deleteDeletedTaxonomies',
[
$taxonomies,
],
],
[
'\\Drupal\\structure_sync\\Controller\\TaxonomiesController::importTaxonomiesFull',
[
$taxonomies,
],
],
],
'finished' => '\\Drupal\\structure_sync\\Controller\\TaxonomiesController::taxonomiesImportFinishedCallback',
];
batch_set($batch);
break;
case 'safe':
$batch = [
'title' => $this
->t('Importing taxonomies...'),
'operations' => [
[
'\\Drupal\\structure_sync\\Controller\\TaxonomiesController::importTaxonomiesSafe',
[
$taxonomies,
],
],
],
'finished' => '\\Drupal\\structure_sync\\Controller\\TaxonomiesController::taxonomiesImportFinishedCallback',
];
batch_set($batch);
break;
case 'force':
$batch = [
'title' => $this
->t('Importing taxonomies...'),
'operations' => [
[
'\\Drupal\\structure_sync\\Controller\\TaxonomiesController::deleteTaxonomies',
[],
],
[
'\\Drupal\\structure_sync\\Controller\\TaxonomiesController::importTaxonomiesForce',
[
$taxonomies,
],
],
],
'finished' => '\\Drupal\\structure_sync\\Controller\\TaxonomiesController::taxonomiesImportFinishedCallback',
];
batch_set($batch);
break;
default:
StructureSyncHelper::logMessage('Style not recognized', 'error');
break;
}
}