Conditional Fields (Read only mode) #12157
-
It would be nice if we could set how we want the field to be displayed, sometimes we want the users to be able to see the content, but not be able to change the value of the field. The idea would be to be able to set the field as editable or disabled Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Each field type is responsible for displaying their own UI, so getting all field types (including third party plugins) on board with this would be tough. Although you could pull this off using a “Template” UI element, which can output the raw value of a field, and can be set to only be displayed for users that wouldn't have permission to see the actual field. |
Beta Was this translation helpful? Give feedback.
-
I have a little module I whipped up for this purpose: https://github.com/TopShelfCraft/write-protected-fields-core The gist is: In a config file, I can specify conditions under which certain fields are displayed using their "static" UI. (Craft fieldtypes can specify both editable and "static" UI variants. This is primarily used in the UI for editing Commerce orders: When you first pull up an order element in the CP, the fields display a read-only preview of their content. When you click the Edit button, the fields switch to their editable UI so you can make changes. I just extended that concept so that I can ask fields to always use the "static" version of their UI, under certain conditions.) The catch is, this only affects the UI rendering of each field; Under the hood, the field values aren't actually write-protected. So, if a user manually changes the UI — e.g. using the dev tools to set a disabled text input as enabled — then the field value will get updated (assuming the user has permission to update the element in the first place). I've used this as a UI nicety — to keep non-admins from editing fields that hold "system" values. n.b. Depending on your use case, it might be more straightforward to display the values you're interested in via a Template UI element, as Brandon described. It's nice to avoid third-party customization if you can help it. But, if you think my module could be helpful as a partial solution for your needs, I'm happy to answer questions. (I haven't updated it for Craft 4 yet, but one easily could...) |
Beta Was this translation helpful? Give feedback.
Each field type is responsible for displaying their own UI, so getting all field types (including third party plugins) on board with this would be tough.
Although you could pull this off using a “Template” UI element, which can output the raw value of a field, and can be set to only be displayed for users that wouldn't have permission to see the actual field.