function paragraphs_library_update_8005 in Paragraphs 8
Add author and revision fields.
File
- modules/
paragraphs_library/ paragraphs_library.install, line 203 - Install/Update hooks for paragraphs library.
Code
function paragraphs_library_update_8005() {
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $entity_definition_update_manager
->getEntityType('paragraphs_library_item');
$revision_metadata_keys = $entity_type
->get('revision_metadata_keys');
$revision_metadata_keys += [
'revision_created' => 'revision_created',
'revision_user' => 'revision_uid',
'revision_log_message' => 'revision_log',
];
$entity_type
->set('revision_metadata_keys', $revision_metadata_keys);
$entity_definition_update_manager
->updateEntityType($entity_type);
$uid = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of the library item author.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\paragraphs_library\\Entity\\LibraryItem::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'region' => 'hidden',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
$created = BaseFieldDefinition::create('created')
->setLabel(t('Revision create time'))
->setDescription(t('The time that the current revision was created.'))
->setRevisionable(TRUE);
$revision_uid = 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);
$has_content_translation_uid_field = \Drupal::moduleHandler()
->moduleExists('content_translation') && $entity_definition_update_manager
->getFieldStorageDefinition('content_translation_uid', $entity_type
->id());
if ($has_content_translation_uid_field) {
$uid
->setInitialValueFromField('content_translation_uid');
$revision_uid
->setInitialValueFromField('content_translation_uid');
}
else {
$uid
->setInitialValue(0);
$revision_uid
->setInitialValue(0);
}
$entity_definition_update_manager
->installFieldStorageDefinition('uid', $entity_type
->id(), 'paragraphs_library', $uid);
$entity_definition_update_manager
->installFieldStorageDefinition('revision_created', $entity_type
->id(), 'paragraphs_library', $created);
$entity_definition_update_manager
->installFieldStorageDefinition('revision_uid', $entity_type
->id(), 'paragraphs_library', $revision_uid);
$database = \Drupal::database();
$result = $database
->query('SELECT revision_id, MIN(changed) as changed FROM {paragraphs_library_item_field_data} GROUP BY revision_id');
foreach ($result as $row) {
$database
->update('paragraphs_library_item_revision')
->fields([
'revision_created' => $row->changed,
])
->condition('revision_id', $row->revision_id)
->execute();
}
// Uninstall the 'content_translation_uid' field if needed.
if ($has_content_translation_uid_field) {
// First we have to remove the field data.
$database
->update($entity_type
->getDataTable())
->fields([
'content_translation_uid' => NULL,
])
->execute();
$database
->update($entity_type
->getRevisionDataTable())
->fields([
'content_translation_uid' => NULL,
])
->execute();
$content_translation_status = $entity_definition_update_manager
->getFieldStorageDefinition('content_translation_uid', $entity_type
->id());
$entity_definition_update_manager
->uninstallFieldStorageDefinition($content_translation_status);
}
}