You are here

protected function EntityCdfSerializer::handleModules in Acquia Content Hub 8.2

Ensures all required modules of a set of entities are enabled.

If modules are missing from the code base, this method will throw an exception before any importing of content can occur which should prevent entities from being in half-operational states.

Parameters

\Acquia\ContentHubClient\CDFDocument $cdf: The CDF Document.

\Drupal\depcalc\DependencyStack $stack: The dependency stack.

Throws

\Exception The exception thrown if a module is missing from the code base.

1 call to EntityCdfSerializer::handleModules()
EntityCdfSerializer::unserializeEntities in src/EntityCdfSerializer.php
Unserializes a CDF into a list of Drupal entities.

File

src/EntityCdfSerializer.php, line 215

Class

EntityCdfSerializer
Serialize an entity to a CDF format.

Namespace

Drupal\acquia_contenthub

Code

protected function handleModules(CDFDocument $cdf, DependencyStack $stack) {
  $dependencies = [];
  $unordered_entities = $cdf
    ->getEntities();
  foreach ($unordered_entities as &$entity) {

    // Don't process entities, their dependencies are working.
    if ($stack
      ->hasDependency($entity
      ->getUuid())) {
      continue;
    }

    // Don't process non-entities we've previously processed.
    if ($entity
      ->hasProcessedDependencies()) {
      continue;
    }

    // No need to process entities that don't have module dependencies.
    if (!$entity
      ->getModuleDependencies()) {
      continue;
    }
    $dependencies = NestedArray::mergeDeep($dependencies, $entity
      ->getModuleDependencies());
    $entity
      ->markProcessedDependencies();
  }

  // Check the uniqueness of the module list.
  $dependencies = array_unique($dependencies);
  foreach ($dependencies as $index => $module) {

    // @todo consider a configuration that prevents new module installation.
    // Module isn't installed.
    if (!$this
      ->getModuleHandler()
      ->moduleExists($module)) {

      // Module doesn't exist in the code base, so we can't install.
      if (!drupal_get_filename('module', $module)) {
        throw new \Exception(sprintf("The %s module code base is not present.", $module));
      }
    }
    else {
      unset($dependencies[$index]);
    }
  }
  if (!empty($dependencies)) {
    $this->moduleInstaller
      ->install(array_values($dependencies));
  }
  unset($unordered_entities, $dependencies);

  // @todo determine if this cache invalidation is necessary.
  \Drupal::cache()
    ->invalidateAll();

  // Using \Drupal::entityTypeManager() do to caching of the instance in
  // some services. Looks like a core bug.
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
}