You are here

function bibcite_entity_update_8013 in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/bibcite_entity.install \bibcite_entity_update_8013()

Make Reference entity type revisionable. Enable Entity module.

File

modules/bibcite_entity/bibcite_entity.install, line 444
Module installation hooks implementation.

Code

function bibcite_entity_update_8013() {

  // Enable Entity module.

  /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
  $module_installer = \Drupal::service('module_installer');
  $module_installer
    ->install([
    'entity',
  ]);
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  // Add the published entity key and revisionable metadata fields to the
  // my_entity entity type.
  $entity_type = $definition_update_manager
    ->getEntityType('bibcite_reference');
  $entity_type_id = $entity_type
    ->id();
  $entity_keys = $entity_type
    ->getKeys();
  $entity_keys['published'] = 'status';
  $entity_type
    ->set('entity_keys', $entity_keys);
  $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);
  $definition_update_manager
    ->updateEntityType($entity_type);

  // Add the status field.
  $status = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Publishing status'))
    ->setDescription(t('A boolean indicating the published state.'))
    ->setRevisionable(TRUE)
    ->setDefaultValue(TRUE);
  $status
    ->setInitialValue(TRUE);
  $definition_update_manager
    ->installFieldStorageDefinition('status', $entity_type_id, $entity_type_id, $status);

  // 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);
}