Skip to content

Instantly share code, notes, and snippets.

@delineas
Created July 19, 2017 16:11
Show Gist options
  • Save delineas/2e546768d75bfa2e903c235e98fc1896 to your computer and use it in GitHub Desktop.
Save delineas/2e546768d75bfa2e903c235e98fc1896 to your computer and use it in GitHub Desktop.
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));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment