5 * Contains \Drupal\Console\Command\Generate\EntityCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Command\Shared\ModuleTrait;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
18 abstract class EntityCommand extends Command
28 protected function setEntityType($entityType)
30 $this->entityType = $entityType;
36 protected function setCommandName($commandName)
38 $this->commandName = $commandName;
44 protected function configure()
46 $commandKey = str_replace(':', '.', $this->commandName);
49 ->setName($this->commandName)
52 $this->trans('commands.'.$commandKey.'.description'),
58 $this->trans('commands.'.$commandKey.'.help'),
63 ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
67 InputOption::VALUE_REQUIRED,
68 $this->trans('commands.'.$commandKey.'.options.entity-class')
73 InputOption::VALUE_REQUIRED,
74 $this->trans('commands.'.$commandKey.'.options.entity-name')
79 InputOption::VALUE_OPTIONAL,
80 $this->trans('commands.' . $commandKey . '.options.base-path')
85 InputOption::VALUE_REQUIRED,
86 $this->trans('commands.'.$commandKey.'.options.label')
90 protected function execute(InputInterface $input, OutputInterface $output)
92 // Operations defined in EntityConfigCommand and EntityContentCommand.
98 protected function interact(InputInterface $input, OutputInterface $output)
100 $io = new DrupalStyle($input, $output);
102 $commandKey = str_replace(':', '.', $this->commandName);
103 $utils = $this->stringConverter;
106 $module = $input->getOption('module');
108 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
109 $module = $this->moduleQuestion($io);
110 $input->setOption('module', $module);
113 // --entity-class option
114 $entityClass = $input->getOption('entity-class');
116 $entityClass = $io->ask(
117 $this->trans('commands.'.$commandKey.'.questions.entity-class'),
119 function ($entityClass) {
120 return $this->validator->validateSpaces($entityClass);
124 $input->setOption('entity-class', $entityClass);
127 // --entity-name option
128 $entityName = $input->getOption('entity-name');
130 $entityName = $io->ask(
131 $this->trans('commands.'.$commandKey.'.questions.entity-name'),
132 $utils->camelCaseToMachineName($entityClass),
133 function ($entityName) {
134 return $this->validator->validateMachineName($entityName);
137 $input->setOption('entity-name', $entityName);
141 $label = $input->getOption('label');
144 $this->trans('commands.'.$commandKey.'.questions.label'),
145 $utils->camelCaseToHuman($entityClass)
147 $input->setOption('label', $label);
150 // --base-path option
151 $base_path = $input->getOption('base-path');
153 $base_path = $this->getDefaultBasePath();
155 $base_path = $io->ask(
156 $this->trans('commands.'.$commandKey.'.questions.base-path'),
159 if (substr($base_path, 0, 1) !== '/') {
160 // Base path must start with a leading '/'.
161 $base_path = '/' . $base_path;
163 $input->setOption('base-path', $base_path);
167 * Gets default base path.
172 protected function getDefaultBasePath()
174 return '/admin/structure';