You are here

function block_user_role_delete in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/block/block.module \block_user_role_delete()
  2. 7 modules/block/block.module \block_user_role_delete()

Implements hook_ENTITY_TYPE_delete() for user_role entities.

Removes deleted role from blocks that use it.

File

core/modules/block/block.module, line 275
Controls the visual building blocks a page is constructed with.

Code

function block_user_role_delete($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();
    }
  }
}