Skip to content

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

Closed
@AaronMcHale

Description

@AaronMcHale

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..

Activity

AaronMcHale

AaronMcHale commented on Feb 7, 2019

@AaronMcHale
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

AaronMcHale commented on Feb 7, 2019

@AaronMcHale
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

enzolutions commented on May 9, 2019

@enzolutions
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @LOBsTerr@enzolutions@AaronMcHale

        Issue actions

          [generate:entity:content] Start using show_revision_ui Entity Annotation Key · Issue #4000 · hechoendrupal/drupal-console