4 * Contains \Drupal\Console\Command\Create\CommentsCommand.
7 namespace Drupal\Console\Command\Create;
9 use Drupal\Console\Annotations\DrupalCommand;
10 use Drupal\Console\Command\Shared\CreateTrait;
11 use Drupal\Console\Core\Command\Command;
12 use Drupal\Console\Utils\Create\CommentData;
13 use Drupal\node\Entity\Node;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Input\InputOption;
16 use Symfony\Component\Console\Output\OutputInterface;
19 * Class CommentsCommand
21 * @package Drupal\Console\Command\Generate
24 * extension = "comment",
25 * extensionType = "module"
28 class CommentsCommand extends Command
35 protected $createCommentData;
38 * CommentsCommand constructor.
40 * @param CommentData $createCommentData
42 public function __construct(CommentData $createCommentData)
44 $this->createCommentData = $createCommentData;
45 parent::__construct();
51 protected function configure()
54 ->setName('create:comments')
55 ->setDescription($this->trans('commands.create.comments.description'))
58 InputOption::VALUE_REQUIRED,
59 $this->trans('commands.create.comments.arguments.node-id'),
65 InputOption::VALUE_OPTIONAL,
66 $this->trans('commands.create.comments.options.limit')
71 InputOption::VALUE_OPTIONAL,
72 $this->trans('commands.create.comments.options.title-words')
77 InputOption::VALUE_OPTIONAL,
78 $this->trans('commands.create.comments.options.time-range')
79 )->setAliases(['crc']);
85 protected function interact(InputInterface $input, OutputInterface $output)
87 $nodeId = $input->getArgument('node-id');
89 $nodeId = $this->getIo()->ask(
90 $this->trans('commands.create.comments.questions.node-id')
92 $input->setArgument('node-id', $nodeId);
95 $limit = $input->getOption('limit');
97 $limit = $this->getIo()->ask(
98 $this->trans('commands.create.comments.questions.limit'),
101 $input->setOption('limit', $limit);
104 $titleWords = $input->getOption('title-words');
106 $titleWords = $this->getIo()->ask(
107 $this->trans('commands.create.comments.questions.title-words'),
111 $input->setOption('title-words', $titleWords);
114 $timeRange = $input->getOption('time-range');
116 $timeRanges = $this->getTimeRange();
118 $timeRange = $this->getIo()->choice(
119 $this->trans('commands.create.comments.questions.time-range'),
120 array_values($timeRanges)
123 $input->setOption('time-range', array_search($timeRange, $timeRanges));
130 protected function execute(InputInterface $input, OutputInterface $output)
132 $nodeId = $input->getArgument('node-id')?:1;
133 $node = \Drupal\node\Entity\Node::load($nodeId);
135 throw new \InvalidArgumentException(
137 'commands.generate.controller.messages.node-id-invalid'
141 $limit = $input->getOption('limit')?:25;
142 $titleWords = $input->getOption('title-words')?:5;
143 $timeRange = $input->getOption('time-range')?:31536000;
145 $result = $this->createCommentData->create(
152 if ($result['success']) {
155 $this->trans('commands.create.comments.messages.node-id'),
156 $this->trans('commands.create.comments.messages.comment-id'),
157 $this->trans('commands.create.comments.messages.title'),
158 $this->trans('commands.create.comments.messages.created'),
161 $this->getIo()->table($tableHeader, $result['success']);
163 $this->getIo()->success(
165 $this->trans('commands.create.comments.messages.created-comments'),
166 count($result['success'])
171 if (isset($result['error'])) {
172 foreach ($result['error'] as $error) {
173 $this->getIo()->error(
175 $this->trans('commands.create.comments.messages.error'),