0

I created a custom entity, I can edit, save, delete, create revisions and revert. I adapted the code from node module to achieve all this and everything works just fine except the revision deletion.

This code will delete entries from my_entity_revision table but not from my_entity_revision_field_data table:

$this->myEntityStorage->deleteRevision($revision_id)

Any ideas why this might happen? Thanks.

Here is my entity annotation:

@ContentEntityType(
   id = "my_entity",
   label = @Translation("My entity"),
   bundle_label = @Translation("My Entity type"),
   handlers = {
     "list_builder" = "Drupal\my_entity\Controller\MyEntityListBuilder",
     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
     "access" = "Drupal\website_settings\MyEntityAccessControlHandler",
     "form" = {
       "default" = "Drupal\my_entity\Form\MyEntityForm",
       "add" = "Drupal\my_entity\Form\MyEntityForm",
       "edit" = "Drupal\my_entity\Form\MyEntityForm",
       "delete" = "Drupal\my_entity\Form\MyEntityDeleteForm"
     },
     "views_data" = "Drupal\my_entity\MyEntityViewsData",
   },
   base_table = "my_entity",
   data_table = "my_entity_field_data",
   revision_table = "my_entity_revision",
   revision_data_table = "my_entity_revision_field_data",
   translatable = TRUE,
   entity_revision_parent_id_field = "parent_id",
   entity_keys = {
     "id" = "id",
     "uuid" = "uuid",
     "bundle" = "type",
     "langcode" = "langcode",
     "revision" = "revision_id"
   },
   bundle_entity_type = "my_entity_type",
   field_ui_base_route = "entity.my_entity_type.edit_form",
   links = {
     "canonical" = "/admin/structure/my_entity/{my_entity}",
     "edit-form" = "/admin/structure/my_entity/{my_entity}/edit",
     "delete-form" = "/admin/structure/my_entity/{my_entity}/delete",
     "revision" = "/admin/structure/my_entity/{my_entity}/revision/{my_entity_revision}/view",
     "version-history" = "/admin/structure/my_entity/{my_entity}/revisions"
   }
)

And my baseFieldDefinition:

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

    // Standard field, used as unique if primary index.
    $fields['id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the Website settings entity.'))
      ->setReadOnly(TRUE);

    // Standard field, unique outside of the scope of the current project.
    $fields['uuid'] = BaseFieldDefinition::create('uuid')
      ->setLabel(t('UUID'))
      ->setDescription(t('The UUID of the Website settings entity.'))
      ->setReadOnly(TRUE);

    $fields['revision_id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Revision ID'))
      ->setDescription(t('The paragraphs entity revision ID.'))
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);

    $fields['type'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Type'))
      ->setDescription(t('The website settings bundle type.'))
      ->setSetting('target_type', 'website_settings_type')
      ->setReadOnly(TRUE);

    $fields['langcode'] = BaseFieldDefinition::create('language')
      ->setLabel(t('Language code'))
      ->setDescription(t('The paragraphs entity language code.'))
      ->setRevisionable(TRUE);

    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Authored by'))
      ->setDescription(t('The user ID of the website settings author.'))
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->setDefaultValueCallback('Drupal\website_settings\Entity\WebsiteSettings::getCurrentUserId')
      ->setTranslatable(TRUE)
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'hidden',
        'weight' => 0,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'hidden',
        'weight' => 0,
      ))
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE);

    // We set display options for the view as well as the form.
    // Users with correct privileges can change the view and edit configuration.
    $fields['title'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setSettings(array(
        'default_value' => '',
        'max_length' => 255,
        'text_processing' => 0,
      ))
      ->setDisplayOptions('view', array(
        'label' => 'above',
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'string_textfield',
        'weight' => -6,
      ))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['status'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Published'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDefaultValue(TRUE)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Authored on'))
      ->setDescription(t('The time that the website settings was created.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDisplayOptions('view', array(
        'type' => 'hidden',
        'weight' => 0,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'hidden',
        'weight' => 0,
      ))
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE);

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed on'))
      ->setDescription(t('The time that the website settings was last updated.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDisplayOptions('view', array(
        'type' => 'hidden',
        'weight' => 0,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'hidden',
        'weight' => 0,
      ))
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE);

    $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Revision user ID'))
      ->setDescription(t('The user ID of the author of the current revision.'))
      ->setSetting('target_type', 'user')
      ->setQueryable(FALSE)
      ->setRevisionable(TRUE);

    $fields['parent_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Parent ID'))
      ->setDescription(t('The ID of the parent entity of which this entity is referenced.'))
      ->setSetting('target_type', 'my_entity')
      ->setSetting('handler', 'default')
      ->setRevisionable(TRUE);

    $fields['langcode'] = BaseFieldDefinition::create('language')
      ->setLabel(t('Language code'))
      ->setDescription(t('The language code of ContentEntityExample entity.'));

    return $fields;
  }
  • You should share at least your entity class and its annotation. – Berdir Oct 12 '16 at 18:06
  • I have added the annotation and defaultBaseFields funciton. – Marius Ilie Oct 13 '16 at 6:25
0

Is your entity extending the RevisionableInterface and RevisionLogInterface or the ContentEntityInterface ?

Otherwise it could be a drupal core issue ..

Other having the same (or is this you ?): https://www.drupal.org/node/2818053

Known issue (only with media): https://www.drupal.org/node/1239558

  • I am extending the ContentEntityInterface which extends the RevisionableInterface. It looks like it's a drupal bug. The rows in node_field_revisions table are not deleted as well. – Marius Ilie Oct 19 '16 at 6:46
  • have you checked @Berdir answer ? drupal.stackexchange.com/questions/95200/… – rémy Oct 19 '16 at 7:06

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.