DashboardDeleteBlockForm.php in Draggable dashboard 8.2
File
src/Form/DashboardDeleteBlockForm.php
View source
<?php
namespace Drupal\draggable_dashboard\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\draggable_dashboard\Entity\DashboardEntityInterface;
class DashboardDeleteBlockForm extends ConfirmFormBase {
protected $dashboard;
protected $block = [];
public function getQuestion() {
return $this
->t('Are you sure you want delete block `%title`?', [
'%title' => $this->block['settings']['label'],
]);
}
public function getCancelUrl() {
return Url::fromRoute('entity.dashboard_entity.edit_form', [
'dashboard_entity' => $this->dashboard
->id(),
]);
}
public function getFormId() {
return 'draggable_dashboard_block_delete';
}
protected function init(FormStateInterface $form_state, DashboardEntityInterface $dashboard_entity, $block_id) {
$form_state
->set('form_initialized', TRUE);
$this->dashboard = $dashboard_entity;
$blocks = $dashboard_entity
->get('blocks');
if (!empty($blocks[$block_id])) {
$this->block = $blocks[$block_id];
}
}
public function buildForm(array $form, FormStateInterface $form_state, DashboardEntityInterface $dashboard_entity = NULL, $block_id = '') {
if (!$form_state
->has('form_initialized')) {
$this
->init($form_state, $dashboard_entity, $block_id);
}
$form['block_id'] = [
'#type' => 'value',
'#value' => $block_id,
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$block_id = $form_state
->getValue('block_id');
$blocks = $this->dashboard
->get('blocks');
if (isset($blocks[$block_id])) {
unset($blocks[$block_id]);
}
$this->dashboard
->set('blocks', $blocks)
->save();
$form_state
->setRedirect('entity.dashboard_entity.edit_form', [
'dashboard_entity' => $this->dashboard
->id(),
]);
}
}