You are here

function blockgroup_update_8100 in Block Group 8

Updated blockgroup plugin IDs in block placements.

File

./blockgroup.install, line 11
Blockgroup install file.

Code

function blockgroup_update_8100() {

  /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
  $entity_repository = \Drupal::service('entity.repository');
  $config_factory = \Drupal::configFactory();
  $update_count = 0;
  foreach ($config_factory
    ->listAll('block.block.') as $config_name) {
    $config = $config_factory
      ->getEditable($config_name);
    $needs_save = FALSE;
    foreach ([
      'plugin',
      'settings.id',
    ] as $key) {
      $value = $config
        ->get($key);
      $parts = explode(':', $value, 2);
      if (count($parts) === 2 && $parts[0] === 'block_group') {
        $block_group = $entity_repository
          ->loadEntityByUuid('block_group_content', $parts[1]);
        $block_group_id = $block_group
          ->id();
        $new_value = "block_group:{$block_group_id}";
        $config
          ->set($key, $new_value);
        $update_count += 1;
        $needs_save = TRUE;
      }
    }

    // Mark as trusted to not trigger further processing.
    if ($needs_save) {
      $config
        ->save(TRUE);
    }
  }
  return "Updated {$update_count} blockgroup plugin IDs (2 per block) in block placements.";
}