1

I know that to get all available permissions you can do

var_dump(array_keys(\Drupal::service('user.permissions')->getPermissions()));

which give you a list of all permissions' name.

I also know that when you create a NodeType, it refresh this list.

So what I want to do here, is to fill a table of my database with a list of all available permissions, that update when permission is add or removed.

When I create a NodeType :

function inside_entity_insert(\Drupal\Core\Entity\EntityInterface $entity) : void
{
    if (get_class($entity) === 'Drupal\node\Entity\NodeType') {
        $permissions = array_keys(\Drupal::service('user.permissions')->getPermissions());

        foreach ($permissions as $permission) {
        // somestuff -> updating database permission table
        }
     }
}

So that means when NodeType is created the Drupal permissions list is updated and I must update my database too.

What I want to do here is the same thing but when a NodeType is deleted.

I tried :

  1. hook_entity_delete
  2. hook_entity_predelete

But in both hook the user.permission list didn't change, I still get the same permissions, and what I though is that the permissions of my actual nodetype dissapeared, because of the deletion.

I do not know where to look..

Is getting the name of the nodeType and delete matchings rules' name of the database, a good alternative ?

I also wanted where can I initialize these database, because actually it's created & updated only when someone create a NodeType. But that's not urgent at the moment.

Thanks to all

1 Answer 1

0

You are using the wrong hooks. I am not sure what your objective is, but you want hook_ENTITY_BUNDLE_delete I believe:

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_bundle_delete/8.6.x

function mymodule_entity_bundle_delete($entity_type_id, $entity) : void
{
    if ($entity_type_id == 'node' && $bundle == 'node type to check') {
        $permissions = array_keys(\Drupal::service('user.permissions')->getPermissions());

        foreach ($permissions as $permission) {
        // somestuff -> updating database permission table
        }
     }
}

I don't know if you care to check $bundle or not, but if you delete any node type, this code should fire.

1
  • Thank you, that's exactly what I'm looking for. And no, I do not need to check the $bundle, just needed the hook with updated permission that is triggered on nodetype deletion ! So thanks again Commented Jun 1, 2018 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.