Skip to content

Commit

Permalink
[generate:entity:content] Added owner option (#4098)
Browse files Browse the repository at this point in the history
* [update:execute] Fixed update table

* Revert "Merge remote-tracking branch 'upstream/master'"

This reverts commit ddf7739, reversing
changes made to a95b7e6.

* [generate:entity:content] Added owner option
  • Loading branch information
hjuarez20 authored and enzolutions committed Jun 22, 2019
1 parent 02498e8 commit 6fb6004
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
28 changes: 22 additions & 6 deletions src/Command/Generate/EntityContentCommand.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ protected function configure()
null, null,
InputOption::VALUE_NONE, InputOption::VALUE_NONE,
$this->trans('commands.generate.entity.content.options.has-forms') $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']);
} }


/** /**
Expand Down Expand Up @@ -143,6 +149,13 @@ protected function interact(InputInterface $input, OutputInterface $output)
true true
); );
$input->setOption('has-forms', $has_forms); $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);
} }


/** /**
Expand All @@ -158,9 +171,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$base_path = $input->getOption('base-path')?:'/admin/structure'; $base_path = $input->getOption('base-path')?:'/admin/structure';
$learning = $input->getOption('learning')?:false; $learning = $input->getOption('learning')?:false;
$bundle_entity_type = $has_bundles ? $entity_name . '_type' : null; $bundle_entity_type = $has_bundles ? $entity_name . '_type' : null;
$is_translatable = $input->getOption('is-translatable')? : true; $is_translatable = $input->getOption('is-translatable');
$revisionable = $input->getOption('revisionable')? :false; $revisionable = $input->getOption('revisionable');
$has_forms = $input->getOption('has-forms')?:true; $has_forms = $input->getOption('has-forms');
$has_owner = $input->getOption('has-owner');


$generator = $this->generator; $generator = $this->generator;


Expand All @@ -178,6 +192,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'is_translatable' => $is_translatable, 'is_translatable' => $is_translatable,
'revisionable' => $revisionable, 'revisionable' => $revisionable,
'has_forms' => $has_forms, 'has_forms' => $has_forms,
'has_owner' => $has_owner,
]); ]);


if ($has_bundles) { if ($has_bundles) {
Expand All @@ -187,7 +202,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
'--entity-class' => $entity_class . 'Type', '--entity-class' => $entity_class . 'Type',
'--entity-name' => $entity_name . '_type', '--entity-name' => $entity_name . '_type',
'--label' => $label . ' type', '--label' => $label . ' type',
'--bundle-of' => $entity_name '--bundle-of' => $entity_name,
'--no-interaction'
] ]
); );
} }
Expand Down
14 changes: 13 additions & 1 deletion templates/module/src/Entity/entity-content.php.twig
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace Drupal\{{ module }}\Entity;
{% endblock %} {% endblock %}


{% block use_class %} {% block use_class %}
{% if has_owner %}
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
{% endif %}
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
{% if revisionable %} {% if revisionable %}
use Drupal\Core\Entity\RevisionableContentEntityBase; use Drupal\Core\Entity\RevisionableContentEntityBase;
Expand All @@ -20,7 +22,9 @@ use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait; use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait; use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
{% if has_owner %}
use Drupal\user\UserInterface; use Drupal\user\UserInterface;
{% endif %}
{% endblock %} {% endblock %}


{% block class_declaration %} {% block class_declaration %}
Expand Down Expand Up @@ -79,7 +83,9 @@ use Drupal\user\UserInterface;
{% endif %} {% endif %}
* "label" = "name", * "label" = "name",
* "uuid" = "uuid", * "uuid" = "uuid",
{% if has_owner %}
* "uid" = "user_id", * "uid" = "user_id",
{% endif %}
* "langcode" = "langcode", * "langcode" = "langcode",
* "published" = "status", * "published" = "status",
* }, * },
Expand Down Expand Up @@ -124,6 +130,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB
{% endblock %} {% endblock %}


{% block class_methods %} {% block class_methods %}
{% if has_owner %}


/** /**
* {@inheritdoc} * {@inheritdoc}
Expand All @@ -134,6 +141,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB
'user_id' => \Drupal::currentUser()->id(), 'user_id' => \Drupal::currentUser()->id(),
]; ];
} }
{% endif %}
{% if revisionable %} {% if revisionable %}


/** /**
Expand All @@ -157,8 +165,8 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB
*/ */
public function preSave(EntityStorageInterface $storage) { public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage); parent::preSave($storage);

{% if is_translatable %} {% if is_translatable %}

foreach (array_keys($this->getTranslationLanguages()) as $langcode) { foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
$translation = $this->getTranslation($langcode); $translation = $this->getTranslation($langcode);


Expand Down Expand Up @@ -206,6 +214,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB
$this->set('created', $timestamp); $this->set('created', $timestamp);
return $this; return $this;
} }
{% if has_owner %}


/** /**
* {@inheritdoc} * {@inheritdoc}
Expand Down Expand Up @@ -236,6 +245,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB
$this->set('user_id', $account->id()); $this->set('user_id', $account->id());
return $this; return $this;
} }
{% endif %}


/** /**
* {@inheritdoc} * {@inheritdoc}
Expand All @@ -245,6 +255,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB


// Add the published field. // Add the published field.
$fields += static::publishedBaseFieldDefinitions($entity_type); $fields += static::publishedBaseFieldDefinitions($entity_type);
{% if has_owner %}


$fields['user_id'] = BaseFieldDefinition::create('entity_reference') $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by')) ->setLabel(t('Authored by'))
Expand Down Expand Up @@ -272,6 +283,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB
]) ])
->setDisplayConfigurable('form', TRUE) ->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE); ->setDisplayConfigurable('view', TRUE);
{% endif %}


$fields['name'] = BaseFieldDefinition::create('string') $fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name')) ->setLabel(t('Name'))
Expand Down
6 changes: 4 additions & 2 deletions templates/module/src/Entity/interface-entity-content.php.twig
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use Drupal\Core\Entity\RevisionLogInterface;
{% endif %} {% endif %}
use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityPublishedInterface; use Drupal\Core\Entity\EntityPublishedInterface;
{% if has_owner %}
use Drupal\user\EntityOwnerInterface; use Drupal\user\EntityOwnerInterface;
{% endif %}
{% endblock %} {% endblock %}


{% block class_declaration %} {% block class_declaration %}
Expand All @@ -24,7 +26,7 @@ use Drupal\user\EntityOwnerInterface;
* *
* @ingroup {{module}} * @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 %} {% block class_methods %}
/** /**
* Add get/set methods for your configuration properties here. * Add get/set methods for your configuration properties here.
Expand Down Expand Up @@ -67,8 +69,8 @@ interface {{ entity_class }}Interface extends ContentEntityInterface{% if revisi
* The called {{ label }} entity. * The called {{ label }} entity.
*/ */
public function setCreatedTime($timestamp); public function setCreatedTime($timestamp);

{% if revisionable %} {% if revisionable %}

/** /**
* Gets the {{ label }} revision creation timestamp. * Gets the {{ label }} revision creation timestamp.
* *
Expand Down

0 comments on commit 6fb6004

Please sign in to comment.