You are here

function charts_update_8302 in Charts 8.4

Same name and namespace in other branches
  1. 5.0.x charts.install \charts_update_8302()

Update existing views to use the new chart settings.

File

./charts.install, line 36
Install and update functions for the Charts module.

Code

function charts_update_8302() {
  $view_storage = \Drupal::entityTypeManager()
    ->getStorage('view');
  $view_ids = $view_storage
    ->getQuery()
    ->condition('display.*.display_options.style.type', 'chart', '=')
    ->execute();
  if ($view_ids) {
    foreach ($view_ids as $view_id) {

      /** @var \Drupal\views\ViewEntityInterface $view */
      $view = $view_storage
        ->load($view_id);
      if ($view) {
        $changed = FALSE;
        $displays = $view
          ->get('display');
        foreach ($displays as $id => &$display) {
          $style =& $display['display_options']['style'];
          if ($style['type'] === 'chart' && isset($style['options']['field_colors']) && isset($style['options']['fields']['table'])) {
            $changed = TRUE;
            unset($style['options']['fields']);
            $options =& $style['options'];
            $options = ChartsDefaultSettings::transformLegacySettingsToNew($options);
            $chart_settings_elements = [
              'library',
              'type',
              'fields',
              'display',
              'xaxis',
              'yaxis',
            ];
            foreach ($options as $option_key => $option) {
              if (in_array($option_key, $chart_settings_elements)) {
                $options['chart_settings'][$option_key] = $option;
                unset($options[$option_key]);
              }
            }
          }
        }
        if ($changed) {
          $view
            ->set('display', $displays);
          $view
            ->save();
        }
      }
    }
  }
}