-
Notifications
You must be signed in to change notification settings - Fork 9
Add drupal console generator command. #38
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
Open
Jaesin
wants to merge
11
commits into
8.x-1.x
Choose a base branch
from
console-ceb
base: 8.x-1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
de86542
[Console] Start the drupal-console integration.
Jaesin 46212cc
Merge branch '8.x-1.x' into console-ceb
Jaesin c711a14
Merge branch '8.x-1.x' into console-ceb
Jaesin 366788b
[Drupal console] Support createing a CEB entity.
Jaesin 6b930c4
[Drupal console] Update the documentation.
Jaesin 4a35144
[Drupal console] Add a custom entity type generator.
Jaesin ba56412
Merge branch '8.x-1.x' into console-ceb
Jaesin 24ebd1c
[Console] Refacter and remove the ceb-bundle command.
Jaesin e51b313
Merge in 8.x-1.x
Jaesin d07c2ed
Merge branch '8.x-1.x' into console-ceb
Jaesin b31fe1e
[cleanup] Use a hyphonated verison of the entity machine name for pat…
Jaesin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,7 @@ | ||
| command: | ||
| console_ceb: | ||
| generate: | ||
| 'entity:ceb': | ||
| description: 'Generate a new (Content Entity Base) content entity' | ||
| questions: | ||
| entity-class: 'Class?' |
This file contains hidden or 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,5 @@ | ||
| name: Console CEB | ||
| type: module | ||
| description: Provides Drupal Console integration for the CEB module. | ||
| core: 8.x | ||
| package: Drupal console |
This file contains hidden or 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,53 @@ | ||
| <?php | ||
| /** | ||
| * @file | ||
| * Contains \Drupal\console_ceb\Command\GenerateEntityCommand. | ||
| */ | ||
|
|
||
| namespace Drupal\console_ceb\Command; | ||
|
|
||
| use Drupal\Console\Command\Generate\EntityCommand; | ||
| use Drupal\console_ceb\Generator\EntityGenerator; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| /** | ||
| * Class GenerateEntityCommand. | ||
| * | ||
| * @package Drupal\console_ceb | ||
| */ | ||
| class GenerateEntityCommand extends EntityCommand { | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function configure() { | ||
| $this->setEntityType('EntityContent'); | ||
| $this->setCommandName('generate:entity:ceb'); | ||
| parent::configure(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function execute(InputInterface $input, OutputInterface $output) | ||
| { | ||
| $module = $input->getOption('module'); | ||
| $entity_class = $input->getOption('entity-class'); | ||
| $entity_name = $input->getOption('entity-name'); | ||
| $label = $input->getOption('label'); | ||
|
|
||
| $bundle_entity_name = $entity_name . '_type'; | ||
|
|
||
| // Use the generator to create the entity files from the template files. | ||
| $this | ||
| ->getGenerator() | ||
| ->generate($module, $entity_name, $entity_class, $label, $bundle_entity_name); | ||
| } | ||
|
|
||
| protected function createGenerator() { | ||
| return new EntityGenerator(); | ||
| } | ||
| } | ||
This file contains hidden or 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,204 @@ | ||
| <?php | ||
| /** | ||
| * @file | ||
| * Contains \Drupal\console_ceb\Generator\EntityGenerator. | ||
| */ | ||
|
|
||
| namespace Drupal\console_ceb\Generator; | ||
|
|
||
| use Drupal\Console\Generator\EntityContentGenerator; | ||
|
|
||
| /** | ||
| * Handles generating the files required for a Content Entity Base entity. | ||
| * | ||
| * Generates: | ||
| * | ||
| * 1. module.permissions.yml | ||
| * 2. module.links.menu.yml | ||
| * 3. module.links.action.yml | ||
| * 4. module.links.task.yml | ||
| * 5. module.page.inc | ||
| * 6. templates/entity.html.twig | ||
| * 7. templates/entity-content-add-list.html.twig | ||
| * 8. src/Entity/EntityClass.php | ||
| * 9. src/Entity/Access/EntityClassPermissions.php | ||
| * | ||
| * @package Drupal\console_ceb\Generator | ||
| */ | ||
| class EntityGenerator extends EntityContentGenerator { | ||
| /** | ||
| * Generator Entity. | ||
| * | ||
| * @param string $module Module name | ||
| * @param string $entity_name Entity machine name | ||
| * @param string $entity_class Entity class name | ||
| * @param string $label Entity label | ||
| * @param string $bundle_entity_type (Config) entity type acting as bundle | ||
| */ | ||
| public function generate($module, $entity_name, $entity_class, $label, $bundle_entity_type = null) { | ||
|
|
||
| // Get the module, entity and template paths. | ||
| $module_path = $this->getSite()->getModulePath($module); | ||
| $entity_path = $this->getSite()->getEntityPath($module); | ||
| $template_path = $this->getSite()->getTemplatePath($module); | ||
| $module_filename = "{$module_path}/{$module}.module"; | ||
| $entity_hyphenated = str_replace('_', '-', $entity_name); | ||
|
|
||
|
|
||
| // Use these parameters for content entity creation. | ||
| $parameters = [ | ||
| 'module' => $module, | ||
| 'entity_name' => $entity_name, | ||
| 'entity_hyphenated' => $entity_hyphenated, | ||
| 'entity_class' => $entity_class, | ||
| 'label' => $label, | ||
| 'bundle_entity_type' => $bundle_entity_type, | ||
| ]; | ||
|
|
||
| /** | ||
| * Create the yml files. | ||
| */ | ||
| $this->renderFile( | ||
| 'module/permissions-entity-ceb.yml.twig', | ||
| "{$module_path}/{$module}.permissions.yml", | ||
| $parameters, | ||
| FILE_APPEND | ||
| ); | ||
|
|
||
| $this->renderFile( | ||
| 'module/links.menu-entity-ceb.yml.twig', | ||
| "{$module_path}/{$module}.links.menu.yml", | ||
| $parameters, | ||
| FILE_APPEND | ||
| ); | ||
|
|
||
| $this->renderFile( | ||
| 'module/links.task-entity-ceb.yml.twig', | ||
| "{$module_path}/{$module}.links.task.yml", | ||
| $parameters, | ||
| FILE_APPEND | ||
| ); | ||
|
|
||
| $this->renderFile( | ||
| 'module/links.action-entity-ceb.yml.twig', | ||
| "{$module_path}/{$module}.links.action.yml", | ||
| $parameters, | ||
| FILE_APPEND | ||
| ); | ||
|
|
||
| /** | ||
| * Create the permissions class. | ||
| */ | ||
| $this->renderFile( | ||
| 'module/src/Entity/Access/permissions.php.twig', | ||
| "{$entity_path}/Access/{$entity_class}Permissions.php", | ||
| $parameters + [ | ||
| 'class_name' => $entity_class.'Permissions', | ||
| ] | ||
| ); | ||
|
|
||
| /** | ||
| * Create the content entity plugin. | ||
| */ | ||
| $this->renderFile( | ||
| 'module/src/Entity/entity-ceb-content.php.twig', | ||
| "{$entity_path}/{$entity_class}.php", | ||
| $parameters | ||
| ); | ||
|
|
||
| /** | ||
| * Create the content entity's template files. | ||
| */ | ||
| $this->renderFile( | ||
| 'module/entity-content-page.php.twig', | ||
| "{$module_path}/{$entity_name}.page.inc", | ||
| $parameters | ||
| ); | ||
|
|
||
| $this->renderFile( | ||
| 'module/templates/entity-html.twig', | ||
| "{$template_path}/{$entity_name}.html.twig", | ||
| $parameters | ||
| ); | ||
|
|
||
| if ($bundle_entity_type) { | ||
| $bundle_entity_hyphenated = str_replace('_', '-', $entity_name); | ||
| $this->renderFile( | ||
| 'module/templates/entity-with-bundle-content-add-list-html.twig', | ||
| "{$template_path}/{$bundle_entity_hyphenated}-content-add-list.html.twig", | ||
| $parameters | ||
| ); | ||
|
|
||
| // Check for hook_theme() in module file and warn ... | ||
| if (file_exists($module_filename) && preg_match("/function\\s+{$module}_theme/", file_get_contents($module_filename)) !== 0) { | ||
| echo "================ | ||
| Warning: | ||
| ================ | ||
| It looks like you have a hook_theme already declared! | ||
| Please manually merge the two hook_theme() implementations in {$module_filename}! | ||
| "; | ||
| } else { | ||
|
|
||
| $this->renderFile( | ||
| 'module/src/Entity/entity-content-with-bundle.theme.php.twig', | ||
| $module_filename, | ||
| $parameters, | ||
| FILE_APPEND | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Compose the bundle parameters. | ||
| */ | ||
| $bundle_entity_class = "{$entity_class}Type"; | ||
| $bundle_label = "{$label} type"; | ||
| $bundle_parameters = [ | ||
| 'module' => $module, | ||
| 'entity_name' => $bundle_entity_type, | ||
| 'entity_hyphenated' => $bundle_entity_hyphenated, | ||
| 'entity_class' => $bundle_entity_class, | ||
| 'label' => $bundle_label, | ||
| 'bundle_of' => $entity_name, | ||
| ]; | ||
|
|
||
| /** | ||
| * Render the bundle entity files. | ||
| */ | ||
| $this->renderFile( | ||
| 'module/config/schema/entity.schema.yml.twig', | ||
| "{$module_path}" . "/config/schema/{$bundle_entity_type}.schema.yml", | ||
| $bundle_parameters | ||
| ); | ||
|
|
||
| $this->renderFile( | ||
| 'module/links.menu-entity-config.yml.twig', | ||
| "{$module_path}" . "/{$module}.links.menu.yml", | ||
| $bundle_parameters, | ||
| FILE_APPEND | ||
| ); | ||
|
|
||
| $this->renderFile( | ||
| 'module/links.action-entity.yml.twig', | ||
| "{$module_path}/{$module}.links.action.yml", | ||
| $bundle_parameters, | ||
| FILE_APPEND | ||
| ); | ||
|
|
||
| $this->renderFile( | ||
| 'module/src/Entity/entity-ceb-bundle.php.twig', | ||
| "{$entity_path}/{$bundle_entity_class}.php", | ||
| $bundle_parameters | ||
| ); | ||
| } | ||
|
|
||
| $content = $this->getRenderHelper()->render( | ||
| 'module/src/Entity/entity-content.theme.php.twig', | ||
| $parameters | ||
| ); | ||
|
|
||
| if ($this->isLearning()) { | ||
| echo 'Add this to your hook_theme:'.PHP_EOL; | ||
| echo $content; | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
console_ceb/templates/module/links.action-entity-ceb.yml.twig
This file contains hidden or 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 @@ | ||
| # Definition for {{ label }} menu links. | ||
|
|
||
| entity.{{ entity_name }}.add_form: | ||
| route_name: entity.{{ entity_name }}.add_page | ||
| title: 'Add {{ label }}' | ||
| appears_on: | ||
| - entity.{{ entity_name }}.collection | ||
| - entity.{{ entity_name }}.canonical | ||
|
|
12 changes: 12 additions & 0 deletions
12
console_ceb/templates/module/links.menu-entity-ceb.yml.twig
This file contains hidden or 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,12 @@ | ||
| # Definition for {{ label }} menu items. | ||
|
|
||
| entity.{{ entity_name }}.collection: | ||
| title: '{{ label }}' | ||
| route_name: entity.{{ entity_name }}.collection | ||
| description: 'List {{ label }} content' | ||
| parent: system.admin_content | ||
|
|
||
| {{ entity_name }}.add_page: | ||
| title: 'Add {{ label }}' | ||
| route_name: entity.{{ entity_name }}.add_page | ||
|
|
9 changes: 9 additions & 0 deletions
9
console_ceb/templates/module/links.menu-entity-config.yml.twig
This file contains hidden or 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 @@ | ||
| # Definition for {{ label }} menu items. | ||
|
|
||
| entity.{{ entity_name }}.collection: | ||
| title: '{{ label }}' | ||
| route_name: entity.{{ entity_name }}.collection | ||
| description: 'Manage {{ label }} (bundles)' | ||
| parent: system.admin_structure | ||
| weight: 99 | ||
|
|
22 changes: 22 additions & 0 deletions
22
console_ceb/templates/module/links.task-entity-ceb.yml.twig
This file contains hidden or 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,22 @@ | ||
| # Definition for {{ label }} menu tasks. | ||
| entity.{{ entity_name }}.canonical: | ||
| route_name: entity.{{ entity_name }}.canonical | ||
| base_route: entity.{{ entity_name }}.canonical | ||
| title: 'View' | ||
|
|
||
| entity.{{ entity_name }}.edit_form: | ||
| route_name: entity.{{ entity_name }}.edit_form | ||
| base_route: entity.{{ entity_name }}.canonical | ||
| title: 'Edit' | ||
|
|
||
| entity.{{ entity_name }}.delete_form: | ||
| route_name: entity.{{ entity_name }}.delete_form | ||
| base_route: entity.{{ entity_name }}.canonical | ||
| title: 'Delete' | ||
| weight: 10 | ||
|
|
||
| entity.{{ entity_name }}.collection: | ||
| route_name: entity.{{ entity_name }}.collection | ||
| base_route: system.admin_content | ||
| title: '{{ label }} list' | ||
|
|
This file contains hidden or 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,3 @@ | ||
| permission_callbacks: | ||
| - \Drupal\{{ module }}\Entity\Access\{{ entity_class }}Permissions::entityPermissions | ||
|
|
33 changes: 33 additions & 0 deletions
33
console_ceb/templates/module/src/Entity/Access/permissions.php.twig
This file contains hidden or 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,33 @@ | ||
| {% extends "base/class.php.twig" %} | ||
|
|
||
| {% block file_path %} | ||
| Drupal\{{module}}\Entity\Access\{{ class_name }}. | ||
| {% endblock %} | ||
|
|
||
| {% block namespace_class %} | ||
| namespace Drupal\{{module}}\Entity\Access; | ||
| {% endblock %} | ||
|
|
||
| {% block use_class %} | ||
| use Drupal\content_entity_base\Entity\Access\EntityBasePermissions; | ||
| use Drupal\Core\Entity\ContentEntityTypeInterface; | ||
| {% endblock %} | ||
|
|
||
| {% block class_declaration %} | ||
| /** | ||
| * Class {{ class_name }}. | ||
| * Defines a class containing permission callbacks. | ||
| * @package Drupal\{{ module }}\Entity\Access | ||
| */ | ||
| class {{ class_name }} extends EntityBasePermissions {% endblock %} | ||
| {% block class_methods %} | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| * | ||
| * @todo Leverage https://www.drupal.org/node/2652684 | ||
| */ | ||
| public function entityPermissions(ContentEntityTypeInterface $entity_type = NULL) { | ||
| return parent::entityPermissions(\Drupal::entityTypeManager()->getDefinition('{{ entity_name }}')); | ||
| } | ||
| {% endblock %} |
Oops, something went wrong.
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.
I think we should also check whether this is an existing entity or not.
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.
That's a good idea.