5 * Contains \Drupal\Console\Command\Create\NodesCommand.
8 namespace Drupal\Console\Command\Create;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Command\Shared\CreateTrait;
17 use Drupal\Console\Utils\Create\NodeData;
18 use Drupal\Console\Utils\DrupalApi;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Core\Language\LanguageInterface;
25 * @package Drupal\Console\Command\Generate
27 class NodesCommand extends Command
39 protected $createNodeData;
42 * NodesCommand constructor.
44 * @param DrupalApi $drupalApi
45 * @param NodeData $createNodeData
47 public function __construct(
49 NodeData $createNodeData
51 $this->drupalApi = $drupalApi;
52 $this->createNodeData = $createNodeData;
53 parent::__construct();
59 protected function configure()
62 ->setName('create:nodes')
63 ->setDescription($this->trans('commands.create.nodes.description'))
66 InputArgument::IS_ARRAY,
67 $this->trans('commands.create.nodes.arguments.content-types')
72 InputOption::VALUE_OPTIONAL,
73 $this->trans('commands.create.nodes.options.limit')
78 InputOption::VALUE_OPTIONAL,
79 $this->trans('commands.create.nodes.options.title-words')
84 InputOption::VALUE_OPTIONAL,
85 $this->trans('commands.create.nodes.options.time-range')
90 InputOption::VALUE_OPTIONAL,
91 $this->trans('commands.create.nodes.options.language')
98 protected function interact(InputInterface $input, OutputInterface $output)
100 $io = new DrupalStyle($input, $output);
102 $contentTypes = $input->getArgument('content-types');
103 if (!$contentTypes) {
104 $bundles = $this->drupalApi->getBundles();
105 $contentTypes = $io->choice(
106 $this->trans('commands.create.nodes.questions.content-type'),
107 array_values($bundles),
112 $contentTypes = array_map(
113 function ($contentType) use ($bundles) {
114 return array_search($contentType, $bundles);
119 $input->setArgument('content-types', $contentTypes);
122 $limit = $input->getOption('limit');
125 $this->trans('commands.create.nodes.questions.limit'),
128 $input->setOption('limit', $limit);
131 $titleWords = $input->getOption('title-words');
133 $titleWords = $io->ask(
134 $this->trans('commands.create.nodes.questions.title-words'),
138 $input->setOption('title-words', $titleWords);
141 $timeRange = $input->getOption('time-range');
143 $timeRanges = $this->getTimeRange();
145 $timeRange = $io->choice(
146 $this->trans('commands.create.nodes.questions.time-range'),
147 array_values($timeRanges)
150 $input->setOption('time-range', array_search($timeRange, $timeRanges));
153 // Language module is enabled or not.
154 $language_module_enabled = \Drupal::moduleHandler()->moduleExists('language');
156 // If language module is enabled.
157 if ($language_module_enabled) {
158 // Get available languages on site.
159 $available_languages = \Drupal::languageManager()->getLanguages();
160 // Holds the available languages.
163 foreach ($available_languages as $lang) {
164 $language_list[$lang->getId()] = $lang->getName();
167 $language = $input->getOption('language');
168 // If no language option or invalid language code in option.
169 if (!$language || !array_key_exists($language, $language_list)) {
170 $language = $io->choice(
171 $this->trans('commands.create.nodes.questions.language'),
175 $input->setOption('language', $language);
177 // If 'language' module is not enabled.
178 $input->setOption('language', LanguageInterface::LANGCODE_NOT_SPECIFIED);
185 protected function execute(InputInterface $input, OutputInterface $output)
187 $io = new DrupalStyle($input, $output);
189 $contentTypes = $input->getArgument('content-types');
190 $limit = $input->getOption('limit')?:25;
191 $titleWords = $input->getOption('title-words')?:5;
192 $timeRange = $input->getOption('time-range')?:31536000;
193 $available_types = array_keys($this->drupalApi->getBundles());
194 $language = $input->getOption('language');
196 foreach ($contentTypes as $type) {
197 if (!in_array($type, $available_types)) {
198 throw new \Exception('Invalid content type name given.');
202 if (!$contentTypes) {
203 $contentTypes = $available_types;
206 $nodes = $this->createNodeData->create(
215 $this->trans('commands.create.nodes.messages.node-id'),
216 $this->trans('commands.create.nodes.messages.content-type'),
217 $this->trans('commands.create.nodes.messages.title'),
218 $this->trans('commands.create.nodes.messages.created'),
221 $io->table($tableHeader, $nodes['success']);
225 $this->trans('commands.create.nodes.messages.created-nodes'),