function BlockHooks::userRoleDelete

Implements hook_ENTITY_TYPE_delete() for user_role entities.

Removes deleted role from blocks that use it.

File

core/modules/block/src/Hook/BlockHooks.php, line 139

Class

BlockHooks
Hook implementations for block.

Namespace

Drupal\block\Hook

Code

public function userRoleDelete($role) {
    foreach (Block::loadMultiple() as $block) {
        
        /** @var \Drupal\block\BlockInterface $block */
        $visibility = $block->getVisibility();
        if (isset($visibility['user_role']['roles'][$role->id()])) {
            unset($visibility['user_role']['roles'][$role->id()]);
            $block->setVisibilityConfig('user_role', $visibility['user_role']);
            $block->save();
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.