Conditional readonly fields #7320
Replies: 3 comments 1 reply
-
You could make custom fields? |
Beta Was this translation helpful? Give feedback.
-
Wouldn't this addon work? https://statamic.com/addons/thoughtco/restrict-fields |
Beta Was this translation helpful? Give feedback.
-
@twd3 That's not the problem. I'm able to conditionally show fields based on user roles. That's pretty basic stuff. The problem is that when the fields are hidden, they are overriding the values saved by other users when fields are visible. I'm not sure, but if I had to guess, I would say this is a bug with the statamic/eloquent-driver, that simply shoves the resulting data of the blueprint handling into the What I did now was to replace the public function save($entry)
{
// Data resulting from the blueprint handling
$processedData = $entry->data();
// Get the keys from the payload
$payloadKeys = array_keys(request()->all());
// Remove keys that aren't in the payload from the processed data
$updates = $processedData->only($payloadKeys)->all();
// Curent data in the datababe
$storedData = $entry->model()->data;
// Merge new values into current data
$newData = array_replace_recursive([], $storedData, $updates);
// Update the entry instance data with the new data
$entry->data($newData);
$model = $entry->toModel();
$model->save();
$entry->model($model->fresh());
Blink::put("eloquent-entry-{$entry->id()}", $entry);
Blink::put("eloquent-entry-{$entry->uri()}", $entry);
} I hope this makes the problem clearer. I'm still not sure if this is a bug or the expected behavior, and I also don't know if this is something happening only when using a database. Either way, if anyone think it's a bug, I'm glad to open an issue or a PR. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, folks! It's been a week since I dived deep into learning Statamic and I'd like to share a situation I dealt with that a I find could maybe be better handled by the framework. Maybe there's already a better way of doing this, but I couldn't find it on the dos nor the discussions. It goes like this:
I have 2 roles:
editor
andwriter
I have a
schedule
blueprint with the following fields:title
contents
deadline
priority
An
editor
canedit
everything.An
writer
, however, canview
everything, but cannotedit
deadline
andpriority
.So, I started thinking how could I achieve this.
First try
My first try was to add two
priority
fields to the blueprint and conditionally switch between them based on the user role. Theeditor
field would have it'svisibility
set to visible while thewriter
field would be set toreadonly
. I quickly found out that you can't have 2 fields with the same handle in a blueprint.Getting the correct UI
Then, I kept the basic idea but, for the
writer
, instead of thereadonly
fields, I created acomputed_priority
and acomputed_deadline
, set theirvisibility
tocomputed
and used the same display label for them as the ones for theeditor
. This way I got visually identical forms for both roles, but preventingwriter
users from being able to changepriority
anddeadline
.The form submission problem
From there, the problem became this: when a
writer
submits the form, sincepriority
anddeadline
aren't included in thepayload
because these arecomputed
, they end up overriding the already saved entry'spriority
anddeadline
. For instance:If an
editor
saved this:And then a
writer
submitted the form, the entry's data ended up like this:So, I needed a way to prevent
writer
users from touching this properties.Is event subscribers the best choice for this?
So, I created an event subscriber to listen for the EntrySaving event. In there, I check the role of the user submitting the form, and then use the current saved data to set the missing payload values on the entry being saved. Here's a simplified version of the subscriber:
This is working perfectly as I intended. I just think it's a bit cumbersome. Is there a better/easier way to achieve this?
If there isn't, we could think of alternatives to make this kind of scenario easier to deal with.
What could be done?
The first thing that comes to my mind is to have an option on the collection to tell Statamic not to touch saved values if their not present on the request. Something that worked like array_replace_recursive, keeping everything form the source and replacing existing keys.
Again... I don't know the internals of Statamic and even if this is possible. But I think something like this could make things a lot easier in several scenarios. What do you think?
Beta Was this translation helpful? Give feedback.
All reactions