You are here

public function GridDialog::submitDialog in CKEditor Bootstrap Grid 2.0.x

Commit the changes and close the dialog window.

Parameters

array $form: The form.

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

Return value

\Drupal\Core\Ajax\AjaxResponse An ajax response.

File

src/Form/GridDialog.php, line 512

Class

GridDialog
Creates a grid dialog form for use in CKEditor.

Namespace

Drupal\ckeditor_bs_grid\Form

Code

public function submitDialog(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $settings = $form_state
    ->get('bs_grid_settings');

  // Set container classes.
  if (isset($settings['add_container'])) {
    $settings['container_class'] = $settings['container_class'] ?? '';
    if ($settings['container_type'] === 'default') {
      $settings['container_class'] = 'container ' . $settings['container_class'];
    }
    else {
      $settings['container_class'] = 'container-fluid ' . $settings['container_class'];
    }
  }
  else {
    $settings['container_class'] = '';
  }

  // Set row classes.
  if (isset($settings['row_class'])) {
    if (strpos($settings['row_class'], 'row') === FALSE) {
      $settings['row_class'] = 'row ' . $settings['row_class'];
    }
    if ($settings['no_gutter'] && strpos($settings['row_class'], 'no-gutters') === FALSE) {
      $settings['row_class'] = 'no-gutters ' . $settings['row_class'];
    }
  }
  elseif ($settings['no_gutter']) {
    $settings['row_class'] = 'row no-gutters';
  }
  else {
    $settings['row_class'] = 'row';
  }

  // Parse out the column classes.
  for ($i = 1; $i <= $settings['num_columns']; $i++) {
    $keys = [];
    $col = 'col_' . $i . '_classes';
    foreach ($settings['breakpoints'] as $prefix => $selection) {

      // Advanced.
      if ($selection['layout'] === 'none') {
        continue;
      }
      else {
        $vals = explode('_', $selection['layout']);
        $col_value = $vals[$i - 1];
        if (!empty($col_value)) {
          $suffix = $col_value === 'equal' ? '' : '-' . $col_value;
          if ($prefix === 'none') {
            $keys['col' . $suffix] = TRUE;
          }
          else {
            $keys['col-' . $prefix . $suffix] = TRUE;
          }
        }
      }
    }
    $current = implode(' ', array_keys($keys));
    if (!empty($settings[$col])) {
      $settings[$col] = $current . ' ' . $settings[$col];
    }
    else {
      $settings[$col] = $current;
    }
  }

  // Track that we're committed.
  $settings['saved'] = TRUE;
  $response
    ->addCommand(new EditorDialogSave($settings));
  $response
    ->addCommand(new CloseModalDialogCommand());
  return $response;
}