d93640dd9985d55226cff007da50eafd2ddb02fd
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Command / Drupal_8 / Plugin / EntityReferenceSelection.php
1 <?php
2
3 namespace DrupalCodeGenerator\Command\Drupal_8\Plugin;
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:plugin:entity-reference-selection command.
14  */
15 class EntityReferenceSelection extends BaseGenerator {
16
17   protected $name = 'd8:plugin:entity-reference-selection';
18   protected $description = 'Generates entity reference selection plugin';
19   protected $alias = 'entity-reference-selection';
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function interact(InputInterface $input, OutputInterface $output) {
25
26     $base_classes = [
27       'comment' => 'Drupal\comment\Plugin\EntityReferenceSelection\CommentSelection',
28       'file' => 'Drupal\file\Plugin\EntityReferenceSelection\FileSelection',
29       'node' => 'Drupal\node\Plugin\EntityReferenceSelection\NodeSelection',
30       'taxonomy_term' => 'Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection',
31       'user' => 'Drupal\user\Plugin\EntityReferenceSelection\UserSelection',
32     ];
33
34     $questions = Utils::defaultQuestions();
35
36     $questions['entity_type'] = new Question('Entity type that can be referenced by this plugin', 'node');
37     $questions['entity_type']->setValidator([Utils::class, 'validateMachineName']);
38     $questions['entity_type']->setAutocompleterValues(array_keys($base_classes));
39
40     $questions['plugin_label'] = new Question('Plugin label', 'Advanced {entity_type} selection');
41     $questions['plugin_label']->setValidator([Utils::class, 'validateRequired']);
42
43     $questions['plugin_id'] = new Question('Plugin ID', [Utils::class, 'defaultPluginId']);
44     $questions['plugin_id']->setValidator([Utils::class, 'validateMachineName']);
45
46     $questions['configurable'] = new ConfirmationQuestion('Provide additional plugin configuration?', FALSE);
47
48     $default_class = function ($vars) {
49       return Utils::camelize($vars['machine_name'] . '_' . $vars['entity_type']) . 'Selection';
50     };
51     $questions['class'] = new Question('Class', $default_class);
52
53     $vars = &$this->collectVars($input, $output, $questions);
54
55     if (isset($base_classes[$vars['entity_type']])) {
56       $vars['base_class_full'] = $base_classes[$vars['entity_type']];
57     }
58     else {
59       $vars['base_class_full'] = 'Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection';
60     }
61
62     $vars['base_class'] = explode('EntityReferenceSelection\\', $vars['base_class_full'])[1];
63
64     $this->addFile()
65       ->path('src/Plugin/EntityReferenceSelection/{class}.php')
66       ->template('d8/plugin/entity-reference-selection.twig');
67
68     $this->addFile()
69       ->path('config/schema/{machine_name}.schema.yml')
70       ->template('d8/plugin/entity-reference-selection-schema.twig')
71       ->action('append');
72   }
73
74 }