New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bundle classes generator #4903
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d96c5c
Add bundle classes generator
weitzman f0ae0ba
Move classes.
weitzman 9264f7b
Unneeded changes
weitzman b5fe8aa
Better code comment
weitzman 5b7310b
Only content entities. Add option to skip Base class
weitzman 0f8c09a
Change namespace to src/Entity/Bundle
weitzman File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Drupal/Generators/entity/EntityBundleClassesGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
namespace Drush\Drupal\Generators\entity; | ||
|
||
use Drupal\Core\Entity\EntityTypeBundleInfoInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use DrupalCodeGenerator\Command\ModuleGenerator; | ||
use DrupalCodeGenerator\Utils; | ||
use Symfony\Component\Console\Question\ChoiceQuestion; | ||
|
||
class EntityBundleClassesGenerator extends ModuleGenerator | ||
{ | ||
protected string $name = 'entity:bundle-classes'; | ||
protected string $description = 'Generate a bundle class for each content entity.'; | ||
protected string $alias = 'ebc'; | ||
protected string $templatePath = __DIR__; | ||
|
||
protected $bundleInfo; | ||
protected $entityTypeManager; | ||
|
||
public function __construct(EntityTypeBundleInfoInterface $bundleInfo, EntityTypeManagerInterface $entityTypeManager) | ||
{ | ||
parent::__construct($this->name); | ||
$this->bundleInfo = $bundleInfo; | ||
$this->entityTypeManager = $entityTypeManager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function generate(array &$vars): void | ||
{ | ||
$this->collectDefault($vars); | ||
$vars['infos'] = $this->bundleInfo->getAllBundleInfo(); | ||
$choices = array_keys($vars['infos']); | ||
$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); | ||
foreach ($vars['entity_type_ids'] as $id) { | ||
$base_class = $vars['base_class'] = Utils::camelize($id . 'BundleBase'); | ||
$vars['entity_class'] = $this->entityTypeManager->getStorage($id)->getEntityClass(); | ||
$vars['entity_type_id'] = $id; | ||
$this->addFile("src/Bundle/$id/${base_class}.php", 'base_bundle_class.php.twig')->vars($vars); | ||
foreach ($vars['infos'][$id] as $bundle => $info) { | ||
$bundle_class = $vars['bundle_class'] = Utils::camelize($bundle); | ||
$this->addFile("src/Bundle/$id/${bundle_class}.php", 'bundle_class.php.twig')->vars($vars); | ||
} | ||
} | ||
$this->logger->warning('Run `drush cache:rebuild` so the bundle classes are recognized.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace Drupal\{{ machine_name }}\Bundle\{{ entity_type_id }}; | ||
|
||
use {{ entity_class }}; | ||
|
||
/** | ||
* A base bundle class for {{ entity_type_id }} entities. | ||
*/ | ||
abstract class {{ base_class }} extends {{ entity_class|split('\\')|last }} { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
namespace Drupal\{{ machine_name }}\Bundle\{{ entity_type_id }}; | ||
|
||
/** | ||
* A bundle class for {{ entity_type_id }} entities. | ||
*/ | ||
class {{ bundle_class }} extends {{ base_class }} { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Implements hook_entity_bundle_info_alter(). | ||
*/ | ||
function drush_empty_module_entity_bundle_info_alter(array &$bundles): void { | ||
{% for id in entity_type_ids %} | ||
{% for bundle in infos[id]|keys %} | ||
$bundles['{{ id }}']['{{ bundle }}']['class'] = \Drupal\{{ machine_name }}\Bundle\{{ id }}\{{ bundle|camelize }}::class; | ||
{% endfor %} | ||
{% endfor %} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Primary module hooks for {{ machine_name }} module. | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default PHPCS lints will want this moved into a use statement. Given that it can also automatically fix the lint, I'd say no change is warranted here.