Let's say I have a page with a content area, and there are couple of blocks predefined (e.g. by administrator). Is it possible to prevent regular editors (who can edit the page) from removing those block from content area?
They still should be able to create/add blocks, but I definitely don't want them to remove some of them.
You could create a custom validator, as described here:https://www.jondjones.com/learn-episerver-cms/episerver-developers-tutorials/episerver-content-areas/how-to-create-a-custom-content-area-validator-in-episerver/
I am not sure exactly what logic you want, but it the blocks should always be there, maybe (pseudo-code):
if (!user.isAdmin() && !contentArea.containsRequiredBlocks())
return new ValidationError("Stop deleting the blocks, common user!");
You could also place the content area on a tab only available to administrators. That way only administrators will be able to delete the blocks.You can create another content area for the other users, on a tab where everyone can access the property.
Not sure if this fits your use case, though.
You could hook into the OnSavingContent-event, then load the previous version of the page using IContentVersionRepository and then compare the content area of the different versions to see if any items have been removed.
If you detect that an item has been removed that shouldn't have, you can trigger a validation error and cancel the save.
What would be the experience for the editor with the validator approach? I think he would be able to remove the block, validation error would occur and in order to be able to save the page, he would need to restore the block manually. Or revert page version. Correct?
I would personally just put the configuration of the default blocks somewhere other than the page then render them both out. I did this on a previous project and it worked well.