This thread is very useful, so I thought it'd be worth coming back here to update for Drupal 10. Based on the addition made here:
https://www.drupal.org/node/3002289 β
which introduces hasRole()
// If current user is an administrator, do something
if (\Drupal::currentUser()->hasRole('administrator')) {
// Do something
}
For my uses, I needed to hide a button for all users who were NOT administrators. This is what worked for me:
if (!\Drupal::currentUser()->hasRole('administrator')) {
$form['actions']['revert']['#access'] = FALSE;
}
I also scoured the Internet for a solution to this, because I ran into the exact same issue recently. Your solution was very helpful, especially when you noted to add "Title" referencing the "Relationship." In my case I had a "Parent Name" with a Field Type set to an entity reference of type "Group."
In my specific View, the steps I followed were:
1. Added a "Relationship" of "field_name_parent: Group"
2. Added "Title" to the filter criteria with the "Relationship" set to "field_name_parent: Group", and I was able to set the "Operator" to "Contains"
This allowed me to filter results for "Parent Name" correctly. Thanks again!
Hello. When would this fix go into an office stable release? We are running into the same issue on our project. Thank you.