Skip to content
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

#2242 [generate:entity:content] Allow option to generate translatable content entities #2445

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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', 'admin/structure'],
['Foo', 'foo' . rand(), 'Bar', 'bar', 'admin/structure', 'true'],
];
}
}
6 changes: 4 additions & 2 deletions Test/Generator/EntityContentGeneratorTest.php
Expand Up @@ -30,7 +30,8 @@ public function testGenerateEntityContent(
$entity_name,
$entity_class,
$label,
$base_path
$base_path,
$is_translatable
) {
$generator = new EntityContentGenerator();
$this->getRenderHelper()->setSkeletonDirs($this->getSkeletonDirs());
Expand All @@ -42,7 +43,8 @@ public function testGenerateEntityContent(
$entity_name,
$entity_class,
$label,
$base_path
$base_path,
$is_translatable
);

$files = [
Expand Down
2 changes: 2 additions & 0 deletions config/translations/ca/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: Etiqueta
has-bundles: 'L''entitat conté bundles'
base-path: 'El camí base per les rutes de l''entitat de continguts'
is-translatable: 'Content entity translatable'
questions:
module: common.questions.module
entity-class: 'Introduïu la classe de la nova entitat de contingut'
entity-name: 'Introduïu el nom de la nova entitat de contingut'
label: 'Introduïu l''etiqueta de la nova entitat de contingut'
has-bundles: 'Vols que aquest contingut tingui bundles'
base-path: 'Introduïu el camí base per les rutes de l''entitat de contigut'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/en/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/es/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: Etiqueta
has-bundles: 'La entidad tiene bundles'
base-path: 'El directorio raíz para las rutas de la entidad de contenido'
is-translatable: 'Content entity translatable'
questions:
module: common.questions.module
entity-class: 'Proporcione la clase de la nueva entidad de contenido'
entity-name: 'Proporcione el nombre de la nueva entidad de contenido'
label: 'Proporcione la etiqueta de la nueva entidad de contenido'
has-bundles: 'Desea que esta entidad de contenido tenga bundles'
base-path: 'Proporcione el directorio raíz para las rutas de la entidad de contenido'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/fr/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'Le libellé'
has-bundles: 'Est-ce que l''entité possède des bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
questions:
module: common.questions.module
entity-class: 'Entrez le nom de votre classe pour votre nouvelle entité de contenu'
entity-name: 'Entrez le nom de votre nouvelle entité de contenu'
label: 'Entrez le libellé de votre nouvelle entité de contenu'
has-bundles: 'Voulez-vous que cette entité possède des bundles'
base-path: 'Enter the base-path for the content entity routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/gu/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/hi/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: लेबल
has-bundles: 'एंटिटि मॆ बंडल हे'
base-path: 'कॉन्फिग एंटिटी रुट्स के लिए बेस पथ'
is-translatable: 'Content entity translatable'
questions:
module: common.questions.module
entity-class: 'अपने नई कंटेंट एंटिटि की कक्षा लिखॆ'
entity-name: 'अपने नई कंटेंट एंटिटि का नाम लिखॆ'
label: 'अपने नई कंटेंट एंटिटि का लेबल लिखॆ'
has-bundles: 'आप (कंटेंट) एंटिटि मे बंडल चाहते हैं'
base-path: 'कॉन्फिग एंटिटी रुट्स के लिए बेस पथ दर्ज करे'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/hu/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'A felirat'
has-bundles: 'Az entitás nem rendelkezik mezőcsoportokkal'
base-path: 'A tartalom entitás útvonalak alapútvonala'
is-translatable: 'Content entity translatable'
questions:
module: common.questions.module
entity-class: 'Meg kell adni az új tartalom entitás osztályát'
entity-name: 'Meg kell adni az új tartalom entitás nevét'
label: 'Meg kell adni az új tartalom entitás feliratát'
has-bundles: 'Ez a tartalom entitás rendelkezzen mezőcsoportokkal?'
base-path: 'Meg kell adni a tartalom entitás útvonalak alapútvonalát'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/id/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/ja/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/ko/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/mr/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/pa/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/pt_br/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'O rótulo'
has-bundles: 'A entidade tem bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
questions:
module: common.questions.module
entity-class: 'Digite a classe da sua nova entidade de conteúdo'
entity-name: 'Digite o nome da sua nova entidade de conteúdo'
label: 'Digite o rótulo da sua nova entidade de conteúdo'
has-bundles: 'Deseja que esta entidade de conteúdo tenha bundles'
base-path: 'Enter the base-path for the content entity routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/ro/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: Eticheta
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
questions:
module: common.questions.module
entity-class: 'Introduceți clasa noii dvs. entități de conținut'
entity-name: 'Introduceți numele noii dvs. entități de conținut'
label: 'Introduceți eticheta noii dvs. entități de conținut'
has-bundles: 'Do you want this (content) entity to have bundles'
base-path: 'Enter the base-path for the content entity routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/ru/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/tl/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/vn/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: Nhãn
has-bundles: 'Entity có các bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
questions:
module: common.questions.module
entity-class: 'Nhập lớp của config entity mới của bạn'
entity-name: 'Nhập tên của config entity mới của bạn'
label: 'Nhập nhãn của config entity mới của bạn'
has-bundles: 'Bạn có muốn (content) entity này có bundles không'
base-path: 'Enter the base-path for the content entity routes'
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/zh_hans/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 标签
has-bundles: '实体包含 Bundles'
base-path: 内容实体路由的基本路径
is-translatable: 'Content entity translatable'
questions:
module: common.questions.module
entity-class: 输入新内容实体的类名
entity-name: 输入新内容实体的名称
label: 输入新内容实体的标签
has-bundles: '这个(内容)实体包含 Bundles 吗?'
base-path: 输入内容实体路由的基本路径
is-translatable: 'Is your entity translatable'
2 changes: 2 additions & 0 deletions config/translations/zh_hant/generate.entity.content.yml
Expand Up @@ -7,10 +7,12 @@ options:
label: 'The label'
has-bundles: 'Entity has bundles'
base-path: 'The base-path for the content entity routes'
is-translatable: 'Content entity translatable'
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 routes'
is-translatable: 'Is your entity translatable'
17 changes: 16 additions & 1 deletion src/Command/Generate/EntityContentCommand.php
Expand Up @@ -31,6 +31,13 @@ protected function configure()
InputOption::VALUE_NONE,
$this->trans('commands.generate.entity.content.options.has-bundles')
);

$this->addOption(
'is-translatable',
null,
InputOption::VALUE_NONE,
$this->trans('commands.generate.entity.content.options.is-translatable')
);
}

