Skip to content

Commit

Permalink
Fixes hechoendrupal#1813: Add base_path option for entity generation
Browse files Browse the repository at this point in the history
  • Loading branch information
larowlan committed Jan 26, 2016
1 parent 9ad6702 commit 5905e4e
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 26 deletions.
7 changes: 5 additions & 2 deletions Test/Command/GeneratorEntityConfigCommandTest.php
Expand Up @@ -21,14 +21,16 @@ class GeneratorEntityConfigCommandTest extends GenerateCommandTest
* @param $entity_name
* @param $entity_class
* @param $label
* @param $base_path
*
* @dataProvider commandData
*/
public function testGenerateEntityConfig(
$module,
$entity_name,
$entity_class,
$label
$label,
$base_path
) {
$command = new EntityConfigCommand($this->getHelperSet());
$command->setHelperSet($this->getHelperSet());
Expand All @@ -41,7 +43,8 @@ public function testGenerateEntityConfig(
'--module' => $module,
'--entity-name' => $entity_name,
'--entity-class' => $entity_class,
'--label' => $label
'--label' => $label,
'--base-path' => $base_path,
],
['interactive' => false]
);
Expand Down
7 changes: 5 additions & 2 deletions Test/Command/GeneratorEntityContentCommandTest.php
Expand Up @@ -21,14 +21,16 @@ class EntityContentCommandTest extends GenerateCommandTest
* @param $entity_name
* @param $entity_class
* @param $label
* @param $base_path
*
* @dataProvider commandData
*/
public function testGenerateEntityContent(
$module,
$entity_name,
$entity_class,
$label
$label,
$base_path
) {
$command = new EntityContentCommand($this->getHelperSet());
$command->setHelperSet($this->getHelperSet());
Expand All @@ -41,7 +43,8 @@ public function testGenerateEntityContent(
'--module' => $module,
'--entity-name' => $entity_name,
'--entity-class' => $entity_class,
'--label' => $label
'--label' => $label,
'--base-path' => $base_path,
],
['interactive' => false]
);
Expand Down
2 changes: 1 addition & 1 deletion Test/DataProvider/EntityConfigDataProviderTrait.php
Expand Up @@ -16,7 +16,7 @@ public function commandData()
$this->setUpTemporaryDirectory();

return [
['Foo', 'foo' . rand(), 'Bar', 'bar'],
['Foo', 'foo' . rand(), 'Bar', '', 'bar', 'admin/structure'],
];
}
}
2 changes: 1 addition & 1 deletion Test/DataProvider/EntityContentDataProviderTrait.php
Expand Up @@ -16,7 +16,7 @@ public function commandData()
$this->setUpTemporaryDirectory();

