Skip to content

Commit

Permalink
adding entity-class and entity-name like parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dmouse committed Jul 9, 2014
1 parent ff5d734 commit d78054f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 51 deletions.
46 changes: 32 additions & 14 deletions src/Command/GeneratorEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ protected function configure()
{
$this
->setDefinition(array(
new InputOption('module','',InputOption::VALUE_REQUIRED, 'The name of the module'),
new InputOption('entity','',InputOption::VALUE_REQUIRED, 'The name of the entity')
new InputOption('module',null,InputOption::VALUE_REQUIRED, 'The name of the module'),
new InputOption('entity-class',null,InputOption::VALUE_REQUIRED, 'The entity class name'),
new InputOption('entity-name',null,InputOption::VALUE_REQUIRED, 'The name of the entity'),
))
->setName('generate:entity')
->setDescription('Generate entity')
->setHelp('The <info>generate:entity</info> command helps you generate a new entity.');
->setName('generate:entity:config')
->setDescription('Generate entity configuration')
->setHelp('The <info>generate:entity:config</info> command helps you generate a new entity.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();

$module = $input->getOption('module');
$entity = $input->getOption('entity');
$entity_class = $input->getOption('entity-class');
$entity_name = $input->getOption('entity-name');

$this
->getGenerator()
->generate($module, $entity);
->generate($module, $entity_name, $entity_class);

$errors = [];
$dialog->writeGeneratorSummary($output, $errors);
Expand All @@ -54,6 +56,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Drupal entity generator');
$utils = $this->getStringUtils();

// --module option
$module = $input->getOption('module');
Expand All @@ -63,16 +66,31 @@ protected function interact(InputInterface $input, OutputInterface $output)
}
$input->setOption('module', $module);

// --entity option
$entity = $input->getOption('entity');
if (!$entity) {
$entity = $dialog->ask(
$output,
$dialog->getQuestion('Enter the entity name', '')
// --entity-class option
$entity_class = $input->getOption('entity-class');
if (!$entity_class) {
$entity_class = $dialog->ask(
$output,
$dialog->getQuestion('Enter the entity class name', 'DefaultEntity'),
'DefaultEntity',
null
);
}
$input->setOption('entity', $entity);
$input->setOption('entity-class', $entity_class);

$machine_name = $utils->camelCaseToMachineName($entity_class);

// --entity-name option
$entity_name = $input->getOption('entity-name');
if (!$entity_name) {
$entity_name = $dialog->ask(
$output,
$dialog->getQuestion('Enter the entity name', $machine_name),
$machine_name,
null
);
}
$input->setOption('entity-name', $entity_name);
}


Expand Down
31 changes: 16 additions & 15 deletions src/Generator/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,68 @@
class EntityGenerator extends Generator
{
/**
* Generator Service
* @param string $module Module name
* @param string $service_name Service name
* @param string $class_name Class name
* @param array $services List of services
* Generator Entity
*
* @param string $module Module name
* @param string $entity_name Entity machine name
* @param string $entity_class Entity class name
*/
public function generate($module, $entity)
public function generate($module, $entity_name, $entity_class)
{

$parameters = [
'module' => $module,
'entity' => $entity
'entity_name' => $entity_name,
'entity_class' => $entity_class,
];

$this->renderFile(
'module/config/schema/entity.schema.yml.twig',
$this->getModulePath($module). '/config/schema/' . $entity . '.schema.yml',
$this->getModulePath($module) . '/config/schema/' . $entity_class . '.schema.yml',
$parameters
);

$this->renderFile(
'module/routing-entity.yml.twig',
$this->getModulePath($module).'/'.$module.'.routing.yml',
$this->getModulePath($module) . '/' . $module . '.routing.yml',
$parameters,
FILE_APPEND
);

$this->renderFile(
'module/local_actions-entity.yml.twig',
$this->getModulePath($module).'/'.$module.'.local_actions.yml',
$this->getModulePath($module) . '/' . $module . '.local_actions.yml',
$parameters,
FILE_APPEND
);

$this->renderFile(
'module/interface-entity.php.twig',
$this->getSourcePath($module).'/'.ucwords($entity).'Interface.php',
$this->getSourcePath($module) . '/' . $entity_class . 'Interface.php',
$parameters
);

$this->renderFile(
'module/Entity/entity.php.twig',
$this->getEntityPath($module).'/'.ucwords($entity).'.php',
$this->getEntityPath($module) . '/' . $entity_class . '.php',
$parameters
);

$this->renderFile(
'module/Form/entity.php.twig',
$this->getFormPath($module).'/'.ucwords($entity).'Form.php',
$this->getFormPath($module).'/' . $entity_class . 'Form.php',
$parameters
);

$this->renderFile(
'module/Form/entity-delete.php.twig',
$this->getFormPath($module).'/'.ucwords($entity).'DeleteForm.php',
$this->getFormPath($module) . '/' . $entity_class . 'DeleteForm.php',
$parameters
);

$this->renderFile(
'module/Controller/entity-listbuilder.php.twig',
$this->getControllerPath($module).'/'.ucwords($entity).'ListBuilder.php',
$this->getControllerPath($module) . '/' . $entity_class . 'ListBuilder.php',
$parameters
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @file
* Contains Drupal\{{module}}\Controller\{{ entity|capitalize }}ListBuilder.
* Contains Drupal\{{module}}\Controller\{{ entity_class }}ListBuilder.
*/
namespace Drupal\{{module}}\Controller;
Expand All @@ -10,16 +10,16 @@ use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of {{ entity|capitalize }}.
* Provides a listing of {{ entity_class }}.
*/
class {{ entity|capitalize }}ListBuilder extends ConfigEntityListBuilder
class {{ entity_class }}ListBuilder extends ConfigEntityListBuilder
{
/**
* {@inheritdoc}
*/
public function buildHeader()
{
$header['label'] = $this->t('{{ entity|capitalize }}');
$header['label'] = $this->t('{{ entity_class }}');
$header['id'] = $this->t('Machine name');
return $header + parent::buildHeader();
}
Expand Down
35 changes: 18 additions & 17 deletions src/Resources/skeleton/module/Entity/entity.php.twig
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
<?php
/**
* @file
* Contains Drupal\{{ module }}\Entity\{{ entity|capitalize }}.
* Contains Drupal\{{ module }}\Entity\{{ entity_class }}.
*/
namespace Drupal\{{ module }}\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\{{ module }}\{{ entity|capitalize }}Interface;
use Drupal\{{ module }}\{{ entity_class }}Interface;
/**
* Defines the {{ entity|capitalize }} entity.
* Defines the {{ entity_class }} entity.
*
* @ConfigEntityType(
* id = "{{ entity }}",
* label = @Translation("{{ entity|capitalize }}"),
* id = "{{ entity_name }}",
* label = @Translation("{{ entity_class }}"),
* controllers = {
* "list_builder" = "Drupal\{{ module }}\Controller\{{ entity|capitalize }}ListBuilder",
* "list_builder" = "Drupal\{{ module }}\Controller\{{ entity_class }}ListBuilder",
* "form" = {
* "add" = "Drupal\{{ module }}\Form\{{ entity|capitalize }}Form",
* "edit" = "Drupal\{{ module }}\Form\{{ entity|capitalize }}Form",
* "delete" = "Drupal\{{ module }}\Form\{{ entity|capitalize }}DeleteForm"
* "add" = "Drupal\{{ module }}\Form\{{ entity_class }}Form",
* "edit" = "Drupal\{{ module }}\Form\{{ entity_class }}Form",
* "delete" = "Drupal\{{ module }}\Form\{{ entity_class }}DeleteForm"
* }
* },
* config_prefix = "{{ entity }}",
* config_prefix = "{{ entity_name }}",
* admin_permission = "administer site configuration",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "{{ entity }}.edit",
* "delete-form" = "{{ entity }}.delete"
* "edit-form" = "{{ entity_name }}.edit",
* "delete-form" = "{{ entity_name }}.delete"
* }
* )
*/
class {{ entity|capitalize }} extends ConfigEntityBase implements {{ entity|capitalize }}Interface
class {{ entity_class }} extends ConfigEntityBase implements {{ entity_class }}Interface
{
/**
* The {{ entity|capitalize }} ID.
* The {{ entity_class }} ID.
*
* @var string
*/
public $id;
/**
* The {{ entity|capitalize }} UUID.
* The {{ entity_class }} UUID.
*
* @var string
*/
public $uuid;
/**
* The {{ entity|capitalize }} label.
* The {{ entity_class }} label.
*
* @var string
*/
public $label;
// Your specific configuration property get/set methods go here,
// TODO: Your specific configuration property get/set methods go here,
// implementing the interface.
}
2 changes: 1 addition & 1 deletion src/Resources/skeleton/module/Form/entity-delete.php.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @file
* Contains Drupal\{{module}}\Form\{{ entity|capitalize }}DeleteForm.
* Contains Drupal\{{module}}\Form\{{ entity_class }}DeleteForm.
*/
namespace Drupal\{{module}}\Form;
Expand Down

0 comments on commit d78054f

Please sign in to comment.