You are here

public function DashboardFormBase::submitForm in Draggable dashboard 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

1 method overrides DashboardFormBase::submitForm()
DashboardManageForm::submitForm in src/Form/DashboardManageForm.php
Form submit.

File

src/Form/DashboardFormBase.php, line 128

Class

DashboardFormBase
Provides a base class for draggable dashboard add/edit forms.

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);
  $args = [
    'id' => empty($did) ? time() : $did,
    'title' => $form_state
      ->getValue('title', ''),
    'description' => $form_state
      ->getValue('description', ''),
    'columns' => $form_state
      ->getValue('columns', 2),
  ];
  foreach ($args as $key => $value) {
    $this->dashboard
      ->set($key, $value);
  }
  $this->dashboard
    ->save();

  // Delete block from column if number of columns has been changed.
  if (!empty($did) && $this->dashboard
    ->get('columns') > $args['columns']) {
    $all_blocks = json_decode($this->dashboard
      ->get('blocks'), TRUE);
    for ($i = $args['columns'] + 1; $i <= self::MAX_COLUMNS_COUNT; $i++) {
      foreach ($all_blocks as $key => $relation) {
        if ($relation['cln'] == $i) {
          $block = Block::load($relation['bid']);
          if ($block) {
            $block
              ->delete();
          }
          unset($all_blocks[$key]);
        }
      }
    }
    $this->dashboard
      ->set('blocks', json_encode($all_blocks))
      ->save();
  }

  // Invalidate block list cache.
  $this->blockManager
    ->clearCachedDefinitions();
  $this
    ->messenger()
    ->addMessage($this
    ->t('Dashboard has been saved.'));

  // Redirect just created dashboard to manage blocks page.
  if (empty($did)) {
    $form_state
      ->setRedirect('draggable_dashboard.manage_dashboard', [
      'did' => $args['id'],
    ]);
  }
  else {
    $form_state
      ->setRedirect('draggable_dashboard.overview');
  }
}