8e54ca29d605f0aed9563ea496563f8e314dd67e
[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['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);
57
58     $vars = &$this->collectVars($input, $output, $questions);
59
60     if ($vars['dependencies']) {
61       $vars['dependencies'] = array_map('trim', explode(',', strtolower($vars['dependencies'])));
62     }
63
64     if ($vars['entity_base_path'][0] != '/') {
65       $vars['entity_base_path'] = '/' . $vars['entity_base_path'];
66     }
67
68     if ($vars['fieldable']) {
69       $vars['configure'] = 'entity.' . $vars['entity_type_id'] . '.settings';
70     }
71
72     $vars['class_prefix'] = Utils::camelize($vars['entity_type_label']);
73
74     $templates = [
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',
85     ];
86
87     if ($vars['fieldable']) {
88       $templates[] = 'src/Form/ExampleSettingsForm.php.twig';
89     }
90
91     if ($vars['template']) {
92       $templates[] = 'templates/model-example.html.twig.twig';
93       $templates[] = 'model.module.twig';
94     }
95     else {
96       $templates[] = 'src/ExampleViewBuilder.php.twig';
97     }
98
99     if ($vars['access_controller']) {
100       $templates[] = 'src/ExampleAccessControlHandler.php.twig';
101     }
102
103     if ($vars['rest_configuration']) {
104       $templates[] = 'config/optional/rest.resource.entity.example.yml.twig';
105     }
106
107     $templates_path = 'd8/module/content-entity/';
108
109     $vars['template_name'] = str_replace('_', '-', $vars['entity_type_id']) . '.html.twig';
110
111     $path_placeholders = [
112       'model-example.html.twig',
113       'model',
114       'Example',
115       'rest.resource.entity.example',
116     ];
117     $path_replacements = [
118       $vars['template_name'],
119       $vars['machine_name'],
120       $vars['class_prefix'],
121       'rest.resource.entity.' . $vars['entity_type_id'],
122     ];
123
124     foreach ($templates as $template) {
125       $path = $vars['machine_name'] . '/' . str_replace($path_placeholders, $path_replacements, $template);
126       $this->addFile()
127         ->path(preg_replace('#\.twig$#', '', $path))
128         ->template($templates_path . $template);
129     }
130   }
131
132 }