You are here

public function AcquiaContenthubCommands::contenthubList in Acquia Content Hub 8

List entities from the Content Hub using the listEntities() method.

@option limit The number of entities to be listed @option start The offset to start listing the entities (Useful for pagination). @option origin The Client's Origin UUID. @option language The Language that will be used to filter field values. @option attributes The attributes to display for all listed entities @option type The entity type @option filters Filters entities according to a set of of conditions as a key=value pair separated by commas. You could use regex too.

@command acquia:contenthub-list @aliases ach-list,acquia-contenthub-list

Parameters

array $options: An associative array of options whose values come from cli, aliases, config, etc.

File

src/Commands/AcquiaContenthubCommands.php, line 192

Class

AcquiaContenthubCommands
A Drush commandfile.

Namespace

Drupal\acquia_contenthub\Commands

Code

public function contenthubList(array $options = [
  'limit' => NULL,
  'start' => NULL,
  'origin' => NULL,
  'language' => NULL,
  'attributes' => NULL,
  'type' => NULL,
  'filters' => NULL,
]) {

  /** @var \Drupal\acquia_contenthub\Client\ClientManager $client_manager */
  $client_manager = \Drupal::service('acquia_contenthub.client_manager');
  $client = $client_manager
    ->getConnection();
  $list_options = [];

  // Obtaining the limit.
  $limit = $options['limit'];
  if (isset($limit)) {
    $limit = (int) $limit;
    if ($limit < 1 || $limit > 1000) {
      throw new \Exception(dt("The limit has to be an integer from 1 to 1000."));
    }
    else {
      $list_options['limit'] = $limit;
    }
  }

  // Obtaining the offset.
  $start = $options['start'];
  if (isset($start)) {
    if (!is_numeric($start)) {
      throw new \Exception(dt("The start offset has to be numeric starting from 0."));
    }
    else {
      $list_options['start'] = $start;
    }
  }

  // Filtering by origin.
  $origin = $options['origin'];
  if (isset($origin)) {
    if (Uuid::isValid($origin)) {
      $list_options['origin'] = $origin;
    }
    else {
      throw new \Exception(dt("The origin has to be a valid UUID."));
    }
  }

  // Filtering by language.
  $language = $options['language'];
  if (isset($language)) {
    if (strlen($language) == 2) {
      $list_options['language'] = $language;
    }
    else {
      throw new \Exception(dt("The language has to be provided as a 2-letter language code."));
    }
  }

  // Filtering by fields.
  $fields = $options['attributes'];
  if (isset($fields)) {
    $list_options['fields'] = $fields;
  }

  // Filtering by type.
  $type = $options['type'];
  if (isset($type)) {
    $list_options['type'] = $type;
  }

  // Building the filters.
  $filters = $options['filters'];
  if (isset($filters)) {
    $filters = isset($filters) ? explode(",", $filters) : FALSE;
    foreach ($filters as $key => $filter) {
      list($name, $value) = explode("=", $filter);
      $filters[$name] = $value;
      unset($filters[$key]);
    }
    $list_options['filters'] = $filters;
  }
  if ($client_manager
    ->isConnected()) {
    $list = $client_manager
      ->createRequest('listEntities', [
      $list_options,
    ]);
    $this
      ->output()
      ->writeln(print_r($list, TRUE));
    return $list;
  }
  else {
    throw new \Exception(dt('Error trying to connect to the Content Hub. Make sure this site is registered to Content hub.'));
  }
}