diff --git a/src/Drupal/Commands/core/drush.services.yml b/src/Drupal/Commands/core/drush.services.yml index 6dca5b9d28..b48b6d026d 100644 --- a/src/Drupal/Commands/core/drush.services.yml +++ b/src/Drupal/Commands/core/drush.services.yml @@ -21,6 +21,11 @@ services: arguments: ['@entity_type.manager'] tags: - { name: drush.command } + entity.generators: + class: Drush\Drupal\Generators\entity\EntityBundleClassesGenerator + arguments: ['@entity_type.bundle.info', '@entity_type.manager'] + tags: + - { name: drush.generator.v2 } image.commands: class: \Drush\Drupal\Commands\core\ImageCommands tags: diff --git a/src/Drupal/Generators/entity/EntityBundleClassesGenerator.php b/src/Drupal/Generators/entity/EntityBundleClassesGenerator.php new file mode 100644 index 0000000000..4328219fb9 --- /dev/null +++ b/src/Drupal/Generators/entity/EntityBundleClassesGenerator.php @@ -0,0 +1,67 @@ +name); + $this->bundleInfo = $bundleInfo; + $this->entityTypeManager = $entityTypeManager; + } + + /** + * {@inheritdoc} + */ + protected function generate(array &$vars): void + { + $this->collectDefault($vars); + $vars['bundle_info'] = $this->bundleInfo->getAllBundleInfo(); + $definitions = $this->entityTypeManager->getDefinitions(); + $vars['entity_types'] = array_filter($definitions, [$this, 'isContentEntity']); + $choices = array_keys($vars['entity_types']); + $question = new ChoiceQuestion('Entity type(s). Use comma to delimit.', $choices, 'node'); + $question->setValidator([static::class, 'validateRequired']); + $question->setMultiselect(true); + $vars['entity_type_ids'] = $this->io->askQuestion($question); + $this->addFile($vars['machine_name'] . '.module', 'hook_bundle_info.php') + // @todo When we require 2.1, use https://getcomposer.org/doc/07-runtime.md#installed-versions + // to get path to DCG so we use its templates/_lib/file-docs/module.twig instead of a copy of that file. + ->headerTemplate('module.twig') + ->appendIfExists() + ->headerSize(7); + $vars['use_base_class'] = $this->confirm('Generate a base class? Respond no if you can easily modify the entity class.'); + foreach ($vars['entity_type_ids'] as $id) { + $vars['entity_class'] = $vars['parent_class'] = $this->entityTypeManager->getStorage($id)->getEntityClass(); + $vars['entity_type_id'] = $id; + if ($vars['use_base_class']) { + $base_class = $vars['base_class'] = $vars['parent_class'] = Utils::camelize($id . 'BundleBase'); + $this->addFile("src/Entity/Bundle/$id/${base_class}.php", 'base_bundle_class.php.twig')->vars($vars); + } + foreach ($vars['bundle_info'][$id] as $bundle => $info) { + $bundle_class = $vars['bundle_class'] = Utils::camelize($bundle); + $this->addFile("src/Entity/Bundle/$id/${bundle_class}.php", 'bundle_class.php.twig')->vars($vars); + } + } + $this->logger->warning('Run `drush cache:rebuild` so the bundle classes are recognized.'); + } + + protected function isContentEntity($definition) + { + return $definition->getGroup() == 'content'; + } +} diff --git a/src/Drupal/Generators/entity/base_bundle_class.php.twig b/src/Drupal/Generators/entity/base_bundle_class.php.twig new file mode 100644 index 0000000000..08b536fc80 --- /dev/null +++ b/src/Drupal/Generators/entity/base_bundle_class.php.twig @@ -0,0 +1,11 @@ +