You are here

function opigno_dashboard_entity_delete in Opigno dashboard 8

Remove a block from dashboard config after the block deletion.

Implements hook_entity_delete().

File

./opigno_dashboard.module, line 317
Contains opigno_dashboard.module.

Code

function opigno_dashboard_entity_delete(EntityInterface $entity) {
  if ($entity
    ->getEntityTypeId() == 'block') {

    // The block was deleted, need to remove it from dashboard config.
    $config = \Drupal::configFactory()
      ->getEditable('opigno_dashboard.settings');
    if ($blocks = $config
      ->get('blocks')) {
      $key = $entity
        ->getPluginId();
      if (key_exists($key, $blocks)) {
        unset($blocks[$key]);
        $config
          ->set('blocks', $blocks);
        try {
          $config
            ->save();
        } catch (\Exception $e) {
          \Drupal::logger('opigno_dashboard')
            ->error($e
            ->getMessage());
          \Drupal::messenger()
            ->addMessage($e
            ->getMessage(), 'error');
        }
      }
    }
  }
}