4 * Contains \Drupal\Console\Command\Role\DeleteCommand.
7 namespace Drupal\Console\Command\Role;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Drupal\Console\Core\Command\Command;
13 use Drupal\Core\Database\Connection;
14 use Drupal\Core\Entity\EntityTypeManagerInterface;
15 use Drupal\Core\Datetime\DateFormatterInterface;
16 use Drupal\Console\Utils\DrupalApi;
17 use Drupal\Console\Utils\Validator;
18 use Drupal\Console\Command\Shared\ConfirmationTrait;
20 class DeleteCommand extends Command
22 use ConfirmationTrait;
30 * @var EntityTypeManagerInterface
32 protected $entityTypeManager;
35 * @var DateFormatterInterface
37 protected $dateFormatter;
50 * DeleteCommand constructor.
52 * @param Connection $database
53 * @param EntityTypeManagerInterface $entityTypeManager
54 * @param DateFormatterInterface $dateFormatter
55 * @param DrupalApi $drupalApi
56 * @param Validator $validator
58 public function __construct(
60 EntityTypeManagerInterface $entityTypeManager,
61 DateFormatterInterface $dateFormatter,
65 $this->database = $database;
66 $this->entityTypeManager = $entityTypeManager;
67 $this->dateFormatter = $dateFormatter;
68 $this->drupalApi = $drupalApi;
69 $this->validator = $validator;
70 parent::__construct();
76 protected function configure()
79 ->setName('role:delete')
80 ->setDescription($this->trans('commands.role.delete.description'))
81 ->setHelp($this->trans('commands.role.delete.help'))
84 InputArgument::IS_ARRAY,
85 $this->trans('commands.role.delete.argument.roles')
86 )->setAliases(['rd']);
92 protected function execute(InputInterface $input, OutputInterface $output)
94 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
95 if (!$this->confirmOperation()) {
99 $roles = $input->getArgument('roles');
100 foreach ($roles as $roleItem) {
101 $this->validator->validateRoleExistence($roleItem, $this->drupalApi->getRoles());
104 $role = $this->deleteRole($roles);
107 $this->trans('commands.role.delete.messages.role-id'),
108 $this->trans('commands.role.delete.messages.role-name'),
111 if ($role['success']) {
112 $this->getIo()->success(
114 $this->trans('commands.role.delete.messages.role-created')
118 $this->getIo()->table($tableHeader, $role['success']);
123 if ($role['error']) {
124 $this->getIo()->error($role['error']['error']);
133 protected function interact(InputInterface $input, OutputInterface $output)
135 $rolename = $input->getArgument('roles');
137 $roles_collection = [];
138 $siteRoles = $this->drupalApi->getRoles();
139 $roles = array_keys($siteRoles);
140 $this->getIo()->writeln($this->trans('commands.common.questions.roles.message'));
142 $role = $this->getIo()->choiceNoList(
143 $this->trans('commands.common.questions.roles.name'),
149 if (empty($role) || is_numeric($role)) {
153 if (!array_key_exists($role, $siteRoles)) {
154 $this->getIo()->error(sprintf(
155 $this->trans('commands.role.delete.messages.invalid-machine-name'),
161 array_push($roles_collection, $role);
162 $role_key = array_search($role, $roles, true);
163 if ($role_key >= 0) {
164 unset($roles[$role_key]);
168 $input->setArgument('roles', $roles_collection);
173 * Remove and returns an array of deleted roles
179 private function deleteRole($roles)
183 foreach ($roles as $value) {
184 $role = $this->entityTypeManager->getStorage('user_role')->load($value);
185 $this->entityTypeManager->getStorage('user_role')->delete([$role]);
187 $result['success'][] = [
189 'role-name' => $value
192 } catch (\Exception $e) {
194 'error' => 'Error: ' . get_class($e) . ', code: ' . $e->getCode() . ', message: ' . $e->getMessage()