Make field readonly for certain user group

There are 2 parts to your request:

(1) Preventing the form field to be editable

Afaik there is currently no direct option you could utilize to change the collections form behavior.
You’re facing the same question as stated here Extend "view" class of Collections module

What you want is to extend the view and maybe append a JS snipped that turns your readonly-field into readonly for non-admins.

There is an event triggered when a template is rendered. You can hook into that event and extend the to-be-rendered-template-source.

Something along those lines

// config/bootstrap.php
<?php

$this->on('collections.entry.aside', function($collectionName){
  if(collectionName != 'YOUR_COLLECTION_NAME') return;
  
  echo '<script>document.querySelector('input[name="YOURFIELDNAME"]').readOnly = true;</script>';  

})

(2) Preventing values to be saved to the DB

For preventing a value to be set I’d say you’re on the right track.

In the UPDATE rule scripts $context variable you also have the key entry.
If I am not mistaken, you could screen for “is admin” and if not, then remove the admin-only-keys from the entry object.

<?php

if ($context->user && $context->user['group'] != 'admin'){
    unset($context->entry['key']);
}

This should prevent anybody but admins to “save” a value for that field.

1 Like