Hello friends,
I have derived a hotel system from the Blog module of my problem.
Then, only the person I authorized will be able to see the hotel I added to the unauthorized user.
I reached the file vendor/silverstripe/framework/src/Forms/GridField/GridField.php where I went for this. Here, I need to add an edit-disabled class to the table tr of the hotels that cannot be edited in the menu tree in the admin.
Thank you from now.
This is the menu tree view of the limited privileged user:
If I understood you correctly (and that is a big if), you want to add a custom class to the GridField rows depending on some logic.
This can be done in different ways. The simplest one I am aware of is to apply an extension to GridField implementing the updateNewRowClasses extension point, e.g.:
// UNTESTED!
class GridFieldExtension extends Extension
{
public function updateNewRowClasses(&$classes, $total, $index, $record)
{
if (! $record->canEdit()) {
$classes[] = 'edit-disabled';
}
}
}
Sorry but I do not understand: did the above code solve the issue?
canEdit() is a model level API that passes through the standard Silverstripe privilege checks. If you implemented your own privilege management (i.e., whatever “I did not authorize” means in your context) just change that call.
Unfortunately it didn’t fix the problem. What I want to do is when the user is in the menu tree view, if the user does not have the authority to see the page, a class must be assigned there.
Did you mean how to delete the class instead of adding it? $classes is passed by reference, so you can modify it in the way you like. If a class you want to remove is already present, just remove it, e.g.:
// UNTESTED!
$key = array_search('class-to-remove', $classes);
// If you are sure the class already exists, you can remove the check
$key !== false and unset($classes[$key]);
I hope you meant to hide them… deleting database rows when viewing a GridField seems absurd to me.
Just adding the d-none class should hide the row from the front end but the row is still present, so other GridField components (e.g. pagination) will act weirdly.
I’d also like to point out this question is changing at every answer, so please try to focus on what you really need. I have the feeling you are trying to reimplement your own Model-level permission when a simple canView function in the proper DataObject would have solved everything.
If you want to hide rows from a gridfield because users should not have the requisite permissions to view them then you will want to edit the canView() permissions for the relevant DataObject subclass, either directly or via an extension.