eea0580730e61be2713382da1b10a2d4dac35906
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Command / Drupal_8 / Module / ContentEntity.php
1 <?php
2
3 namespace DrupalCodeGenerator\Command\Drupal_8\Module;
4
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;
11
12 /**
13  * Implements d8:module:content-entity command.
14  */
15 class ContentEntity extends BaseGenerator {
16
17   protected $name = 'd8:module:content-entity';
18   protected $description = 'Generates content entity module';
19   protected $alias = 'content-entity';
20   protected $destination = 'modules';
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function interact(InputInterface $input, OutputInterface $output) {
26     $questions = Utils::defaultQuestions();
27
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}');
31
32     $questions['entity_type_id'] = new Question(
33       'Entity type ID',
34       function ($vars) {
35         return Utils::human2machine($vars['entity_type_label']);
36       }
37     );
38     $questions['entity_base_path'] = new Question(
39       'Entity base path',
40       function ($vars) {
41         return '/admin/content/' . str_replace('_', '-', $vars['entity_type_id']);
42       }
43     );
44
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);
58
59     $vars = &$this->collectVars($input, $output, $questions);
60
61     if ($vars['dependencies']) {
62       $vars['dependencies'] = array_map('trim', explode(',', strtolower($vars['dependencies'])));
63     }
64
65     if ($vars['entity_base_path'][0] != '/') {
66       $vars['entity_base_path'] = '/' . $vars['entity_base_path'];
67     }
68
69     if (($vars['fieldable_no_bundle'] = $vars['fieldable'] && !$vars['bundle'])) {
70       $vars['configure'] = 'entity.' . $vars['entity_type_id'] . '.settings';
71     }
72     elseif ($vars['bundle']) {
73       $vars['configure'] = 'entity.' . $vars['entity_type_id'] . '_type.collection';
74     }
75
76     $vars['class_prefix'] = Utils::camelize($vars['entity_type_label']);
77
78     $templates = [
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',
88     ];
89
90     if ($vars['fieldable_no_bundle']) {
91       $templates[] = 'model.routing.yml.twig';
92       $templates[] = 'src/Form/ExampleSettingsForm.php.twig';
93     }
94
95     if ($vars['template']) {
96       $templates[] = 'templates/model-example.html.twig.twig';
97       $templates[] = 'model.module.twig';
98     }
99     else {
100       $templates[] = 'src/ExampleViewBuilder.php.twig';
101     }
102
103     if ($vars['access_controller']) {
104       $templates[] = 'src/ExampleAccessControlHandler.php.twig';
105     }
106
107     if ($vars['rest_configuration']) {
108       $templates[] = 'config/optional/rest.resource.entity.example.yml.twig';
109     }
110
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';
117     }
118
119     $templates_path = 'd8/module/content-entity/';
120
121     $vars['template_name'] = str_replace('_', '-', $vars['entity_type_id']) . '.html.twig';
122
123     $path_placeholders = [
124       'model-example.html.twig',
125       'model',
126       'Example',
127       'rest.resource.entity.example',
128       'views.view.example',
129     ];
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'],
136     ];
137
138     foreach ($templates as $template) {
139       $path = $vars['machine_name'] . '/' . str_replace($path_placeholders, $path_replacements, $template);
140       $this->addFile()
141         ->path(preg_replace('#\.twig$#', '', $path))
142         ->template($templates_path . $template);
143     }
144   }
145
146 }