delineas
7/19/2017 - 4:11 PM

Drush entity list Drupal 8

Drush entity list Drupal 8

<?php


/**
* Implements hook_drush_command().
*/
function MYMODULE_drush_command() {


  $commands['mymodule-entity-list'] = [
    'description' => 'Entity show all entities from entity type',
    'callback' => 'base_drush_show_all_entities',
    'aliases' => ['mymoduleel'],
    'arguments' => [
      'entity' => 'Entity',
    ],
    'examples' => [
      'drush rpapiel site' => 'Show all entities for "site" type',
    ],
  ];

  return $commands;
}

function base_drush_show_all_entities($entity) {
  if(is_null($entity))
    return drush_set_error('DRUSH_FRAMEWORK_ERROR', dt('Entity is not defined'));

  $ids = \Drupal::entityQuery($entity)->execute();
  $storage_handler = \Drupal::entityTypeManager()->getStorage($entity);
  $entities = $storage_handler->loadMultiple($ids);

  $items = [];
  $items[] = implode("\t\t\t",['ID','Label']);
  foreach($entities as $entity_load) {
    $items[] = implode("\t\t\t",[
      $entity_load->id(),
      $entity_load->label(),
    ]);
  }
  drush_print(implode("\n",$items));
}