3

I made a custom entity that extends RevisionableContentEntityBase. I can use the web interface to create entities. If I edit an entity and check the box Create new revision then a new revision will be created. There is no configuration form available to set the default for Create new revision.

The custom entity is quite simple. It has a few base fields and was built using drush generate module-content-entity.

How do I make my custom entity create a new revision by default?

0

2 Answers 2

7

Normally the default value is stored in bundle configurations. If you have no bundles then override getNewRevisionDefault() of your content entity form:

class MyEntityForm extends ContentEntityForm {

  protected function getNewRevisionDefault() {
    return TRUE;
  }

}

This class should already exist when you've generated the entity with Drush/Console. Check in the entity annotations that the class is used as form handler.

4

In addition to 4k4's answer here, which is very good and specific, a coworker helped me arrive at another solution in overriding ContentEntityForm::buildForm function:

class MyEntityForm extends ContentEntityForm {

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['revision'] = [
      '#type' => 'value',
      '#default_value' => TRUE,
    ];
  }
}

The benefit of overriding the buildForm function is you can have a bit more form-specific logic, e.g. making it a hidden value or a checkbox.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.