You are here

function opigno_dashboard_blocks_update in Opigno dashboard 8

Updates dashboard blocks with Drupal way machine names.

1 call to opigno_dashboard_blocks_update()
opigno_dashboard_update_8001 in ./opigno_dashboard.install
Update dashboard blocks with Drupal way machine names.

File

./opigno_dashboard.module, line 226
Contains opigno_dashboard.module.

Code

function opigno_dashboard_blocks_update() {
  $blocks_ids = [
    'views_block:opigno_group_members-block_pending',
    'views_block:comments_recent-block_1',
    'views_block:content_recent-block_1',
    'views_block:latest_active_trainings-block',
    'views_block:opigno_calendar-month_block',
    'views_block:opigno_notifications-block_unread_dashboard',
    'views_block:opigno_score_modules-dashboard_notavaluated_modules',
    'views_block:private_message-block_dashboard',
    'views_block:who_s_new-block_1',
    'views_block:who_s_online-who_s_online_block',
  ];
  $config = \Drupal::configFactory();
  $block_service = new BlockService();
  $blocks = $block_service
    ->getAllBlocks();
  foreach ($blocks_ids as $id) {
    $old_id = $block_service
      ->sanitizeIdOld($id);
    $new_id = str_replace('-', '_', $old_id);
    if ($block_old = Block::load($old_id)) {
      try {
        $block_old
          ->delete();
      } catch (Exception $e) {
        \Drupal::logger('opigno_dashboard')
          ->error($e
          ->getMessage());
      }
    }
    if (!Block::load($new_id)) {
      $admin_label = $blocks[$id]['admin_label'];
      if (is_object($admin_label)) {
        $admin_label = $admin_label
          ->render();
      }
      $settings = [
        'plugin' => $id,
        'region' => 'content',
        'id' => $new_id,
        'theme' => $config
          ->get('system.theme')
          ->get('default'),
        'label' => t('Dashboard:') . ' ' . $admin_label,
        'visibility' => [
          'request_path' => [
            'id' => 'request_path',
            'pages' => '<front>',
            'negate' => FALSE,
            'context_mapping' => [],
          ],
        ],
        'weight' => 0,
      ];
      $values = [];
      foreach ([
        'region',
        'id',
        'theme',
        'plugin',
        'weight',
        'visibility',
      ] as $key) {
        $values[$key] = $settings[$key];

        // Remove extra values that do not belong in the settings array.
        unset($settings[$key]);
      }
      foreach ($values['visibility'] as $id => $visibility) {
        $values['visibility'][$id]['id'] = $id;
      }
      $values['settings'] = $settings;
      $block_new = Block::create($values);
      try {
        $block_new
          ->save();
      } catch (Exception $e) {
        \Drupal::logger('opigno_dashboard')
          ->error($e
          ->getMessage());
      }
    }
  }
  drupal_flush_all_caches();
}