You are here

public function ContentEntityHandler::onCreateCdf in Acquia Content Hub 8.2

Creates a new CDF representation of Content Entities.

Parameters

\Drupal\acquia_contenthub\Event\CreateCdfEntityEvent $event: Event.

Throws

\Exception

File

src/EventSubscriber/Cdf/ContentEntityHandler.php, line 102

Class

ContentEntityHandler
The Content entity CDF creator.

Namespace

Drupal\acquia_contenthub\EventSubscriber\Cdf

Code

public function onCreateCdf(CreateCdfEntityEvent $event) {
  $entity = $event
    ->getEntity();
  if (!$entity instanceof ContentEntityInterface) {

    // Bail early if this isn't a content entity.
    return;
  }
  $settings = $this->clientFactory
    ->getSettings();
  $cdf = new CDFObject('drupal8_content_entity', $entity
    ->uuid(), date('c'), date('c'), $settings
    ->getUuid());
  $metadata = [
    'default_language' => $entity
      ->language()
      ->getId(),
  ];
  if ($dependencies = $event
    ->getDependencies()) {
    $metadata['dependencies'] = $dependencies;
  }
  $cdf
    ->setMetadata($metadata);
  $fields = [];
  foreach ($entity as $field_name => $field) {
    $exclude_field_event = new ExcludeEntityFieldEvent($entity, $field_name, $field);
    $this->dispatcher
      ->dispatch(AcquiaContentHubEvents::EXCLUDE_CONTENT_ENTITY_FIELD, $exclude_field_event);
    if ($exclude_field_event
      ->isExcluded()) {
      continue;
    }
    $field_event = new SerializeCdfEntityFieldEvent($entity, $field_name, $field, $cdf);
    $this->dispatcher
      ->dispatch(AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD, $field_event);
    $fields[$field_name] = $field_event
      ->getFieldData();
  }
  $metadata = $cdf
    ->getMetadata();
  $metadata['data'] = base64_encode(json_encode($fields));
  $cdf
    ->setMetadata($metadata);

  // Dispatch an event for final alteration if we want
  // to add additional metadata to the CDF.
  $serialize_additional_metadata_event = new SerializeAdditionalMetadataEvent($entity, $cdf);
  $this->dispatcher
    ->dispatch(AcquiaContentHubEvents::SERIALIZE_ADDITIONAL_METADATA, $serialize_additional_metadata_event);
  $cdf = $serialize_additional_metadata_event
    ->getCdf();
  $event
    ->addCdf($cdf);
}