5 * Contains \Drupal\Console\Command\User\DeleteCommand.
8 namespace Drupal\Console\Command\User;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Command\Shared\CommandTrait;
15 use Drupal\Core\Entity\EntityTypeManagerInterface;
16 use Drupal\Core\Entity\Query\QueryFactory;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Utils\DrupalApi;
23 * @package Drupal\Console\Command\User
25 class DeleteCommand extends Command
30 * @var EntityTypeManagerInterface
32 protected $entityTypeManager;
37 protected $entityQuery;
45 * DeleteCommand constructor.
47 * @param EntityTypeManagerInterface $entityTypeManager
48 * @param QueryFactory $entityQuery
49 * @param DrupalApi $drupalApi
51 public function __construct(
52 EntityTypeManagerInterface $entityTypeManager,
53 QueryFactory $entityQuery,
56 $this->entityTypeManager = $entityTypeManager;
57 $this->entityQuery = $entityQuery;
58 $this->drupalApi = $drupalApi;
59 parent::__construct();
65 protected function configure()
68 ->setName('user:delete')
69 ->setDescription($this->trans('commands.user.delete.description'))
73 InputOption::VALUE_OPTIONAL,
74 $this->trans('commands.user.delete.options.user-id')
79 InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
80 $this->trans('commands.user.delete.options.roles')
87 protected function interact(InputInterface $input, OutputInterface $output)
89 $io = new DrupalStyle($input, $output);
91 $userId = $input->getOption('user-id');
93 $userId = $io->askEmpty(
94 $this->trans('commands.user.delete.questions.user-id'),
97 $input->setOption('user-id', $userId);
100 $roles = $input->getOption('roles');
102 if (!$userId && !$roles) {
103 $systemRoles = $this->drupalApi->getRoles(false, false, false);
104 $roles = $io->choice(
105 $this->trans('commands.user.delete.questions.roles'),
106 array_values($systemRoles),
112 function ($role) use ($systemRoles) {
113 return array_search($role, $systemRoles);
118 $input->setOption('roles', $roles);
125 protected function execute(InputInterface $input, OutputInterface $output)
127 $io = new DrupalStyle($input, $output);
129 $userId = $input->getOption('user-id');
131 if ($userId && $userId <= 1) {
134 $this->trans('commands.user.delete.errors.invalid-user-id'),
143 $user = $this->entityTypeManager
150 $this->trans('commands.user.delete.errors.invalid-user'),
162 $this->trans('commands.user.delete.messages.user-deleted'),
166 } catch (\Exception $e) {
167 $io->error($e->getMessage());
173 $roles = $input->getOption('roles');
176 $userStorage = $this->entityTypeManager->getStorage('user');
178 $query = $this->entityQuery->get('user');
179 $query->condition('roles', is_array($roles)?$roles:[$roles], 'IN');
180 $query->condition('uid', 1, '>');
181 $results = $query->execute();
183 $users = $userStorage->loadMultiple($results);
186 $this->trans('commands.user.debug.messages.user-id'),
187 $this->trans('commands.user.debug.messages.username'),
191 foreach ($users as $userId => $user) {
194 $tableRows['success'][] = [$userId, $user->getUsername()];
195 } catch (\Exception $e) {
196 $tableRows['error'][] = [$userId, $user->getUsername()];
197 $io->error($e->getMessage());
203 if ($tableRows['success']) {
204 $io->table($tableHeader, $tableRows['success']);
207 $this->trans('commands.user.delete.messages.users-deleted'),
208 count($tableRows['success'])