2

I am trying to make an existing entity type (knowledge), from the Knowledge Project, revisionable. I followed these instructions Converting a content entity type to be revisionable. A new table, knowledge_revision, is not created, and and the fields are added to the knowledge table. I am lost on what to do next.

update code


use Drupal\Core\Field\BaseFieldDefinition;

$definition_update_manager = \Drupal::entityDefinitionUpdateManager();

// Add the published entity key and revisionable metadata fields to the
// knowledge entity type.
$entity_type = $definition_update_manager->getEntityType('knowledge');
$entity_type_id = $entity_type->id();

$revision_metadata_keys = [
  'revision_user' => 'revision_user',
  'revision_created' => 'revision_created',
  'revision_log_message' => 'revision_log_message'
];
$entity_type->set('revision_metadata_keys', $revision_metadata_keys);

$entity_type->set('show_revision_ui', TRUE);

$definition_update_manager->updateEntityType($entity_type);

// Add the revision metadata fields.
$revision_created = BaseFieldDefinition::create('created')
  ->setLabel(t('Revision create time'))
  ->setDescription(t('The time that the current revision was created.'))
  ->setRevisionable(TRUE);
$definition_update_manager->installFieldStorageDefinition('revision_created', $entity_type_id, $entity_type_id, $revision_created);

$revision_user = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Revision user'))
  ->setDescription(t('The user ID of the author of the current revision.'))
  ->setSetting('target_type', 'user')
  ->setRevisionable(TRUE);
$definition_update_manager->installFieldStorageDefinition('revision_user', $entity_type_id, $entity_type_id, $revision_user);

$revision_log_message = BaseFieldDefinition::create('string_long')
  ->setLabel(t('Revision log message'))
  ->setDescription(t('Briefly describe the changes you have made.'))
  ->setRevisionable(TRUE)
  ->setDefaultValue('')
  ->setDisplayOptions('form', [
    'type' => 'string_textarea',
    'weight' => 25,
    'settings' => [
      'rows' => 4,
    ],
  ]);
$definition_update_manager->installFieldStorageDefinition('revision_log_message', $entity_type_id, $entity_type_id, $revision_log_message);

Drupal\knowledge\Entity\Knowledge

/**
 * Defines the knowledge entity class.
 *
 * @ContentEntityType(
 *   id = "knowledge",
 *   label = @Translation("Knowledge"),
 *   label_singular = @Translation("knowledge"),
 *   label_plural = @Translation("knowledge"),
 *   label_count = @PluralTranslation(
 *     singular = "@count knowledge",
 *     plural = "@count knowledge",
 *   ),
 *   bundle_label = @Translation("Knowledge type"),
 *   handlers = {
 *     "storage" = "Drupal\knowledge\KnowledgeStorage",
 *     "storage_schema" = "Drupal\knowledge\KnowledgeStorageSchema",
 *     "access" = "Drupal\knowledge\KnowledgeAccessControlHandler",
 *     "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
 *     "view_builder" = "Drupal\knowledge\KnowledgeViewBuilder",
 *     "views_data" = "Drupal\knowledge\KnowledgeViewsData",
 *     "form" = {
 *       "default" = "Drupal\knowledge\KnowledgeForm",
 *       "delete" = "Drupal\knowledge\Form\DeleteForm",
 *       "unlink" = "Drupal\knowledge\Form\UnlinkForm",
 *     },
 *   },
 *   base_table = "knowledge",
 *   revision_table = "knowledge_revision",
 *   show_revision_ui = TRUE,
 *   uri_callback = "knowledge_uri",
 *   translatable = FALSE,
 *   entity_keys = {
 *     "id" = "kid",
 *     "bundle" = "knowledge_type",
 *     "uuid" = "uuid",
 *     "published" = "status",
 *     "owner" = "uid",
 *     "revision" = "revision_id",
 *   },
 *   revision_metadata_keys = {
 *     "revision_user" = "revision_user",
 *     "revision_created" = "revision_created",
 *     "revision_log_message" = "revision_log_message",
 *   },
 *   links = {
 *     "canonical" = "/knowledge/{knowledge}",
 *     "delete-form" = "/knowledge/{knowledge}/delete",
 *     "delete-multiple-form" = "/admin/content/knowledge/delete",
 *     "edit-form" = "/knowledge/{knowledge}/edit",
 *     "create" = "/knowledge",
 *     "unlink-form" = "/knowledge/{knowledge}/unlink",
 *   },
 *   bundle_entity_type = "knowledge_type",
 *   field_ui_base_route  = "entity.knowledge_type.edit_form",
 * )
 */
class Knowledge extends EditorialContentEntityBase implements KnowledgeInterface {
 ...
  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
    $fields = parent::baseFieldDefinitions($entity_type);
    // $fields += static::publishedBaseFieldDefinitions($entity_type);
    $fields += static::ownerBaseFieldDefinitions($entity_type);

    $fields['kid']->setLabel(t('Knowledge ID'))
      ->setDescription(t('The knowledge ID.'));

    $fields['uuid']->setDescription(t('The knowledge UUID.'));

    $fields['knowledge_type']->setLabel(t('Knowledge Type'))
      ->setDescription(t('The knowledge type.'));

    // Set the default value callback for the status field.
    $fields['status']->setDefaultValueCallback('Drupal\knowledge\Entity\Knowledge::getDefaultStatus');

    $fields['uid']
      ->setDescription(t('The user ID of the knowledge author.'));

    $fields['field_name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Knowledge field name'))
      ->setRequired(TRUE)
      ->setDescription(t('The field name through which this knowledge was added.'))
      ->setSetting('is_ascii', TRUE)
      ->setSetting('max_length', FieldStorageConfig::NAME_MAX_LENGTH);

    $fields['entity_type'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Entity type'))
      ->setRequired(TRUE)
      ->setRevisionable(TRUE)
      ->setDescription(t('The entity type to which this knowledge is attached.'))
      ->setSetting('is_ascii', TRUE)
      ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);

    $fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Entity ID'))
      ->setDescription(t('The ID of the entity of which this knowledge is a reply.'))
      ->setRevisionable(TRUE)
      ->setRequired(TRUE);

    $fields['incident_type'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Incident type'))
      ->setRequired(TRUE)
      ->setDescription(t('The entity type to which this knowledge is attached.'))
      ->setSetting('is_ascii', TRUE)
      ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);

    $fields['incident_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Incident Id'))
      ->setDescription(t('The ID of the support case.'))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setRequired(TRUE);

    $fields['citation'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Citation'))
      ->setDescription(t("The owner linked to another's articled."))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setRevisionable(TRUE)
      ->setRequired(TRUE);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
      ->setDescription(t('The time that the knowledge was created.'))
      ->setTranslatable(TRUE);

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the knowledge was last edited.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);

    return $fields;
  }
...
}

0

Your Answer

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

Browse other questions tagged or ask your own question.