3 namespace DrupalCodeGenerator\Command\Drupal_8\Module;
5 use DrupalCodeGenerator\Command\BaseGenerator;
6 use DrupalCodeGenerator\Utils;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Output\OutputInterface;
9 use Symfony\Component\Console\Question\ConfirmationQuestion;
10 use Symfony\Component\Console\Question\Question;
13 * Implements d8:module:content-entity command.
15 class ContentEntity extends BaseGenerator {
17 protected $name = 'd8:module:content-entity';
18 protected $description = 'Generates content entity module';
19 protected $alias = 'content-entity';
20 protected $destination = 'modules';
25 protected function interact(InputInterface $input, OutputInterface $output) {
26 $questions = Utils::defaultQuestions();
28 $questions['package'] = new Question('Package', 'Custom');
29 $questions['dependencies'] = new Question('Dependencies (comma separated)');
30 $questions['entity_type_label'] = new Question('Entity type label', '{name}');
32 $questions['entity_type_id'] = new Question(
35 return Utils::human2machine($vars['entity_type_label']);
38 $questions['entity_base_path'] = new Question(
41 return '/admin/content/' . str_replace('_', '-', $vars['entity_type_id']);
45 $questions['fieldable'] = new ConfirmationQuestion('Make the entity type fieldable?', TRUE);
46 $questions['revisionable'] = new ConfirmationQuestion('Make the entity type revisionable?', FALSE);
47 $questions['translatable'] = new ConfirmationQuestion('Make the entity type translatable?', FALSE);
48 $questions['template'] = new ConfirmationQuestion('Create entity template?', TRUE);
49 $questions['access_controller'] = new ConfirmationQuestion('Create CRUD permissions?', FALSE);
50 $questions['title_base_field'] = new ConfirmationQuestion('Add "title" base field?', TRUE);
51 $questions['status_base_field'] = new ConfirmationQuestion('Add "status" base field?', TRUE);
52 $questions['created_base_field'] = new ConfirmationQuestion('Add "created" base field?', TRUE);
53 $questions['changed_base_field'] = new ConfirmationQuestion('Add "changed" base field?', TRUE);
54 $questions['author_base_field'] = new ConfirmationQuestion('Add "author" base field?', TRUE);
55 $questions['description_base_field'] = new ConfirmationQuestion('Add "description" base field?', TRUE);
56 $questions['rest_configuration'] = new ConfirmationQuestion('Create REST configuration for the entity?', FALSE);
58 $vars = &$this->collectVars($input, $output, $questions);
60 if ($vars['dependencies']) {
61 $vars['dependencies'] = array_map('trim', explode(',', strtolower($vars['dependencies'])));
64 if ($vars['entity_base_path'][0] != '/') {
65 $vars['entity_base_path'] = '/' . $vars['entity_base_path'];
68 if ($vars['fieldable']) {
69 $vars['configure'] = 'entity.' . $vars['entity_type_id'] . '.settings';
72 $vars['class_prefix'] = Utils::camelize($vars['entity_type_label']);
75 'model.info.yml.twig',
76 'model.links.action.yml.twig',
77 'model.links.menu.yml.twig',
78 'model.links.task.yml.twig',
79 'model.permissions.yml.twig',
80 'model.routing.yml.twig',
81 'src/Entity/Example.php.twig',
82 'src/ExampleInterface.php.twig',
83 'src/ExampleListBuilder.php.twig',
84 'src/Form/ExampleForm.php.twig',
87 if ($vars['fieldable']) {
88 $templates[] = 'src/Form/ExampleSettingsForm.php.twig';
91 if ($vars['template']) {
92 $templates[] = 'templates/model-example.html.twig.twig';
93 $templates[] = 'model.module.twig';
96 $templates[] = 'src/ExampleViewBuilder.php.twig';
99 if ($vars['access_controller']) {
100 $templates[] = 'src/ExampleAccessControlHandler.php.twig';
103 if ($vars['rest_configuration']) {
104 $templates[] = 'config/optional/rest.resource.entity.example.yml.twig';
107 $templates_path = 'd8/module/content-entity/';
109 $vars['template_name'] = str_replace('_', '-', $vars['entity_type_id']) . '.html.twig';
111 $path_placeholders = [
112 'model-example.html.twig',
115 'rest.resource.entity.example',
117 $path_replacements = [
118 $vars['template_name'],
119 $vars['machine_name'],
120 $vars['class_prefix'],
121 'rest.resource.entity.' . $vars['entity_type_id'],
124 foreach ($templates as $template) {
125 $path = $vars['machine_name'] . '/' . str_replace($path_placeholders, $path_replacements, $template);
127 ->path(preg_replace('#\.twig$#', '', $path))
128 ->template($templates_path . $template);