function profile_update_8009 in Profile 8
Update revision fields.
File
- ./
profile.install, line 167 - Install, update and uninstall functions for the profile module.
Code
function profile_update_8009() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $definition_update_manager
->getEntityType('profile');
$revision_metadata_keys = $entity_type
->get('revision_metadata_keys');
$revision_metadata_keys += [
'revision_created' => 'revision_created',
'revision_user' => 'revision_user',
'revision_log_message' => 'revision_log_message',
];
$entity_type
->set('revision_metadata_keys', $revision_metadata_keys);
$definition_update_manager
->updateEntityType($entity_type);
$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(), 'profile', $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')
->setInitialValueFromField('uid')
->setRevisionable(TRUE);
$definition_update_manager
->installFieldStorageDefinition('revision_user', $entity_type
->id(), 'profile', $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(), 'profile', $revision_log_message);
// Use the "changed" timestamp as the initial revision_created.
$database = \Drupal::database();
$database
->update($entity_type
->getRevisionTable())
->expression('revision_created', 'changed')
->execute();
return t('The revision metadata fields have been added to profiles.');
}