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['bundle'] = new ConfirmationQuestion('The entity type has bundle?', FALSE);
49 $questions['template'] = new ConfirmationQuestion('Create entity template?', TRUE);
50 $questions['access_controller'] = new ConfirmationQuestion('Create CRUD permissions?', FALSE);
51 $questions['title_base_field'] = new ConfirmationQuestion('Add "title" base field?', TRUE);
52 $questions['status_base_field'] = new ConfirmationQuestion('Add "status" base field?', TRUE);
53 $questions['created_base_field'] = new ConfirmationQuestion('Add "created" base field?', TRUE);
54 $questions['changed_base_field'] = new ConfirmationQuestion('Add "changed" base field?', TRUE);
55 $questions['author_base_field'] = new ConfirmationQuestion('Add "author" base field?', TRUE);
56 $questions['description_base_field'] = new ConfirmationQuestion('Add "description" base field?', TRUE);
57 $questions['rest_configuration'] = new ConfirmationQuestion('Create REST configuration for the entity?', FALSE);
59 $vars = &$this->collectVars($input, $output, $questions);
61 if ($vars['dependencies']) {
62 $vars['dependencies'] = array_map('trim', explode(',', strtolower($vars['dependencies'])));
65 if ($vars['entity_base_path'][0] != '/') {
66 $vars['entity_base_path'] = '/' . $vars['entity_base_path'];
69 if (($vars['fieldable_no_bundle'] = $vars['fieldable'] && !$vars['bundle'])) {
70 $vars['configure'] = 'entity.' . $vars['entity_type_id'] . '.settings';
72 elseif ($vars['bundle']) {
73 $vars['configure'] = 'entity.' . $vars['entity_type_id'] . '_type.collection';
76 $vars['class_prefix'] = Utils::camelize($vars['entity_type_label']);
79 'model.info.yml.twig',
80 'model.links.action.yml.twig',
81 'model.links.menu.yml.twig',
82 'model.links.task.yml.twig',
83 'model.permissions.yml.twig',
84 'src/Entity/Example.php.twig',
85 'src/ExampleInterface.php.twig',
86 'src/ExampleListBuilder.php.twig',
87 'src/Form/ExampleForm.php.twig',
90 if ($vars['fieldable_no_bundle']) {
91 $templates[] = 'model.routing.yml.twig';
92 $templates[] = 'src/Form/ExampleSettingsForm.php.twig';
95 if ($vars['template']) {
96 $templates[] = 'templates/model-example.html.twig.twig';
97 $templates[] = 'model.module.twig';
100 $templates[] = 'src/ExampleViewBuilder.php.twig';
103 if ($vars['access_controller']) {
104 $templates[] = 'src/ExampleAccessControlHandler.php.twig';
107 if ($vars['rest_configuration']) {
108 $templates[] = 'config/optional/rest.resource.entity.example.yml.twig';
111 if ($vars['bundle']) {
112 $templates[] = 'config/optional/views.view.example.yml.twig';
113 $templates[] = 'config/schema/model.entity_type.schema.yml.twig';
114 $templates[] = 'src/ExampleTypeListBuilder.php.twig';
115 $templates[] = 'src/Entity/ExampleType.php.twig';
116 $templates[] = 'src/Form/ExampleTypeForm.php.twig';
119 $templates_path = 'd8/module/content-entity/';
121 $vars['template_name'] = str_replace('_', '-', $vars['entity_type_id']) . '.html.twig';
123 $path_placeholders = [
124 'model-example.html.twig',
127 'rest.resource.entity.example',
128 'views.view.example',
130 $path_replacements = [
131 $vars['template_name'],
132 $vars['machine_name'],
133 $vars['class_prefix'],
134 'rest.resource.entity.' . $vars['entity_type_id'],
135 'views.view.' . $vars['entity_type_id'],
138 foreach ($templates as $template) {
139 $path = $vars['machine_name'] . '/' . str_replace($path_placeholders, $path_replacements, $template);
141 ->path(preg_replace('#\.twig$#', '', $path))
142 ->template($templates_path . $template);