Unique id for collection fields to keep track of renamed or deleted fields

Renaming or deleting a field of a collection doesn’t change existing entries and their values get fetched with each api call. There are a few issues (#775, #843 and #821) and a support question about this topic.

I thought about it again and I think I found a solution.

Revisions for collections would be a nice feature, but it would still work without revisions.

Each field just needs a unique id and we need a trigger before the collection is updated here

$this->app->trigger('collections.updatecollection.before', [$collection, $data]);
$this->app->trigger("collections.updatecollection.{$name}.before", [$collection, $data]);

Now it would be possible to write an addon or a custom bootstrap.php with something like

<?php
$app->on('collections.updatecollection.idtest.before', function($collection, $data){
    
    // compare $collection with $data
    
    // be aware, that "_mby", "_by", "_modified", "_created", "_id" could be visible fields, too
    // there might be other hidden values from addons...
    
    $col_id = array_column($collection['fields'], 'name', '_id');
    $data_id = array_column($data['fields'], 'name', '_id');
    
    $diff = array_diff_assoc($col_id, $data_id);
    
    foreach ($diff as $id => $name) {
        
        if (!array_key_exists($id, $data_id) {
            // field is deleted
            // request to delete all these fields in collection
        } else {
            // field still exists, but name changed
            // request to copy all fields from all entries in collection from 'old name' to 'new name'
            // and to delete 'old name'
        }
        
    }
    
};

I didn’t test the code, but I hope, the idea is understandable.