I have a key field inside my collection. This field should only be editable by the admin. Any other user group should be able to view the value, but not edit it. Is this possible?
What I tried:
Inside the Permission settings of the collection, I enabled UPDATE and added this php code:
<?php
if ($context->user && $context->user['group'] != 'admin')
$context->options['fields']['key'] = false;
But it doesn’t seem to be working. A non-admin user can still edit and update the key field.
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.
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.
The idea is good. Unfortunately, in my case I cannot access any relevant input fields at all. Even using document.querySelectorAll('input') in the function only returns 3 input fields from another level (i.e. the search field on top), but none of the form.
Maybe you have another idea? Thanks again for the help!
The clean solution would be to “wait” until the element is present (with a setInterval and an additional setTimeout for a timeout) - but that might be just overkill and the little delay hack might do “just fine”.