5 * Contains \Drupal\Console\Command\Create\TermsCommand.
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\Utils\Create\TermData;
17 use Drupal\Console\Utils\DrupalApi;
22 * @package Drupal\Console\Command\Generate
25 * extension = "taxonomy",
26 * extensionType = "module"
29 class TermsCommand extends Command
38 protected $createTermData;
41 * TermsCommand constructor.
43 * @param DrupalApi $drupalApi
44 * @param TermData $createTermData
46 public function __construct(
48 TermData $createTermData
50 $this->drupalApi = $drupalApi;
51 $this->createTermData = $createTermData;
52 parent::__construct();
58 protected function configure()
61 ->setName('create:terms')
62 ->setDescription($this->trans('commands.create.terms.description'))
65 InputArgument::IS_ARRAY,
66 $this->trans('commands.create.terms.arguments.vocabularies')
71 InputOption::VALUE_OPTIONAL,
72 $this->trans('commands.create.terms.options.limit')
77 InputOption::VALUE_OPTIONAL,
78 $this->trans('commands.create.terms.options.name-words')
79 )->setAliases(['crt']);
85 protected function interact(InputInterface $input, OutputInterface $output)
87 $vocabularies = $input->getArgument('vocabularies');
89 $vocabularies = $this->drupalApi->getVocabularies();
90 $vids = $this->getIo()->choice(
91 $this->trans('commands.create.terms.questions.vocabularies'),
92 array_values($vocabularies),
98 function ($vid) use ($vocabularies) {
99 return array_search($vid, $vocabularies);
104 $input->setArgument('vocabularies', $vids);
107 $limit = $input->getOption('limit');
109 $limit = $this->getIo()->ask(
110 $this->trans('commands.create.terms.questions.limit'),
113 $input->setOption('limit', $limit);
116 $nameWords = $input->getOption('name-words');
118 $nameWords = $this->getIo()->ask(
119 $this->trans('commands.create.terms.questions.name-words'),
123 $input->setOption('name-words', $nameWords);
130 protected function execute(InputInterface $input, OutputInterface $output)
132 $vocabularies = $input->getArgument('vocabularies');
133 $limit = $input->getOption('limit')?:25;
134 $nameWords = $input->getOption('name-words')?:5;
136 if (!$vocabularies) {
137 $vocabularies = array_keys($this->drupalApi->getVocabularies());
140 $result = $this->createTermData->create(
147 $this->trans('commands.create.terms.messages.term-id'),
148 $this->trans('commands.create.terms.messages.vocabulary'),
149 $this->trans('commands.create.terms.messages.name'),
152 if ($result['success']) {
153 $this->getIo()->table($tableHeader, $result['success']);
155 $this->getIo()->success(
157 $this->trans('commands.create.terms.messages.created-terms'),
158 count($result['success'])
163 if (isset($result['error'])) {
164 foreach ($result['error'] as $error) {
165 $this->getIo()->error(
167 $this->trans('commands.create.terms.messages.error'),