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

[generate:entity:content] Start using show_revision_ui Entity Annotation Key #4000

Closed
AaronMcHale opened this issue Feb 7, 2019 · 3 comments

Comments

@AaronMcHale
Copy link

AaronMcHale commented Feb 7, 2019

Problem/Motivation

Drupal 8.3 introduced a new key in the Content Entity Annotation show_revision_ui (see https://www.drupal.org/node/2835025) eliminating the need for Drupal Console to add some custom code and logic to the Entity Form.

Solution

When Drupal Console generates Content Entities this key should be added and set to TRUE if the user specifies that they would like revision.

The result is that the whole buildForm method in the generated ContentEntityForm class can be removed, or if not all of it just the field which adds the Create new revision checkbox, assuming the later this would leave:

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    // Add custom form elements here

    $entity = $this->entity;

    return $form;
  }

In the save method of the generated ContentEntityForm class will need to be modified slightly, the checkbox which allows the user to choose if they want to create a new revision is now named simply revision, not new_revision:

if (!$form_state->isValueEmpty('new_revision') && $form_state->getValue('new_revision') != FALSE) {

Should be changed to:

if (!$form_state->isValueEmpty('revision') && $form_state->getValue('revision') != FALSE) {

There may be additional code which can also be removed, or some code which may need to change, but I haven't found anything else yet..

@AaronMcHale
Copy link
Author

While I'm on the subject of improving revisioning, currently with Drupal Console generated Content Entities, if you edit a Entity and choose not to create a new revision, but the Entity already has a revision with a log message, the log message will be erased.

To solve this we should add this override method to the generated Entity class (taken directly from the Node module with slight modification):

  /**
   * {@inheritdoc}
   */
  public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
    parent::preSaveRevision($storage, $record);

    if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log_message) || $record->revision_log_message === '')) {
      // If we are updating an existing entity without adding a new revision, we
      // need to make sure $entity->revision_log_message is reset whenever it is
      // empty.  Therefore, this code allows us to avoid clobbering an existing
      // log entry with an empty one.
      $record->revision_log_message = $this->original->revision_log_message->value;
    }
  }

@AaronMcHale
Copy link
Author

Regarding my previous comment, came across this core issue which will probably address that at a higher level https://www.drupal.org/project/drupal/issues/1863258

I suppose it comes down to, add the override of preSaveRevision now and remove later, or just don't bother and assume it'll be fixed eventually in that core issue.

@enzolutions
Copy link
Contributor

@AaronMcHale Could you tell us if this issue was resolved in the latest release of Drupal 8.7.x?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants