From 6fb60046c50b4514fc4b7a723010b3271e42e779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harold=20Ju=C3=A1rez?= Date: Sat, 22 Jun 2019 16:39:32 -0600 Subject: [PATCH] [generate:entity:content] Added owner option (#4098) * [update:execute] Fixed update table * Revert "Merge remote-tracking branch 'upstream/master'" This reverts commit ddf77395b32e35f259e9b1503eedb71b71c52604, reversing changes made to a95b7e60be2b2cd53d5371f2cc2c0d976e1c1c19. * [generate:entity:content] Added owner option --- src/Command/Generate/EntityContentCommand.php | 28 +++++++++++++++---- .../module/src/Entity/entity-content.php.twig | 14 +++++++++- .../Entity/interface-entity-content.php.twig | 6 ++-- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/Command/Generate/EntityContentCommand.php b/src/Command/Generate/EntityContentCommand.php index 581d82428..d6a8a583e 100644 --- a/src/Command/Generate/EntityContentCommand.php +++ b/src/Command/Generate/EntityContentCommand.php @@ -102,8 +102,14 @@ protected function configure() null, InputOption::VALUE_NONE, $this->trans('commands.generate.entity.content.options.has-forms') - ) - ->setAliases(['geco']); + ); + + $this->addOption( + 'has-owner', + null, + InputOption::VALUE_NONE, + $this->trans('commands.generate.entity.content.options.has-owner') + )->setAliases(['geco']); } /** @@ -143,6 +149,13 @@ protected function interact(InputInterface $input, OutputInterface $output) true ); $input->setOption('has-forms', $has_forms); + + // --has-owner option + $has_owner = $this->getIo()->confirm( + $this->trans('commands.generate.entity.content.questions.has-owner'), + true + ); + $input->setOption('has-owner', $has_owner); } /** @@ -158,9 +171,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $base_path = $input->getOption('base-path')?:'/admin/structure'; $learning = $input->getOption('learning')?:false; $bundle_entity_type = $has_bundles ? $entity_name . '_type' : null; - $is_translatable = $input->getOption('is-translatable')? : true; - $revisionable = $input->getOption('revisionable')? :false; - $has_forms = $input->getOption('has-forms')?:true; + $is_translatable = $input->getOption('is-translatable'); + $revisionable = $input->getOption('revisionable'); + $has_forms = $input->getOption('has-forms'); + $has_owner = $input->getOption('has-owner'); $generator = $this->generator; @@ -178,6 +192,7 @@ protected function execute(InputInterface $input, OutputInterface $output) 'is_translatable' => $is_translatable, 'revisionable' => $revisionable, 'has_forms' => $has_forms, + 'has_owner' => $has_owner, ]); if ($has_bundles) { @@ -187,7 +202,8 @@ protected function execute(InputInterface $input, OutputInterface $output) '--entity-class' => $entity_class . 'Type', '--entity-name' => $entity_name . '_type', '--label' => $label . ' type', - '--bundle-of' => $entity_name + '--bundle-of' => $entity_name, + '--no-interaction' ] ); } diff --git a/templates/module/src/Entity/entity-content.php.twig b/templates/module/src/Entity/entity-content.php.twig index c332c4b59..b2fc784b8 100644 --- a/templates/module/src/Entity/entity-content.php.twig +++ b/templates/module/src/Entity/entity-content.php.twig @@ -9,7 +9,9 @@ namespace Drupal\{{ module }}\Entity; {% endblock %} {% block use_class %} +{% if has_owner %} use Drupal\Core\Entity\EntityStorageInterface; +{% endif %} use Drupal\Core\Field\BaseFieldDefinition; {% if revisionable %} use Drupal\Core\Entity\RevisionableContentEntityBase; @@ -20,7 +22,9 @@ use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityChangedTrait; use Drupal\Core\Entity\EntityPublishedTrait; use Drupal\Core\Entity\EntityTypeInterface; +{% if has_owner %} use Drupal\user\UserInterface; +{% endif %} {% endblock %} {% block class_declaration %} @@ -79,7 +83,9 @@ use Drupal\user\UserInterface; {% endif %} * "label" = "name", * "uuid" = "uuid", +{% if has_owner %} * "uid" = "user_id", +{% endif %} * "langcode" = "langcode", * "published" = "status", * }, @@ -124,6 +130,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB {% endblock %} {% block class_methods %} +{% if has_owner %} /** * {@inheritdoc} @@ -134,6 +141,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB 'user_id' => \Drupal::currentUser()->id(), ]; } +{% endif %} {% if revisionable %} /** @@ -157,8 +165,8 @@ 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); @@ -206,6 +214,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB $this->set('created', $timestamp); return $this; } +{% if has_owner %} /** * {@inheritdoc} @@ -236,6 +245,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB $this->set('user_id', $account->id()); return $this; } +{% endif %} /** * {@inheritdoc} @@ -245,6 +255,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB // Add the published field. $fields += static::publishedBaseFieldDefinitions($entity_type); +{% if has_owner %} $fields['user_id'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Authored by')) @@ -272,6 +283,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB ]) ->setDisplayConfigurable('form', TRUE) ->setDisplayConfigurable('view', TRUE); +{% endif %} $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t('Name')) diff --git a/templates/module/src/Entity/interface-entity-content.php.twig b/templates/module/src/Entity/interface-entity-content.php.twig index 4f9e3795a..e33397578 100644 --- a/templates/module/src/Entity/interface-entity-content.php.twig +++ b/templates/module/src/Entity/interface-entity-content.php.twig @@ -15,7 +15,9 @@ use Drupal\Core\Entity\RevisionLogInterface; {% endif %} use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\EntityPublishedInterface; +{% if has_owner %} use Drupal\user\EntityOwnerInterface; +{% endif %} {% endblock %} {% block class_declaration %} @@ -24,7 +26,7 @@ use Drupal\user\EntityOwnerInterface; * * @ingroup {{module}} */ -interface {{ entity_class }}Interface extends ContentEntityInterface{% if revisionable %}, RevisionLogInterface{% endif %}, EntityChangedInterface, EntityPublishedInterface, EntityOwnerInterface {% endblock %} +interface {{ entity_class }}Interface extends ContentEntityInterface{% if revisionable %}, RevisionLogInterface{% endif %}, EntityChangedInterface, EntityPublishedInterface{% if has_owner %}, EntityOwnerInterface{% endif %} {% endblock %} {% block class_methods %} /** * Add get/set methods for your configuration properties here. @@ -67,8 +69,8 @@ interface {{ entity_class }}Interface extends ContentEntityInterface{% if revisi * The called {{ label }} entity. */ public function setCreatedTime($timestamp); - {% if revisionable %} + /** * Gets the {{ label }} revision creation timestamp. *