You are here

public function DashboardManageForm::submitForm in Draggable dashboard 8

Form submit.

Parameters

array $form: Form elements array.

\Drupal\Core\Form\FormStateInterface $form_state: Form state object.

Overrides DashboardFormBase::submitForm

File

src/Form/DashboardManageForm.php, line 250

Class

DashboardManageForm
Provides the draggable dashboard edit form.

Namespace

Drupal\draggable_dashboard\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Remove unnecessary values.
  $form_state
    ->cleanValues();
  $did = $form_state
    ->getValue('did', 0);

  // Save Dashboard Blocks.
  $dBlocks = [];
  $blocks = $form_state
    ->getValue('blocks', []);
  $table = $form_state
    ->getValue('dashboard_blocks_table', []);
  foreach ($blocks as $id => $block) {
    $position = 0;
    foreach ($table as $key => $new_position) {
      if ($id == $key) {
        break;
      }
      if ($block['region'] == $blocks[$key]['region']) {
        $position++;
      }
    }
    $dBlocks[] = [
      'bid' => $id,
      'cln' => $block['region'],
      'position' => (int) $position,
    ];
  }
  $this->dashboard
    ->set('blocks', json_encode($dBlocks))
    ->save();
  $this
    ->messenger()
    ->addMessage($this
    ->t('Dashboard blocks has been updated.'));
  $form_state
    ->setRedirect('draggable_dashboard.manage_dashboard', [
    'did' => $did,
  ]);
}