Adding a class to the Grid Field menu tree in admin

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:

This is the sitetree menu image and its discarded class:

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';
        }
    }
}
2 Likes

Hi @ntd ,
If I did not authorize the user, this class should be the class that will be placed.

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.

Hi @ntd,

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.

Hi @ntd,

Thans Solved.

Hi @ntd ,
How can I delete the class instead of throwing it?

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]);

@ntd ,

I want to delete unauthorized rows instead of assigning classes to them.

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.

Sory @ntd ,

I couldn’t explain. I wanted to delete the $index listed there instead of throwing a class.

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.

Take a look at the model-level permissions docs for more about that.

This is exactly what I suggested in a previous comment. Anyway, seeing the kind of feedback received, it seems we are interacting with a chatbot.

Hi @ntd ,

In fact, I think that if there is a user control and authority, we should list only the one with that authority.