4 * Contains \Drupal\Console\Command\User\CreateCommand.
7 namespace Drupal\Console\Command\User;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Database\Connection;
15 use Drupal\Core\Entity\EntityTypeManagerInterface;
16 use Drupal\Core\Datetime\DateFormatterInterface;
17 use Drupal\Console\Utils\DrupalApi;
18 use Drupal\user\Entity\User;
20 class CreateCommand extends Command
29 * @var EntityTypeManagerInterface
31 protected $entityTypeManager;
34 * @var DateFormatterInterface
36 protected $dateFormatter;
44 * CreateCommand constructor.
46 * @param Connection $database
47 * @param EntityTypeManagerInterface $entityTypeManager
48 * @param DateFormatterInterface $dateFormatter
49 * @param DrupalApi $drupalApi
51 public function __construct(
53 EntityTypeManagerInterface $entityTypeManager,
54 DateFormatterInterface $dateFormatter,
57 $this->database = $database;
58 $this->entityTypeManager = $entityTypeManager;
59 $this->dateFormatter = $dateFormatter;
60 $this->drupalApi = $drupalApi;
61 parent::__construct();
67 protected function configure()
70 ->setName('user:create')
71 ->setDescription($this->trans('commands.user.create.description'))
72 ->setHelp($this->trans('commands.user.create.help'))
75 InputArgument::OPTIONAL,
76 $this->trans('commands.user.create.options.username')
80 InputArgument::OPTIONAL,
81 $this->trans('commands.user.create.options.password')
86 InputOption::VALUE_OPTIONAL,
87 $this->trans('commands.user.create.options.roles')
92 InputOption::VALUE_OPTIONAL,
93 $this->trans('commands.user.create.options.email')
98 InputOption::VALUE_OPTIONAL,
99 $this->trans('commands.user.create.options.status')
100 )->setAliases(['uc']);
106 protected function execute(InputInterface $input, OutputInterface $output)
108 $username = $input->getArgument('username');
109 $password = $input->getArgument('password');
110 $roles = $input->getOption('roles');
111 $email = $input->getOption('email');
112 $status = $input->getOption('status');
114 $user = $this->createUser(
122 $tableHeader = ['Field', 'Value'];
125 $this->trans('commands.user.create.messages.user-id'),
126 $this->trans('commands.user.create.messages.username'),
127 $this->trans('commands.user.create.messages.password'),
128 $this->trans('commands.user.create.messages.email'),
129 $this->trans('commands.user.create.messages.roles'),
130 $this->trans('commands.user.create.messages.created'),
131 $this->trans('commands.user.create.messages.status'),
134 if ($user['success']) {
135 $tableData = array_map(
136 function ($field, $value) {
137 return [$field, $value];
143 $this->getIo()->table($tableHeader, $tableData);
145 $this->getIo()->success(
147 $this->trans('commands.user.create.messages.user-created'),
148 $user['success']['username']
155 if ($user['error']) {
156 $this->getIo()->error($user['error']['error']);
165 protected function interact(InputInterface $input, OutputInterface $output)
167 $username = $input->getArgument('username');
169 $username = $this->getIo()->ask(
170 $this->trans('commands.user.create.questions.username')
173 $input->setArgument('username', $username);
176 $password = $input->getArgument('password');
178 $password = $this->getIo()->askEmpty(
179 $this->trans('commands.user.create.questions.password')
182 $input->setArgument('password', $password);
185 $roles = $input->getOption('roles');
187 $systemRoles = $this->drupalApi->getRoles(false, false, false);
188 $roles = $this->getIo()->choice(
189 $this->trans('commands.user.create.questions.roles'),
190 array_values($systemRoles),
196 function ($role) use ($systemRoles) {
197 return array_search($role, $systemRoles);
202 $input->setOption('roles', $roles);
205 $email = $input->getOption('email');
207 $email = $this->getIo()->askEmpty(
208 $this->trans('commands.user.create.questions.email')
211 $input->setOption('email', $email);
214 $status = $input->getOption('status');
216 $status = $this->getIo()->choice(
217 $this->trans('commands.user.create.questions.status'),
222 $input->setOption('status', $status);
226 private function createUser($username, $password, $roles, $email = null, $status = null)
228 $user = User::create(
231 'mail' => $email ?: $username . '@example.com',
232 'pass' => $password?:user_password(),
235 'created' => REQUEST_TIME,
244 $result['success'] = [
245 'user-id' => $user->id(),
246 'username' => $user->getUsername(),
247 'password' => $password,
248 'email' => $user->getEmail(),
249 'roles' => implode(', ', $roles),
250 'created' => $this->dateFormatter->format(
251 $user->getCreatedTime(),
258 } catch (\Exception $e) {
260 'vid' => $user->id(),
261 'name' => $user->get('name'),
262 'error' => 'Error: ' . get_class($e) . ', code: ' . $e->getCode() . ', message: ' . $e->getMessage()