You are here

public function DraggableDashboardController::updateBlockPositions in Draggable dashboard 8

Returns a set of nodes' last read timestamps.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request of the page.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

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

File

src/Controller/DraggableDashboardController.php, line 189

Class

DraggableDashboardController
Controller routines for draggable dashboards.

Namespace

Drupal\draggable_dashboard\Controller

Code

public function updateBlockPositions(Request $request) {
  if ($this
    ->currentUser()
    ->isAnonymous()) {
    throw new AccessDeniedHttpException();
  }
  $did = $request->request
    ->get('did');
  $blocks = $request->request
    ->get('blocks');

  /** @var \Drupal\draggable_dashboard\Entity\DashboardEntity $dashboard */
  $dashboard = $this->entityTypeManager
    ->getStorage('dashboard_entity')
    ->load($did);
  $dBlocks = json_decode($dashboard
    ->get('blocks'), TRUE);
  if (!isset($blocks) && empty($dashboard)) {
    throw new NotFoundHttpException();
  }

  // Update dashboard blocks positions.
  foreach ($dBlocks as $key => $dBlock) {
    foreach ($blocks as $bid => $relation) {
      if ($dBlock['bid'] == $bid) {
        $dBlocks[$key]['cln'] = $relation['region'];
        $dBlocks[$key]['position'] = $relation['order'];
      }
    }
  }
  $dashboard
    ->set('blocks', json_encode($dBlocks))
    ->save();
  return new JsonResponse([
    'success' => TRUE,
  ]);
}