You are here

public static function TaxonomiesController::importTaxonomiesSafe in Structure Sync 8

Same name and namespace in other branches
  1. 2.x src/Controller/TaxonomiesController.php \Drupal\structure_sync\Controller\TaxonomiesController::importTaxonomiesSafe()

Function to safely import taxonomies.

Safely meaning that it should only add what isn't already there and not delete and/or update any terms.

1 call to TaxonomiesController::importTaxonomiesSafe()
TaxonomiesController::importTaxonomies in src/Controller/TaxonomiesController.php
Function to import taxonomy terms.

File

src/Controller/TaxonomiesController.php, line 513

Class

TaxonomiesController
Controller for syncing taxonomy terms.

Namespace

Drupal\structure_sync\Controller

Code

public static function importTaxonomiesSafe($taxonomies, &$context) {
  $tidsDone = [];
  $tidsLeft = [];
  $newTids = [];
  $firstRun = TRUE;
  $runAgain = FALSE;
  $context['sandbox']['max'] = count($taxonomies);
  $context['sandbox']['progress'] = 0;
  while ($firstRun || count($tidsLeft) > 0) {
    foreach ($taxonomies as $vid => $vocabulary) {
      foreach ($vocabulary as $taxonomy) {
        $query = StructureSyncHelper::getEntityQuery('taxonomy_term');
        $query
          ->condition('vid', $vid);
        $query
          ->condition('name', $taxonomy['name']);
        $tids = $query
          ->execute();
        if (count($tids) <= 0) {
          if (!in_array($taxonomy['tid'], $tidsDone) && ($taxonomy['parent'] === '0' || in_array($taxonomy['parent'], $tidsDone))) {
            if (!in_array($taxonomy['tid'], $tidsDone)) {
              $parent = $taxonomy['parent'];
              if (isset($newTids[$taxonomy['parent']])) {
                $parent = $newTids[$taxonomy['parent']];
              }
              $context['message'] = t('Importing @taxonomy', [
                '@taxonomy' => $taxonomy['name'],
              ]);
              $entity_properties = [
                'vid' => $vid,
                'langcode' => $taxonomy['langcode'],
                'name' => $taxonomy['name'],
                'description' => [
                  'value' => $taxonomy['description__value'],
                  'format' => $taxonomy['description__format'],
                ],
                'weight' => $taxonomy['weight'],
                'parent' => [
                  $parent,
                ],
                'uuid' => $taxonomy['uuid'],
              ];

              // Identify and build array of any custom fields attached to
              // terms.
              $entity_fields = [];
              foreach ($taxonomy as $field_name => $field_value) {
                $is_custom_field = 'field_' === substr($field_name, 0, 6);
                if ($is_custom_field) {
                  $not_term_reference = empty($field_value[0]['vid']);
                  if ($not_term_reference) {
                    $entity_fields[$field_name] = $field_value;
                  }
                  else {
                    foreach ($field_value as $field_properties) {
                      $tid = StructureSyncHelper::getEntityManager()
                        ->getStorage('taxonomy_term')
                        ->getQuery()
                        ->accessCheck(FALSE)
                        ->condition('vid', $field_properties['vid'])
                        ->condition('name', $field_properties['name'])
                        ->execute();
                      if ($tid) {
                        $entity_fields[$field_name][] = [
                          'target_id' => reset($tid),
                        ];
                      }
                      else {

                        // If we encounter a term reference field referencing a
                        // term that hasn't been imported again, trigger re-import
                        // following current import to update term reference
                        // fields once all terms are available.
                        $runAgain = TRUE;
                      }
                    }
                  }
                }
              }
              Term::create($entity_properties + $entity_fields)
                ->save();
              $query = StructureSyncHelper::getEntityQuery('taxonomy_term');
              $query
                ->condition('vid', $vid);
              $query
                ->condition('name', $taxonomy['name']);
              $tids = $query
                ->execute();
              if (count($tids) > 0) {
                $terms = Term::loadMultiple($tids);
              }
              if (isset($terms) && count($terms) > 0) {
                reset($terms);
                $newTid = key($terms);
                $newTids[$taxonomy['tid']] = $newTid;
              }
              $tidsDone[] = $taxonomy['tid'];
              if (in_array($taxonomy['tid'], $tidsLeft)) {
                unset($tidsLeft[array_search($taxonomy['tid'], $tidsLeft)]);
              }
              if (array_key_exists('drush', $context) && $context['drush'] === TRUE) {
                drush_log('Imported "' . $taxonomy['name'] . '" into ' . $vid, 'ok');
              }
              StructureSyncHelper::logMessage('Imported "' . $taxonomy['name'] . '" into ' . $vid);
              $context['sandbox']['progress']++;
              if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
                $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
              }
            }
          }
          else {
            if (!in_array($taxonomy['tid'], $tidsLeft)) {
              $tidsLeft[] = $taxonomy['tid'];
            }
          }
        }
        elseif (!in_array($taxonomy['tid'], $tidsDone)) {
          $query = StructureSyncHelper::getEntityQuery('taxonomy_term');
          $query
            ->condition('vid', $vid);
          $query
            ->condition('name', $taxonomy['name']);
          $tids = $query
            ->execute();
          if (count($tids) > 0) {
            $terms = Term::loadMultiple($tids);
          }
          if (isset($terms) && count($terms) > 0) {
            reset($terms);
            $newTid = key($terms);
            $newTids[$taxonomy['tid']] = $newTid;
            $tidsDone[] = $taxonomy['tid'];
          }
        }
      }
    }
    if ($runAgain) {
      StructureSyncHelper::logMessage('Running additional full import' . ' after all terms have been created in order to identify missing ' . ' TIDs for term reference fields.');
      Self::importTaxonomiesFull($taxonomies, $context);
    }
    $firstRun = FALSE;
  }
  $context['finished'] = 1;
}