Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginFieldFormatterCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PluginFieldFormatterCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Generator\PluginFieldFormatterGenerator;
14 use Drupal\Console\Command\Shared\ModuleTrait;
15 use Drupal\Console\Command\Shared\ConfirmationTrait;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Core\Field\FieldTypePluginManager;
19 use Drupal\Console\Extension\Manager;
20 use Drupal\Console\Core\Command\Shared\CommandTrait;
21 use Drupal\Console\Core\Utils\StringConverter;
22 use Drupal\Console\Core\Utils\ChainQueue;
23
24 /**
25  * Class PluginFieldFormatterCommand
26  *
27  * @package Drupal\Console\Command\Generate
28  */
29 class PluginFieldFormatterCommand extends Command
30 {
31     use ModuleTrait;
32     use ConfirmationTrait;
33     use CommandTrait;
34
35     /**
36  * @var Manager
37 */
38     protected $extensionManager;
39
40     /**
41  * @var PluginFieldFormatterGenerator
42 */
43     protected $generator;
44
45     /**
46      * @var StringConverter
47      */
48     protected $stringConverter;
49
50     /**
51  * @var FieldTypePluginManager
52 */
53     protected $fieldTypePluginManager;
54
55     /**
56      * @var ChainQueue
57      */
58     protected $chainQueue;
59
60
61     /**
62      * PluginImageFormatterCommand constructor.
63      *
64      * @param Manager                       $extensionManager
65      * @param PluginFieldFormatterGenerator $generator
66      * @param StringConverter               $stringConverter
67      * @param FieldTypePluginManager        $fieldTypePluginManager
68      * @param ChainQueue                    $chainQueue
69      */
70     public function __construct(
71         Manager $extensionManager,
72         PluginFieldFormatterGenerator $generator,
73         StringConverter $stringConverter,
74         FieldTypePluginManager $fieldTypePluginManager,
75         ChainQueue $chainQueue
76     ) {
77         $this->extensionManager = $extensionManager;
78         $this->generator = $generator;
79         $this->stringConverter = $stringConverter;
80         $this->fieldTypePluginManager = $fieldTypePluginManager;
81         $this->chainQueue = $chainQueue;
82         parent::__construct();
83     }
84
85     protected function configure()
86     {
87         $this
88             ->setName('generate:plugin:fieldformatter')
89             ->setDescription($this->trans('commands.generate.plugin.fieldformatter.description'))
90             ->setHelp($this->trans('commands.generate.plugin.fieldformatter.help'))
91             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
92             ->addOption(
93                 'class',
94                 null,
95                 InputOption::VALUE_REQUIRED,
96                 $this->trans('commands.generate.plugin.fieldformatter.options.class')
97             )
98             ->addOption(
99                 'label',
100                 null,
101                 InputOption::VALUE_OPTIONAL,
102                 $this->trans('commands.generate.plugin.fieldformatter.options.label')
103             )
104             ->addOption(
105                 'plugin-id',
106                 null,
107                 InputOption::VALUE_OPTIONAL,
108                 $this->trans('commands.generate.plugin.fieldformatter.options.plugin-id')
109             )
110             ->addOption(
111                 'field-type',
112                 null,
113                 InputOption::VALUE_OPTIONAL,
114                 $this->trans('commands.generate.plugin.fieldformatter.options.field-type')
115             );
116     }
117
118     /**
119      * {@inheritdoc}
120      */
121     protected function execute(InputInterface $input, OutputInterface $output)
122     {
123         $io = new DrupalStyle($input, $output);
124
125         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
126         if (!$this->confirmGeneration($io)) {
127             return 1;
128         }
129
130         $module = $input->getOption('module');
131         $class_name = $input->getOption('class');
132         $label = $input->getOption('label');
133         $plugin_id = $input->getOption('plugin-id');
134         $field_type = $input->getOption('field-type');
135
136         $this->generator->generate($module, $class_name, $label, $plugin_id, $field_type);
137
138         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
139
140         return 0;
141     }
142
143     protected function interact(InputInterface $input, OutputInterface $output)
144     {
145         $io = new DrupalStyle($input, $output);
146
147         // --module option
148         $module = $input->getOption('module');
149         if (!$module) {
150             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
151             $module = $this->moduleQuestion($io);
152             $input->setOption('module', $module);
153         }
154
155         // --class option
156         $class = $input->getOption('class');
157         if (!$class) {
158             $class = $io->ask(
159                 $this->trans('commands.generate.plugin.fieldformatter.questions.class'),
160                 'ExampleFieldFormatter'
161             );
162             $input->setOption('class', $class);
163         }
164
165         // --plugin label option
166         $label = $input->getOption('label');
167         if (!$label) {
168             $label = $io->ask(
169                 $this->trans('commands.generate.plugin.fieldformatter.questions.label'),
170                 $this->stringConverter->camelCaseToHuman($class)
171             );
172             $input->setOption('label', $label);
173         }
174
175         // --name option
176         $plugin_id = $input->getOption('plugin-id');
177         if (!$plugin_id) {
178             $plugin_id = $io->ask(
179                 $this->trans('commands.generate.plugin.fieldformatter.questions.plugin-id'),
180                 $this->stringConverter->camelCaseToUnderscore($class)
181             );
182             $input->setOption('plugin-id', $plugin_id);
183         }
184
185         // --field type option
186         $field_type = $input->getOption('field-type');
187         if (!$field_type) {
188             // Gather valid field types.
189             $field_type_options = [];
190             foreach ($this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions()) as $category => $field_types) {
191                 foreach ($field_types as $name => $field_type) {
192                     $field_type_options[] = $name;
193                 }
194             }
195
196             $field_type = $io->choice(
197                 $this->trans('commands.generate.plugin.fieldwidget.questions.field-type'),
198                 $field_type_options
199             );
200
201             $input->setOption('field-type', $field_type);
202         }
203     }
204 }