function flag_lists_update_8004 in Flag Lists 4.0.x
Update the Related flag to share the new name.
File
- ./
flag_lists.install, line 131 - Contains install and updates for Flag Lists module.
Code
function flag_lists_update_8004() {
$flagService = \Drupal::service('flag');
$flagListsService = \Drupal::service('flaglists');
$database = \Drupal::database();
foreach ($flagListsService
->getAllFlaggingCollections() as $flagList) {
$entity_id = $flagList
->id();
$userId = $flagList
->getOwnerId();
$relatedFlag = $flagList
->getRelatedFlag();
$originalId = $relatedFlag
->id();
$newName = 'relflag_' . $entity_id . '_' . $userId;
if ($originalId != $newName) {
$database
->update('flagging')
->fields([
'flag_id' => $newName,
])
->condition('flag_id', $originalId)
->execute();
$database
->update('flag_counts')
->fields([
'flag_id' => $newName,
])
->condition('flag_id', $originalId)
->execute();
$database
->update('flagging_collection_field_data')
->fields([
'relatedflag' => $newName,
])
->condition('id', $entity_id)
->execute();
$database
->update('flagging_collection_field_revision')
->fields([
'relatedflag' => $newName,
])
->condition('id', $entity_id)
->execute();
$database
->update('flag_list_item_field_data')
->fields([
'baseflag' => $newName,
])
->condition('baseflag', $originalId)
->execute();
// Update the name of the Related Flag by creating a new
// and delete the old. The deletion must be done after all
// other updates, if not much more is deleted than expected!
$newRelatedFlag = $relatedFlag
->createDuplicate();
$newRelatedFlag
->set('id', $newName);
$relatedFlag
->delete();
$newRelatedFlag
->save();
}
}
$message = 'Updated the Flagging Collections Related flags to follow the new naming convention.';
return $message;
}