From f5fd6de208cffd37588b785605cc2280738492c2 Mon Sep 17 00:00:00 2001 From: Nikolay Lobachev Date: Sun, 22 Apr 2018 21:32:38 +0200 Subject: [PATCH] 3275 remove translation code (#3822) * Remove translation related code from the classes and etc. * We keep langcode, because it is the default field --- src/Generator/EntityContentGenerator.php | 12 +++++++----- .../src/Controller/entity-controller.php.twig | 14 +++++++++++--- .../module/src/Entity/entity-content.php.twig | 8 +++++--- templates/module/src/entity-storage.php.twig | 4 ++++ .../module/src/interface-entity-storage.php.twig | 5 ++++- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Generator/EntityContentGenerator.php b/src/Generator/EntityContentGenerator.php index 158c0895f..90a5c0d80 100644 --- a/src/Generator/EntityContentGenerator.php +++ b/src/Generator/EntityContentGenerator.php @@ -182,11 +182,13 @@ public function generate(array $parameters) $moduleFormPath . 'RevisionDeleteForm.php', $parameters ); - $this->renderFile( - 'module/src/Entity/Form/entity-content-revision-revert-translation.php.twig', - $moduleFormPath . 'RevisionRevertTranslationForm.php', - $parameters - ); + if ($is_translatable) { + $this->renderFile( + 'module/src/Entity/Form/entity-content-revision-revert-translation.php.twig', + $moduleFormPath . 'RevisionRevertTranslationForm.php', + $parameters + ); + } $this->renderFile( 'module/src/Entity/Form/entity-content-revision-revert.php.twig', $moduleFormPath . 'RevisionRevertForm.php', diff --git a/templates/module/src/Controller/entity-controller.php.twig b/templates/module/src/Controller/entity-controller.php.twig index 7e340e0e3..cd15c8e01 100644 --- a/templates/module/src/Controller/entity-controller.php.twig +++ b/templates/module/src/Controller/entity-controller.php.twig @@ -65,15 +65,19 @@ class {{ entity_class }}Controller extends ControllerBase implements ContainerIn */ public function revisionOverview({{ entity_class }}Interface ${{ entity_name }}) { $account = $this->currentUser(); + ${{ entity_name }}_storage = $this->entityManager()->getStorage('{{ entity_name }}'); + +{% if is_translatable %} $langcode = ${{ entity_name }}->language()->getId(); $langname = ${{ entity_name }}->language()->getName(); $languages = ${{ entity_name }}->getTranslationLanguages(); $has_translations = (count($languages) > 1); - ${{ entity_name }}_storage = $this->entityManager()->getStorage('{{ entity_name }}'); - $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => ${{ entity_name }}->label()]) : $this->t('Revisions for %title', ['%title' => ${{ entity_name }}->label()]); - $header = [$this->t('Revision'), $this->t('Operations')]; +{% else %} + $build['#title'] = $this->t('Revisions for %title', ['%title' => ${{ entity_name }}->label()]); +{% endif %} + $header = [$this->t('Revision'), $this->t('Operations')]; $revert_permission = (($account->hasPermission("revert all {{ label|lower }} revisions") || $account->hasPermission('administer {{ label|lower }} entities'))); $delete_permission = (($account->hasPermission("delete all {{ label|lower }} revisions") || $account->hasPermission('administer {{ label|lower }} entities'))); @@ -86,9 +90,11 @@ class {{ entity_class }}Controller extends ControllerBase implements ContainerIn foreach (array_reverse($vids) as $vid) { /** @var \Drupal\{{ module }}\{{ entity_class }}Interface $revision */ $revision = ${{ entity_name }}_storage->loadRevision($vid); +{% if is_translatable %} // Only show revisions that are affected by the language that is being // displayed. if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) { +{% endif %} $username = [ '#theme' => 'username', '#account' => $revision->getRevisionUser(), @@ -161,7 +167,9 @@ class {{ entity_class }}Controller extends ControllerBase implements ContainerIn } $rows[] = $row; +{% if is_translatable %} } +{% endif %} } $build['{{ entity_name }}_revisions_table'] = [ diff --git a/templates/module/src/Entity/entity-content.php.twig b/templates/module/src/Entity/entity-content.php.twig index 2925ac629..a31b45599 100644 --- a/templates/module/src/Entity/entity-content.php.twig +++ b/templates/module/src/Entity/entity-content.php.twig @@ -64,9 +64,7 @@ use Drupal\user\UserInterface; * revision_table = "{{ entity_name }}_revision", * revision_data_table = "{{ entity_name }}_field_revision", {% endif %} -{% if is_translatable %} - * translatable = TRUE, -{% endif %} + * translatable = {{ is_translatable ? 'TRUE' : 'FALSE' }}, * admin_permission = "administer {{ label|lower }} entities", * entity_keys = { * "id" = "id", @@ -152,6 +150,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); + {% if is_translatable %} foreach (array_keys($this->getTranslationLanguages()) as $langcode) { $translation = $this->getTranslation($langcode); @@ -160,6 +159,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB $translation->setOwnerId(0); } } + {% endif %} // If no revision author has been set explicitly, make the {{ entity_name }} owner the // revision author. @@ -256,7 +256,9 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB ->setRevisionable(TRUE) ->setSetting('target_type', 'user') ->setSetting('handler', 'default') +{% if is_translatable %} ->setTranslatable(TRUE) +{% endif %} ->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'author', diff --git a/templates/module/src/entity-storage.php.twig b/templates/module/src/entity-storage.php.twig index 01aec1cc4..629e1a7d6 100644 --- a/templates/module/src/entity-storage.php.twig +++ b/templates/module/src/entity-storage.php.twig @@ -11,7 +11,9 @@ namespace Drupal\{{ module }}; {% block use_class %} use Drupal\Core\Entity\Sql\SqlContentEntityStorage; use Drupal\Core\Session\AccountInterface; +{% if is_translatable %} use Drupal\Core\Language\LanguageInterface; +{% endif %} use Drupal\{{ module }}\Entity\{{ entity_class }}Interface; {% endblock %} @@ -46,6 +48,7 @@ class {{ entity_class }}Storage extends SqlContentEntityStorage implements {{ en [':uid' => $account->id()] )->fetchCol(); } +{% if is_translatable %} /** * {@inheritdoc} @@ -64,4 +67,5 @@ class {{ entity_class }}Storage extends SqlContentEntityStorage implements {{ en ->condition('langcode', $language->getId()) ->execute(); } +{% endif %} {% endblock %} diff --git a/templates/module/src/interface-entity-storage.php.twig b/templates/module/src/interface-entity-storage.php.twig index 050b28708..6baf05cba 100644 --- a/templates/module/src/interface-entity-storage.php.twig +++ b/templates/module/src/interface-entity-storage.php.twig @@ -11,7 +11,9 @@ namespace Drupal\{{ module }}; {% block use_class %} use Drupal\Core\Entity\ContentEntityStorageInterface; use Drupal\Core\Session\AccountInterface; +{% if is_translatable %} use Drupal\Core\Language\LanguageInterface; +{% endif %} use Drupal\{{ module }}\Entity\{{ entity_class }}Interface; {% endblock %} @@ -48,7 +50,7 @@ interface {{ entity_class }}StorageInterface extends ContentEntityStorageInterfa * {{ label }} revision IDs (in ascending order). */ public function userRevisionIds(AccountInterface $account); - +{% if is_translatable %} /** * Counts the number of revisions in the default language. * @@ -67,4 +69,5 @@ interface {{ entity_class }}StorageInterface extends ContentEntityStorageInterfa * The language object. */ public function clearRevisionsLanguage(LanguageInterface $language); +{% endif %} {% endblock %}