return [
['Foo', 'foo' . rand(), 'Bar', 'bar'],
['Foo', 'foo' . rand(), 'Bar', 'bar', 'admin/structure'],
];
}
}
7 changes: 5 additions & 2 deletions Test/Generator/EntityConfigGeneratorTest.php
Expand Up @@ -21,14 +21,16 @@ class EntityConfigGeneratorTest extends GeneratorTest
* @param $entity_name
* @param $entity_class
* @param $label
* @param $base_path
*
* @dataProvider commandData
*/
public function testGenerateEntityConfig(
$module,
$entity_name,
$entity_class,
$label
$label,
$base_path
) {
$generator = new EntityConfigGenerator();
$this->getRenderHelper()->setSkeletonDirs($this->getSkeletonDirs());
Expand All @@ -39,7 +41,8 @@ public function testGenerateEntityConfig(
$module,
$entity_name,
$entity_class,
$label
$label,
$base_path
);

$files = [
Expand Down
7 changes: 5 additions & 2 deletions Test/Generator/EntityContentGeneratorTest.php
Expand Up @@ -21,14 +21,16 @@ class EntityContentGeneratorTest extends GeneratorTest
* @param $entity_name
* @param $entity_class
* @param $label
* @param $base_path
*
* @dataProvider commandData
*/
public function testGenerateEntityContent(
$module,
$entity_name,
$entity_class,
$label
$label,
$base_path
) {
$generator = new EntityContentGenerator();
$this->getRenderHelper()->setSkeletonDirs($this->getSkeletonDirs());
Expand All @@ -39,7 +41,8 @@ public function testGenerateEntityContent(
$module,
$entity_name,
$entity_class,
$label
$label,
$base_path
);

$files = [
Expand Down
7 changes: 5 additions & 2 deletions Test/Generator/EntityGeneratorTest.php
Expand Up @@ -21,14 +21,16 @@ class EntityGeneratorTest extends GeneratorTest
* @param $entity_name
* @param $entity_class
* @param $label
* @param $base_path
*
* @dataProvider commandData
*/
public function testGenerateEntity(
$module,
$entity_name,
$entity_class,
$label
$label,
$base_path
) {
$generator = new EntityConfigGenerator();
$this->getRenderHelper()->setSkeletonDirs($this->getSkeletonDirs());
Expand All @@ -39,7 +41,8 @@ public function testGenerateEntity(
$module,
$entity_name,
$entity_class,
$label
$label,
$base_path
);

$files = [
Expand Down
2 changes: 2 additions & 0 deletions config/translations/en/generate.entity.config.yml
Expand Up @@ -6,9 +6,11 @@ options:
entity-name: 'The config entity name'
label: 'The label'
bundle-of: 'Acts as bundle for content entities'
base-path: "The base-path for the config entity's routes"
questions:
module: common.questions.module
entity-class: 'Enter the class of your new config entity'
entity-name: 'Enter the name of your new config entity'
label: 'Enter the label of your new config entity'
bundle-of: 'Name of the content entity you want this (config) entity to act as a bundle for'
base-path: "Enter the base-path for the config entity's routes"
2 changes: 2 additions & 0 deletions config/translations/en/generate.entity.content.yml
Expand Up @@ -6,9 +6,11 @@ options:
entity-name: 'The content entity name'
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: "The base-path for the content entity's routes"
questions:
module: common.questions.module
entity-class: 'Enter the class of your new content entity'
entity-name: 'Enter the name of your new content entity'
label: 'Enter the label of your new content entity'
has-bundles: 'Do you want this (content) entity to have bundles'
base-path: "Enter the base-path for the content entity's routes"
32 changes: 32 additions & 0 deletions src/Command/Generate/EntityCommand.php
Expand Up @@ -71,6 +71,12 @@ protected function configure()
InputOption::VALUE_REQUIRED,
$this->trans('commands.'.$commandKey.'.options.entity-name')
)
->addOption(
'base-path',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.' . $commandKey . '.options.base-path')
)
->addOption(
'label',
null,
Expand Down Expand Up @@ -138,9 +144,35 @@ function ($entityName) {
);
$input->setOption('label', $label);
}

// --base-path option
$base_path = $input->getOption('base-path');
if (!$base_path) {
$base_path = $this->getDefaultBasePath();
}
$base_path = $io->ask(
$this->trans('commands.'.$commandKey.'.questions.base_path'),
$base_path
);
if (substr($base_path, 0, 1) !== '/') {
// Base path must start with a leading '/'.
$base_path = '/' . $base_path;
}
$input->setOption('base-path', $base_path);
}

protected function createGenerator()
{
}

/**
* Gets default base path.
*
* @return string
* Default base path.
*/
protected function getDefaultBasePath()
{
return '/admin/structure';
}
}
4 changes: 3 additions & 1 deletion src/Command/Generate/EntityConfigCommand.php
Expand Up @@ -60,14 +60,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$entity_name = $input->getOption('entity-name');
$label = $input->getOption('label');
$bundle_of = $input->getOption('bundle-of');
$base_path = $input->getOption('base-path');

$this
->getGenerator()
->generate($module, $entity_name, $entity_class, $label, $bundle_of);
->generate($module, $entity_name, $entity_class, $label, $base_path, $bundle_of);
}

protected function createGenerator()
{
return new EntityConfigGenerator();
}

}
3 changes: 2 additions & 1 deletion src/Command/Generate/EntityContentCommand.php
Expand Up @@ -62,12 +62,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$entity_name = $input->getOption('entity-name');
$label = $input->getOption('label');
$has_bundles = $input->getOption('has-bundles');
$base_path = $input->getOption('base-path');

$bundle_entity_name = $has_bundles ? $entity_name . '_type' : null;

$this
->getGenerator()
->generate($module, $entity_name, $entity_class, $label, $bundle_entity_name);
->generate($module, $entity_name, $entity_class, $label, $base_path, $bundle_entity_name);

if ($has_bundles) {
$this->getChain()->addCommand(
Expand Down
4 changes: 3 additions & 1 deletion src/Generator/EntityConfigGenerator.php
Expand Up @@ -16,16 +16,18 @@ class EntityConfigGenerator extends Generator
* @param string $entity_name Entity machine name
* @param string $entity_class Entity class name
* @param string $label Entity label
* @param string $base_path Base path
* @param string $bundle_of Entity machine name of the content entity this config entity acts as a bundle for.
*/
public function generate($module, $entity_name, $entity_class, $label, $bundle_of = null)
public function generate($module, $entity_name, $entity_class, $label, $base_path, $bundle_of = null)
{
$parameters = [
'module' => $module,
'entity_name' => $entity_name,
'entity_class' => $entity_class,
'label' => $label,
'bundle_of' => $bundle_of,
'base_path' => $base_path,
];

$this->renderFile(
Expand Down
4 changes: 3 additions & 1 deletion src/Generator/EntityContentGenerator.php
Expand Up @@ -16,16 +16,18 @@ class EntityContentGenerator extends Generator
* @param string $entity_name Entity machine name
* @param string $entity_class Entity class name
* @param string $label Entity label
* @param string $base_path Base path
* @param string $bundle_entity_type (Config) entity type acting as bundle
*/
public function generate($module, $entity_name, $entity_class, $label, $bundle_entity_type = null)
public function generate($module, $entity_name, $entity_class, $label, $base_path, $bundle_entity_type = null)
{
$parameters = [
'module' => $module,
'entity_name' => $entity_name,
'entity_class' => $entity_class,
'label' => $label,
'bundle_entity_type' => $bundle_entity_type,
'base_path' => $base_path,
];

if ($bundle_entity_type) {
Expand Down
10 changes: 5 additions & 5 deletions templates/module/src/Entity/entity-content.php.twig
Expand Up @@ -67,11 +67,11 @@ use Drupal\user\UserInterface;
* },
{% endif %}
* links = {
* "canonical" = "/admin/structure/{{ entity_name }}/{{ '{'~entity_name~'}' }}",
* "add-form" = "/admin/structure/{{ entity_name }}/add",
* "edit-form" = "/admin/structure/{{ entity_name }}/{{ '{'~entity_name~'}' }}/edit",
* "delete-form" = "/admin/structure/{{ entity_name }}/{{ '{'~entity_name~'}' }}/delete",
* "collection" = "/admin/structure/{{ entity_name }}",
* "canonical" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}",
* "add-form" = "{{ base_path }}/{{ entity_name }}/add",
* "edit-form" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/edit",
* "delete-form" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/delete",
* "collection" = "{{ base_path }}/{{ entity_name }}",
* },
{% if bundle_entity_type %}
* bundle_entity_type = "{{ bundle_entity_type }}",
Expand Down
10 changes: 5 additions & 5 deletions templates/module/src/Entity/entity.php.twig
Expand Up @@ -42,11 +42,11 @@ use Drupal\{{ module }}\{{ entity_class }}Interface;
* "uuid" = "uuid"
* },
* links = {
* "canonical" = "/admin/structure/{{ entity_name }}/{{ '{'~entity_name~'}' }}",
* "add-form" = "/admin/structure/{{ entity_name }}/add",
* "edit-form" = "/admin/structure/{{ entity_name }}/{{ '{'~entity_name~'}' }}/edit",
* "delete-form" = "/admin/structure/{{ entity_name }}/{{ '{'~entity_name~'}' }}/delete",
* "collection" = "/admin/structure/{{ entity_name }}"
* "canonical" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}",
* "add-form" = "{{ base_path }}/{{ entity_name }}/add",
* "edit-form" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/edit",
* "delete-form" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/delete",
* "collection" = "{{ base_path }}/{{ entity_name }}"
* }
* )
*/
Expand Down

0 comments on commit 5905e4e

Please sign in to comment.