You are here

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

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/GridDialog.php, line 61

Class

GridDialog
Creates a grid dialog form for use in CKEditor.

Namespace

Drupal\ckeditor_bs_grid\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Editor $editor = NULL) {
  $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
  $form['#attached']['library'][] = 'ckeditor_bs_grid/dialog';

  // Opening the dialog passes the value as editor object, but is only once.
  $values = $form_state
    ->getValues();
  $input = $form_state
    ->getUserInput();

  // Initialize entity element with form attributes, if present.
  $settings = empty($values['bs_grid_settings']) ? [] : Json::decode($values['bs_grid_settings']);
  $settings += empty($input['bs_grid_settings']) ? [] : Json::decode($input['bs_grid_settings']);

  // The default values are set directly from \Drupal::request()->request,
  // provided by the editor plugin opening the dialog.
  if (!$form_state
    ->get('bs_grid_settings')) {
    $form_state
      ->set('bs_grid_settings', isset($input['editor_object']) ? $input['editor_object'] : []);
  }
  $settings += $form_state
    ->get('bs_grid_settings');

  // Save the editor settings.
  if ($editor instanceof EditorInterface) {
    $editor_settings = $editor
      ->getSettings();
    if (isset($editor_settings['plugins']['bs_grid'])) {
      $settings['editor_settings'] = $editor_settings['plugins']['bs_grid'];
    }
  }
  $form_state
    ->set('bs_grid_settings', $settings);
  if (!$form_state
    ->get('step')) {
    $form_state
      ->set('step', 'select');
  }
  $form['#tree'] = TRUE;
  $form['#prefix'] = '<div id="bs_grid-dialog-form">';
  $form['#suffix'] = '</div>';
  $form['#attributes']['class'][] = 'bs_grid-dialog-step--' . $form_state
    ->get('step');
  if ($form_state
    ->get('step') == 'select') {
    $form = $this
      ->buildSelectStep($form, $form_state);
  }
  elseif ($form_state
    ->get('step') == 'layout') {
    $form = $this
      ->buildLayoutStep($form, $form_state);
  }
  elseif ($form_state
    ->get('step') == 'advanced') {
    $form = $this
      ->buildAdvancedStep($form, $form_state);
  }
  return $form;
}