You are here

function charts_blocks_update_8301 in Charts 8.4

Same name and namespace in other branches
  1. 5.0.x modules/charts_blocks/charts_blocks.install \charts_blocks_update_8301()

Move charts block settings in their own array tree under charts.

File

modules/charts_blocks/charts_blocks.install, line 13
Install and update functions for the Charts module.

Code

function charts_blocks_update_8301() {
  $block_storage = \Drupal::entityTypeManager()
    ->getStorage('block');

  // Getting all the charts blocks.
  $charts_blocks_ids = $block_storage
    ->getQuery()
    ->condition('plugin', 'charts_block')
    ->execute();
  if ($charts_blocks_ids) {
    $old_config_keys = ChartsDefaultSettings::getLegacySettingsMappingKeys();
    foreach ($charts_blocks_ids as $charts_blocks_id) {

      /** @var \Drupal\block\BlockInterface $charts_block */
      $charts_block = $block_storage
        ->load($charts_blocks_id);
      if ($charts_block) {
        $block_settings = $charts_block
          ->get('settings');

        // Adding a charts tree for charts block configurations.
        $block_settings['chart'] = ChartsDefaultSettings::transformLegacySettingsToNew($block_settings, $old_config_keys);

        // Updating the block setting and we save.
        $charts_block
          ->set('settings', $block_settings);
        $charts_block
          ->save();
      }
    }
  }
}