You are here

function drush_acquia_contenthub_local in Acquia Content Hub 8

Loads and prints a local entity from Drupal in CDF Format.

Parameters

string $entity_type: Entity type.

string $entity_id: Entity's Uuid of entity's Identifier.

Return value

mixed|false Returns and array containing the CDF of a local entity, FALSE otherwise.

File

./acquia_contenthub.drush.inc, line 238
ContentHub Drush Commands.

Code

function drush_acquia_contenthub_local($entity_type, $entity_id) {
  $entity_type_manager = \Drupal::entityTypeManager();

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');

  /** @var \Drupal\Core\Entity\EntityRepository $entity_repository */
  $entity_repository = \Drupal::service('entity.repository');
  if (empty($entity_type) || empty($entity_id)) {
    return drush_set_error(dt("Missing required parameters: entity_type and entity_id (or entity's uuid)"));
  }
  elseif (!$entity_type_manager
    ->getDefinition($entity_type)) {
    return drush_set_error(dt("Entity type @entity_type does not exist", [
      '@entity_type' => $entity_type,
    ]));
  }
  else {
    if (Uuid::isValid($entity_id)) {
      $entity = $entity_repository
        ->loadEntityByUuid($entity_type, $entity_id);
    }
    else {
      $entity = $entity_type_manager
        ->getStorage($entity_type)
        ->load($entity_id);
    }
  }
  if (!$entity) {
    drush_print(dt("Entity having entity_type = @entity_type and entity_id = @entity_id does not exist.", [
      '@entity_type' => $entity_type,
      '@entity_id' => $entity_id,
    ]));
  }

  // If nothing else, return our object structure.
  $output = $serializer
    ->normalize($entity, 'acquia_contenthub_cdf');
  return $output;
}