public static function DraggableDashboardController::assignBlock in Draggable dashboard 8
Assign block to a region.
Parameters
array $form: Form array.
\Drupal\Core\Form\FormStateInterface $form_state: Form state object.
File
- src/
Controller/ DraggableDashboardController.php, line 106
Class
- DraggableDashboardController
- Controller routines for draggable dashboards.
Namespace
Drupal\draggable_dashboard\ControllerCode
public static function assignBlock(array &$form, FormStateInterface $form_state) {
// Save block entity.
$settings = $form_state
->getValue('settings');
$region = $form_state
->getValue('region');
$dashboard_id = $form_state
->getValue('dashboard_id');
$block_id = $form_state
->getValue('id');
$obj = $form_state
->getBuildInfo()['callback_object'];
/** @var \Drupal\block\Entity\Block $block */
$block = $obj
->getEntity();
$block
->set('id', $block_id);
$block
->set('region', DashboardEntityInterface::BASE_REGION_NAME);
$block
->set('settings', $settings);
$block
->enable();
$block
->save();
/** @var \Drupal\draggable_dashboard\Entity\DashboardEntity $dashboard */
$dashboard = DashboardEntity::load($dashboard_id);
$blocks = json_decode($dashboard
->get('blocks'), TRUE);
$relationFounded = FALSE;
if (!empty($blocks)) {
foreach ($blocks as $key => $relation) {
if ($relation['bid'] == $block_id) {
$blocks[$key]['cln'] = (int) $region;
$relationFounded = TRUE;
break;
}
}
}
if (!$relationFounded) {
$blocks[] = [
'bid' => $block_id,
'cln' => (int) $region,
'position' => 0,
];
}
// Save relation.
$dashboard
->set('blocks', json_encode($blocks))
->save();
// Redirect to manage blocks screen.
$form_state
->setRedirect('draggable_dashboard.manage_dashboard', [
'did' => $dashboard_id,
]);
}