You are here

public function DraggableDashboardController::deleteBlock in Draggable dashboard 8

Delete block.

Parameters

string $did: Dashboard Id.

string $bid: Block Id.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse Redirect response back to dashboard.

1 string reference to 'DraggableDashboardController::deleteBlock'
draggable_dashboard.routing.yml in ./draggable_dashboard.routing.yml
draggable_dashboard.routing.yml

File

src/Controller/DraggableDashboardController.php, line 157

Class

DraggableDashboardController
Controller routines for draggable dashboards.

Namespace

Drupal\draggable_dashboard\Controller

Code

public function deleteBlock($did, $bid) {

  /** @var \Drupal\draggable_dashboard\Entity\DashboardEntity $dashboard */
  $dashboard = $this->entityTypeManager
    ->getStorage('dashboard_entity')
    ->load($did);
  $blocks = json_decode($dashboard
    ->get('blocks'), TRUE);
  if (!empty($blocks)) {
    foreach ($blocks as $key => $relation) {
      if ($relation['bid'] == $bid) {
        $block = Block::load($relation['bid']);
        $block
          ->delete();
        unset($blocks[$key]);
      }
    }
  }

  // Delete block relation.
  $dashboard
    ->set('blocks', json_encode($blocks))
    ->save();
  $manageURL = Url::fromRoute('draggable_dashboard.manage_dashboard', [
    'did' => $did,
  ]);
  $response = new RedirectResponse($manageURL
    ->toString());
  return $response
    ->send();
}