dd85863b3efb6b868af11a65f7a0f46ae5962494
[yaffs-website] / vendor / drush / drush / src / Drupal / Commands / core / EntityCommands.php
1 <?php
2
3 namespace Drush\Drupal\Commands\core;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drush\Commands\DrushCommands;
7 use Drush\Utils\StringUtils;
8
9 class EntityCommands extends DrushCommands
10 {
11
12     protected $entityTypeManager;
13
14     /**
15      * EntityCommands constructor.
16      */
17     public function __construct(EntityTypeManagerInterface $entityTypeManager)
18     {
19         $this->entityTypeManager = $entityTypeManager;
20     }
21
22     /**
23      * Delete content entities.
24      *
25      * To delete configuration entities, see config:delete command.
26      *
27      * @param string $entity_type An entity machine name.
28      * @param string $ids A comma delimited list of Ids.
29      * @param array $options
30      *
31      * @option bundle Restrict deletion to the specified bundle. Ignored when ids is specified.
32      * @usage drush entity:delete node --bundle=article
33      *   Delete all article entities.
34      * @usage drush entity:delete shortcut
35      *   Delete all shortcut entities.
36      * @usage drush entity:delete node 22,24
37      *   Delete nodes 22 and 24.
38      *
39      * @command entity:delete
40      * @aliases edel,entity-delete
41      * @throws \Exception
42      */
43     public function delete($entity_type, $ids = null, $options = ['bundle' => self::REQ])
44     {
45         $storage = $this->entityTypeManager->getStorage($entity_type);
46         if ($ids = StringUtils::csvToArray($ids)) {
47             $entities = $storage->loadMultiple($ids);
48         } elseif ($bundle = $options['bundle']) {
49             $bundleKey = $this->entityTypeManager->getDefinition($entity_type)->getKey('bundle');
50             $entities = $storage->loadByProperties([$bundleKey => $bundle]);
51         } else {
52             $entities = $storage->loadMultiple();
53         }
54         if (empty($entities)) {
55             throw new \Exception(dt('No matching entities found.'));
56         }
57         $storage->delete($entities);
58         $this->logger()->success(dt('Deleted !type entity Ids: !ids', ['!type' => $entity_type, '!ids' => implode(', ', array_keys($entities))]));
59     }
60 }