2

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?

  • Thanks @berramou, I added some details of how I built the entity. – greggles Dec 30 '19 at 21:05
5

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.

2

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, privacy policy and cookie policy

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