/**
Expand All @@ -50,6 +57,13 @@ protected function interact(InputInterface $input, OutputInterface $output)
);
$input->setOption('has-bundles', $bundle_of);
}

// --is-translatable option
$is_translatable = $io->confirm(
$this->trans('commands.generate.entity.content.questions.is-translatable'),
true
);
$input->setOption('is-translatable', $is_translatable);
}

/**
Expand All @@ -65,13 +79,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$base_path = $input->getOption('base-path');
$learning = $input->hasOption('learning')?$input->getOption('learning'):false;
$bundle_entity_name = $has_bundles ? $entity_name . '_type' : null;
$is_translatable = $input->hasOption('is-translatable') ? $input->getOption('is-translatable') : true;

$io = new DrupalStyle($input, $output);
$generator = $this->getGenerator();
$generator->setIo($io);
$generator->setLearning($learning);

$generator->generate($module, $entity_name, $entity_class, $label, $base_path, $bundle_entity_name);
$generator->generate($module, $entity_name, $entity_class, $label, $base_path, $is_translatable, $bundle_entity_name);

if ($has_bundles) {
$this->getChain()->addCommand(
Expand Down
12 changes: 11 additions & 1 deletion src/Generator/EntityContentGenerator.php
Expand Up @@ -17,9 +17,10 @@ class EntityContentGenerator extends Generator
* @param string $entity_class Entity class name
* @param string $label Entity label
* @param string $base_path Base path
* @param string $is_translatable Translation configuration
* @param string $bundle_entity_type (Config) entity type acting as bundle
*/
public function generate($module, $entity_name, $entity_class, $label, $base_path, $bundle_entity_type = null)
public function generate($module, $entity_name, $entity_class, $label, $base_path, $is_translatable, $bundle_entity_type = null)
{
$parameters = [
'module' => $module,
Expand All @@ -28,6 +29,7 @@ public function generate($module, $entity_name, $entity_class, $label, $base_pat
'label' => $label,
'bundle_entity_type' => $bundle_entity_type,
'base_path' => $base_path,
'is_translatable' => $is_translatable,
];

$this->renderFile(
Expand Down Expand Up @@ -64,6 +66,14 @@ public function generate($module, $entity_name, $entity_class, $label, $base_pat
$parameters
);

if ($is_translatable) {
$this->renderFile(
'module/src/entity-translation-handler.php.twig',
$this->getSite()->getSourcePath($module).'/'.$entity_class.'TranslationHandler.php',
$parameters
);
}

$this->renderFile(
'module/src/Entity/interface-entity-content.php.twig',
$this->getSite()->getEntityPath($module).'/'.$entity_class.'Interface.php',
Expand Down
5 changes: 5 additions & 0 deletions templates/module/src/Entity/entity-content.php.twig
Expand Up @@ -33,6 +33,9 @@ use Drupal\user\UserInterface;
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\{{ module }}\{{ entity_class }}ListBuilder",
* "views_data" = "Drupal\{{ module }}\Entity\{{ entity_class }}ViewsData",
{% if is_translatable %}
* "translation" = "Drupal\{{ module }}\{{ entity_class }}TranslationHandler",
{% endif %}
*
* "form" = {
* "default" = "Drupal\{{ module }}\Form\{{ entity_class }}Form",
Expand All @@ -46,8 +49,10 @@ use Drupal\user\UserInterface;
* },
* },
* base_table = "{{ entity_name }}",
{% if is_translatable %}
* data_table = "{{ entity_name }}_field_data",
* translatable = TRUE,
{% endif %}
* admin_permission = "administer {{ label|lower }} entities",
* entity_keys = {
* "id" = "id",
Expand Down
18 changes: 18 additions & 0 deletions templates/module/src/entity-translation-handler.php.twig
@@ -0,0 +1,18 @@
{% extends "base/class.php.twig" %}

{% block namespace_class %}
namespace Drupal\{{module}};
{% endblock %}

{% block use_class %}
use Drupal\content_translation\ContentTranslationHandler;
{% endblock %}

{% block class_declaration %}
/**
* Defines the translation handler for {{ entity_name }}.
*/
class {{ entity_class }}TranslationHandler extends ContentTranslationHandler {% endblock %}
{% block class_methods %}
// Override here the needed methods from ContentTranslationHandler.
{% endblock %}