You are here

public function ConfigEntityDependencyCollector::onCalculateDependencies in Dependency Calculation 8

Calculates config entity dependencies.

Parameters

\Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event: The dependency calculation event.

string $event_name: The name of the event.

\Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher: The event dispatcher.

Throws

\Exception

File

src/EventSubscriber/DependencyCollector/ConfigEntityDependencyCollector.php, line 55

Class

ConfigEntityDependencyCollector
The config dependency collector.

Namespace

Drupal\depcalc\EventSubscriber\DependencyCollector

Code

public function onCalculateDependencies(CalculateEntityDependenciesEvent $event, string $event_name, EventDispatcherInterface $dispatcher) {
  $entity = $event
    ->getEntity();
  if ($entity instanceof ConfigEntityInterface) {
    $wrapper = $event
      ->getWrapper();
    $dependencies = $event
      ->getDependencies();
    $entity_dependencies = $entity
      ->getDependencies();
    if (isset($entity_dependencies['config'])) {
      $idKey = "{$entity->getEntityType()->getConfigPrefix()}.{$entity->get($entity->getEntityType()->getKey('id'))}";
      $key = array_search($idKey, $entity_dependencies['config']);
      if ($key !== FALSE) {
        unset($entity_dependencies['config'][$key]);
      }
    }
    if (!empty($entity_dependencies['content'])) {

      // @todo figure out how this is stored and iterate over it.
    }

    // Handle config and config entities.
    if (!empty($entity_dependencies['config'])) {
      foreach ($entity_dependencies['config'] as $dependency) {
        $sub_entity = $this->configManager
          ->loadConfigEntityByName($dependency);
        if ($sub_entity) {
          $sub_wrapper = new DependentEntityWrapper($sub_entity);
          $config_dependency_event = new FilterDependencyConfigEntityEvent($sub_wrapper);
          $dispatcher
            ->dispatch(DependencyCalculatorEvents::FILTER_CONFIG_ENTITIES, $config_dependency_event);
          if (!$config_dependency_event
            ->isCalculable()) {
            continue;
          }
          $local_dependencies = [];
          $sub_dependencies = $this
            ->getCalculator()
            ->calculateDependencies($sub_wrapper, $event
            ->getStack(), $local_dependencies);
          unset($sub_dependencies['module']);
          $sub_wrapper
            ->addDependencies($event
            ->getStack(), ...array_values($sub_dependencies));
          $wrapper
            ->addDependency($sub_wrapper, $event
            ->getStack());
        }
        else {
          $dependencies['raw_config'][$dependency] = $this->configManager
            ->getConfigFactory()
            ->get($dependency);
        }
      }
    }
    $event
      ->addDependency($wrapper);
    if (!empty($entity_dependencies['module'])) {
      $event
        ->setModuleDependencies($entity_dependencies['module']);
    }
  }
}