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\Annotations\DrupalCommand;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Command\Shared\CreateTrait;
18 use Drupal\Console\Utils\Create\NodeData;
19 use Drupal\Console\Utils\DrupalApi;
20 use Drupal\Console\Core\Style\DrupalStyle;
21 use Drupal\Core\Language\LanguageInterface;
26 * @package Drupal\Console\Command\Generate
30 * extensionType = "module"
33 class NodesCommand extends Command
45 protected $createNodeData;
48 * NodesCommand constructor.
50 * @param DrupalApi $drupalApi
51 * @param NodeData $createNodeData
53 public function __construct(
55 NodeData $createNodeData
57 $this->drupalApi = $drupalApi;
58 $this->createNodeData = $createNodeData;
59 parent::__construct();
65 protected function configure()
68 ->setName('create:nodes')
69 ->setDescription($this->trans('commands.create.nodes.description'))
72 InputArgument::IS_ARRAY,
73 $this->trans('commands.create.nodes.arguments.content-types')
78 InputOption::VALUE_OPTIONAL,
79 $this->trans('commands.create.nodes.options.limit')
84 InputOption::VALUE_OPTIONAL,
85 $this->trans('commands.create.nodes.options.title-words')
90 InputOption::VALUE_OPTIONAL,
91 $this->trans('commands.create.nodes.options.time-range')
96 InputOption::VALUE_OPTIONAL,
97 $this->trans('commands.create.nodes.options.language')
104 protected function interact(InputInterface $input, OutputInterface $output)
106 $io = new DrupalStyle($input, $output);
108 $contentTypes = $input->getArgument('content-types');
109 if (!$contentTypes) {
110 $bundles = $this->drupalApi->getBundles();
111 $contentTypes = $io->choice(
112 $this->trans('commands.create.nodes.questions.content-type'),
113 array_values($bundles),
118 $contentTypes = array_map(
119 function ($contentType) use ($bundles) {
120 return array_search($contentType, $bundles);
125 $input->setArgument('content-types', $contentTypes);
128 $limit = $input->getOption('limit');
131 $this->trans('commands.create.nodes.questions.limit'),
134 $input->setOption('limit', $limit);
137 $titleWords = $input->getOption('title-words');
139 $titleWords = $io->ask(
140 $this->trans('commands.create.nodes.questions.title-words'),
144 $input->setOption('title-words', $titleWords);
147 $timeRange = $input->getOption('time-range');
149 $timeRanges = $this->getTimeRange();
151 $timeRange = $io->choice(
152 $this->trans('commands.create.nodes.questions.time-range'),
153 array_values($timeRanges)
156 $input->setOption('time-range', array_search($timeRange, $timeRanges));
159 // Language module is enabled or not.
160 $languageModuleEnabled = \Drupal::moduleHandler()
161 ->moduleExists('language');
163 // If language module is enabled.
164 if ($languageModuleEnabled) {
165 // Get available languages on site.
166 $languages = \Drupal::languageManager()->getLanguages();
167 // Holds the available languages.
170 foreach ($languages as $lang) {
171 $language_list[$lang->getId()] = $lang->getName();
174 $language = $input->getOption('language');
175 // If no language option or invalid language code in option.
176 if (!$language || !array_key_exists($language, $language_list)) {
177 $language = $io->choice(
178 $this->trans('commands.create.nodes.questions.language'),
182 $input->setOption('language', $language);
184 // If 'language' module is not enabled.
185 $input->setOption('language', LanguageInterface::LANGCODE_NOT_SPECIFIED);
192 protected function execute(InputInterface $input, OutputInterface $output)
194 $io = new DrupalStyle($input, $output);
196 $contentTypes = $input->getArgument('content-types');
197 $limit = $input->getOption('limit')?:25;
198 $titleWords = $input->getOption('title-words')?:5;
199 $timeRange = $input->getOption('time-range')?:31536000;
200 $available_types = array_keys($this->drupalApi->getBundles());
201 $language = $input->getOption('language')?:'und';
203 foreach ($contentTypes as $type) {
204 if (!in_array($type, $available_types)) {
205 throw new \Exception('Invalid content type name given.');
209 if (!$contentTypes) {
210 $contentTypes = $available_types;
213 $nodes = $this->createNodeData->create(
221 $nodes = is_array($nodes) ? $nodes : [$nodes];
224 $this->trans('commands.create.nodes.messages.node-id'),
225 $this->trans('commands.create.nodes.messages.content-type'),
226 $this->trans('commands.create.nodes.messages.title'),
227 $this->trans('commands.create.nodes.messages.created'),
230 $io->table($tableHeader, $nodes['success']);
234 $this->trans('commands.create.nodes.messages.created-nodes'),