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 Drupal\Console\Core\Command\Command;
14 use Drupal\Console\Command\Shared\ModuleTrait;
16 abstract class EntityCommand extends Command
25 protected function setEntityType($entityType)
27 $this->entityType = $entityType;
33 protected function setCommandName($commandName)
35 $this->commandName = $commandName;
41 protected function configure()
43 $commandKey = str_replace(':', '.', $this->commandName);
46 ->setName($this->commandName)
49 $this->trans('commands.'.$commandKey.'.description'),
55 $this->trans('commands.'.$commandKey.'.help'),
60 ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
64 InputOption::VALUE_REQUIRED,
65 $this->trans('commands.'.$commandKey.'.options.entity-class')
70 InputOption::VALUE_REQUIRED,
71 $this->trans('commands.'.$commandKey.'.options.entity-name')
76 InputOption::VALUE_OPTIONAL,
77 $this->trans('commands.' . $commandKey . '.options.base-path')
82 InputOption::VALUE_REQUIRED,
83 $this->trans('commands.'.$commandKey.'.options.label')
87 protected function execute(InputInterface $input, OutputInterface $output)
89 // Operations defined in EntityConfigCommand and EntityContentCommand.
95 protected function interact(InputInterface $input, OutputInterface $output)
97 $commandKey = str_replace(':', '.', $this->commandName);
98 $utils = $this->stringConverter;
101 $this->getModuleOption();
103 // --entity-class option
104 $entityClass = $input->getOption('entity-class');
106 $entityClass = $this->getIo()->ask(
107 $this->trans('commands.'.$commandKey.'.questions.entity-class'),
109 function ($entityClass) {
110 return $this->validator->validateSpaces($entityClass);
114 $input->setOption('entity-class', $entityClass);
117 // --entity-name option
118 $entityName = $input->getOption('entity-name');
120 $entityName = $this->getIo()->ask(
121 $this->trans('commands.'.$commandKey.'.questions.entity-name'),
122 $utils->camelCaseToMachineName($entityClass),
123 function ($entityName) {
124 return $this->validator->validateMachineName($entityName);
127 $input->setOption('entity-name', $entityName);
131 $label = $input->getOption('label');
133 $label = $this->getIo()->ask(
134 $this->trans('commands.'.$commandKey.'.questions.label'),
135 $utils->camelCaseToHuman($entityClass)
137 $input->setOption('label', $label);
140 // --base-path option
141 $base_path = $input->getOption('base-path');
143 $base_path = $this->getDefaultBasePath();
145 $base_path = $this->getIo()->ask(
146 $this->trans('commands.'.$commandKey.'.questions.base-path'),
149 if (substr($base_path, 0, 1) !== '/') {
150 // Base path must start with a leading '/'.
151 $base_path = '/' . $base_path;
153 $input->setOption('base-path', $base_path);
157 * Gets default base path.
162 protected function getDefaultBasePath()
164 return '/admin/structure';