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 Drupal\Console\Core\Command\Command;
15 use Drupal\Console\Annotations\DrupalCommand;
16 use Drupal\Console\Command\Shared\CreateTrait;
17 use Drupal\Console\Utils\Create\NodeData;
18 use Drupal\Console\Utils\DrupalApi;
19 use Drupal\Core\Language\LanguageInterface;
24 * @package Drupal\Console\Command\Generate
28 * extensionType = "module"
31 class NodesCommand extends Command
42 protected $createNodeData;
45 * NodesCommand constructor.
47 * @param DrupalApi $drupalApi
48 * @param NodeData $createNodeData
50 public function __construct(
52 NodeData $createNodeData
54 $this->drupalApi = $drupalApi;
55 $this->createNodeData = $createNodeData;
56 parent::__construct();
62 protected function configure()
65 ->setName('create:nodes')
66 ->setDescription($this->trans('commands.create.nodes.description'))
69 InputArgument::IS_ARRAY,
70 $this->trans('commands.create.nodes.arguments.content-types')
75 InputOption::VALUE_OPTIONAL,
76 $this->trans('commands.create.nodes.options.limit')
81 InputOption::VALUE_OPTIONAL,
82 $this->trans('commands.create.nodes.options.title-words')
87 InputOption::VALUE_OPTIONAL,
88 $this->trans('commands.create.nodes.options.time-range')
93 InputOption::VALUE_OPTIONAL,
94 $this->trans('commands.create.nodes.options.language')
95 )->setAliases(['crn']);
101 protected function interact(InputInterface $input, OutputInterface $output)
103 $contentTypes = $input->getArgument('content-types');
104 if (!$contentTypes) {
105 $bundles = $this->drupalApi->getBundles();
106 $contentTypes = $this->getIo()->choice(
107 $this->trans('commands.create.nodes.questions.content-type'),
108 array_values($bundles),
113 $contentTypes = array_map(
114 function ($contentType) use ($bundles) {
115 return array_search($contentType, $bundles);
120 $input->setArgument('content-types', $contentTypes);
123 $limit = $input->getOption('limit');
125 $limit = $this->getIo()->ask(
126 $this->trans('commands.create.nodes.questions.limit'),
129 $input->setOption('limit', $limit);
132 $titleWords = $input->getOption('title-words');
134 $titleWords = $this->getIo()->ask(
135 $this->trans('commands.create.nodes.questions.title-words'),
139 $input->setOption('title-words', $titleWords);
142 $timeRange = $input->getOption('time-range');
144 $timeRanges = $this->getTimeRange();
146 $timeRange = $this->getIo()->choice(
147 $this->trans('commands.create.nodes.questions.time-range'),
148 array_values($timeRanges)
151 $input->setOption('time-range', array_search($timeRange, $timeRanges));
154 // Language module is enabled or not.
155 $languageModuleEnabled = \Drupal::moduleHandler()
156 ->moduleExists('language');
158 // If language module is enabled.
159 if ($languageModuleEnabled) {
160 // Get available languages on site.
161 $languages = \Drupal::languageManager()->getLanguages();
162 // Holds the available languages.
165 foreach ($languages as $lang) {
166 $language_list[$lang->getId()] = $lang->getName();
169 $language = $input->getOption('language');
170 // If no language option or invalid language code in option.
171 if (!$language || !array_key_exists($language, $language_list)) {
172 $language = $this->getIo()->choice(
173 $this->trans('commands.create.nodes.questions.language'),
177 $input->setOption('language', $language);
179 // If 'language' module is not enabled.
180 $input->setOption('language', LanguageInterface::LANGCODE_NOT_SPECIFIED);
187 protected function execute(InputInterface $input, OutputInterface $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')?:'und';
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 $result = $this->createNodeData->create(
214 if ($result['success']) {
216 $this->trans('commands.create.nodes.messages.node-id'),
217 $this->trans('commands.create.nodes.messages.content-type'),
218 $this->trans('commands.create.nodes.messages.title'),
219 $this->trans('commands.create.nodes.messages.created'),
222 $this->getIo()->table($tableHeader, $result['success']);
224 $this->getIo()->success(
226 $this->trans('commands.create.nodes.messages.created-nodes'),
227 count($result['success'])
232 if (isset($result['error'])) {
233 foreach ($result['error'] as $error) {
234 $this->getIo()->error(
236 $this->trans('commands.create.nodes.messages.error'),