diff --git a/drush/default_content.drush.inc b/drush/default_content.drush.inc index 9ea8bd3..5fe92de 100644 --- a/drush/default_content.drush.inc +++ b/drush/default_content.drush.inc @@ -28,8 +28,11 @@ function default_content_drush_command() { 'entity_id' => dt('The ID of the entity to export.'), ], 'options' => [ - 'bundle' => dt('The bundle to export.'), 'folder' => dt('Folder to export to, entities are grouped by entity type into directories.'), + 'bundles' => [ + 'description' => 'A comma-separated list of bundles to limit export to.', + 'example-value' => 'article,page', + ], ], 'aliases' => ['dcer'], 'required-arguments' => 1, @@ -77,15 +80,17 @@ function drush_default_content_export_references($entity_type_id, $entity_id = N $exporter = \Drupal::service('default_content.exporter'); $folder = drush_get_option('folder', '.'); - $bundle = drush_get_option('bundle'); + $bundles = drush_get_option_list('bundles', []); if (is_null($entity_id)) { - $query = \Drupal::entityQuery($entity_type_id); - if (!is_null($bundle)) { - $keys = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getKeys(); + /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */ + $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id); + $query = $storage->getQuery(); + if ($bundles) { + $keys = $storage->getEntityType()->getKeys(); if (empty($keys['bundle'])) { throw new \Exception("The entity type {$entity_type_id} only exposes a single bundle."); } - $query->condition($keys['bundle'], $bundle); + $query->condition($keys['bundle'], $bundles, 'IN'); } $entities = $query->accessCheck(FALSE)->execute(); } diff --git a/src/Commands/DefaultContentCommands.php b/src/Commands/DefaultContentCommands.php index 40c08e3..d4b581d 100644 --- a/src/Commands/DefaultContentCommands.php +++ b/src/Commands/DefaultContentCommands.php @@ -4,6 +4,7 @@ namespace Drupal\default_content\Commands; use Drupal\default_content\ExporterInterface; use Drush\Commands\DrushCommands; +use Drush\Utils\StringUtils; /** * Class DefaultContentCommands. @@ -58,21 +59,28 @@ class DefaultContentCommands extends DrushCommands { * The ID of the entity to export. * * @command default-content:export-references - * @options bundle The bundle to export. - * @options folder Folder to export to, entities are grouped by entity type into directories. + * @option folder Folder to export to, entities are grouped by entity type into directories. + * @option bundles A comma-separated list of bundles to limit export to. * @aliases dcer + * @usage drush dcer node --bundles=article,page + * Exports all content entities of type article and page. */ - public function contentExportReferences($entity_type_id, $entity_id = NULL, $options = ['folder' => NULL]) { + public function contentExportReferences($entity_type_id, $entity_id = NULL, $options = [ + 'folder' => NULL, + 'bundles' => NULL, + ]) { $folder = $options['folder']; - $bundle = $options['bundle']; if (is_null($entity_id)) { - $query = \Drupal::entityQuery($entity_type_id); - if (!is_null($bundle)) { - $keys = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getKeys(); + /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */ + $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id); + $query = $storage->getQuery(); + $bundles = StringUtils::csvToArray($options['bundles']); + if ($bundles) { + $keys = $storage->getEntityType()->getKeys(); if (empty($keys['bundle'])) { throw new \Exception("The entity type {$entity_type_id} only exposes a single bundle."); } - $query->condition($keys['bundle'], $bundle); + $query->condition($keys['bundle'], $bundles, 'IN'); } $entities = $query->accessCheck(FALSE)->execute(); }