<?php

/**
 * @file
 * Install,update & uninstall functions for the block_visibility_groups module.
 */

/**
 * Implements hook_uninstall().
 */
function block_visibility_groups_uninstall(): void {
  // Remove the 'condition_group' condition from all blocks.
  /** @var \Drupal\Core\Entity\EntityStorageInterface $block_storage */
  $block_storage = \Drupal::service('entity_type.manager')->getStorage('block');
  /** @var \Drupal\block\Entity\Block[] $blocks */
  $blocks = $block_storage->loadMultiple();
  foreach ($blocks as $block) {
    $conditions = $block->getVisibilityConditions();
    if ($conditions->has('condition_group')) {
      $conditions->removeInstanceId('condition_group');
      $block->save();
    }
  }
}
