You are here

public function ConfigEntityHandler::onCreateCdf in Acquia Content Hub 8.2

Creates a new CDF representation of Configuration Entities.

File

src/EventSubscriber/Cdf/ConfigEntityHandler.php, line 76

Class

ConfigEntityHandler
The Configuration entity CDF creator.

Namespace

Drupal\acquia_contenthub\EventSubscriber\Cdf

Code

public function onCreateCdf(CreateCdfEntityEvent $event, string $event_name, EventDispatcherInterface $dispatcher) {
  $entity = $event
    ->getEntity();
  if (!$entity instanceof ConfigEntityInterface) {

    // Bail early if this isn't a config entity.
    return;
  }
  $settings = $this->clientFactory
    ->getSettings();
  $cdf = new CDFObject('drupal8_config_entity', $entity
    ->uuid(), date('c'), date('c'), $settings
    ->getUuid());
  $metadata = [
    'default_language' => $entity
      ->language()
      ->getId(),
  ];
  if ($dependencies = $event
    ->getDependencies()) {
    $metadata['dependencies'] = $dependencies;
  }

  // Some config entities don't have a dependency on their provider module.
  if ($entity
    ->getEntityType()
    ->getProvider() != 'core') {
    if (!empty($metadata['dependencies']['module'])) {
      $metadata['dependencies']['module'] = NestedArray::mergeDeep($metadata['dependencies']['module'], [
        $entity
          ->getEntityType()
          ->getProvider(),
      ]);
    }
    else {
      $metadata['dependencies']['module'][] = $entity
        ->getEntityType()
        ->getProvider();
    }
  }
  $config_data_event = new ConfigDataEvent($entity);
  $dispatcher
    ->dispatch(AcquiaContentHubEvents::SERIALIZE_CONFIG_ENTITY, $config_data_event);
  $metadata['data'] = base64_encode(Yaml::encode($config_data_event
    ->getData()));
  $cdf
    ->setMetadata($metadata);
  $event
    ->addCdf($cdf);
}