You are here

public static function TaxonomiesController::importTaxonomiesFull in Structure Sync 8

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

Function to fully import the taxonomies.

Basically a safe import with update actions for already existing taxonomy terms.

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

File

src/Controller/TaxonomiesController.php, line 325

Class

TaxonomiesController
Controller for syncing taxonomy terms.

Namespace

Drupal\structure_sync\Controller

Code

public static function importTaxonomiesFull($taxonomies, &$context) {
  $uuidsInConfig = [];
  foreach ($taxonomies as $voc) {
    foreach ($voc as $taxonomy) {
      $uuidsInConfig[] = $taxonomy['uuid'];
    }
  }
  $entities = [];
  if (!empty($uuidsInConfig)) {
    $query = StructureSyncHelper::getEntityQuery('taxonomy_term');
    $query
      ->condition('uuid', $uuidsInConfig, 'IN');
    $tids = $query
      ->execute();
    $controller = StructureSyncHelper::getEntityManager()
      ->getStorage('taxonomy_term');
    $entities = $controller
      ->loadMultiple($tids);
  }
  $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('uuid', $taxonomy['uuid']);
        $tids = $query
          ->execute();
        if (!in_array($taxonomy['tid'], $tidsDone) && ($taxonomy['parent'] === '0' || in_array($taxonomy['parent'], $tidsDone))) {
          $parent = $taxonomy['parent'];
          if (isset($newTids[$taxonomy['parent']])) {
            $parent = $newTids[$taxonomy['parent']];
          }

          // 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;
                  }
                }
              }
            }
          }
          if (count($tids) <= 0) {
            $entity_properties = [
              'vid' => $vid,
              'uuid' => $taxonomy['uuid'],
              'langcode' => $taxonomy['langcode'],
              'name' => $taxonomy['name'],
              'description' => [
                'value' => $taxonomy['description__value'],
                'format' => $taxonomy['description__format'],
              ],
              'weight' => $taxonomy['weight'],
              'parent' => [
                $parent,
              ],
              'uuid' => $taxonomy['uuid'],
            ];
            Term::create($entity_properties + $entity_fields)
              ->save();
          }
          else {
            foreach ($entities as $entity) {
              if ($taxonomy['uuid'] === $entity
                ->uuid()) {
                $term = Term::load($entity
                  ->id());
                if (!empty($term)) {
                  $term->parent = [
                    $parent,
                  ];
                  $term
                    ->setName($taxonomy['name'])
                    ->setDescription($taxonomy['description__value'])
                    ->setFormat($taxonomy['description__format'])
                    ->setWeight($taxonomy['weight']);
                  if ($entity_fields) {
                    foreach ($entity_fields as $field_name => $field_value) {
                      $term->{$field_name}
                        ->setValue($field_value);
                    }
                  }
                  $term
                    ->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'];
          }
        }
      }
    }
    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;
  }
  StructureSyncHelper::logMessage('Flushing all caches');
  if (array_key_exists('drush', $context) && $context['drush'] === TRUE) {
    drush_log('Flushing all caches', 'ok');
  }
  drupal_flush_all_caches();
  StructureSyncHelper::logMessage('Succesfully flushed caches');
  $context['finished'] = 1;
}