'Add an unique class to every wp block on save

How can I add a unique class name to every gutenberg block on save? I tried doing this with php using a combination of wp_after_insert_post, parse_blocks, wp_update_post and something like (simplified):

$block['attrs']['className'] = $block['attrs']['className'] .' ' . substr(md5(uniqid(rand(1,6))), 0, 8);

But even though the classes are saved in the db, they don't show in neither the front end or the block editor.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source