You are here

public function GridStackFormBase::validateForm in GridStack 8.2

Form validation 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 FormBase::validateForm

2 calls to GridStackFormBase::validateForm()
GridStackVariantForm::duplicateVariant in modules/gridstack_ui/src/Form/GridStackVariantForm.php
Callback for duplicating a layout variant.
GridStackVariantForm::saveVariant in modules/gridstack_ui/src/Form/GridStackVariantForm.php
Callback for saving a layout variant draft.

File

modules/gridstack_ui/src/Form/GridStackFormBase.php, line 789

Class

GridStackFormBase
Extends base form for gridstack instance configuration form.

Namespace

Drupal\gridstack_ui\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  // Grids contain the current grid node and probably nested grids.
  $framework = $form_state
    ->getValue([
    'options',
    'use_framework',
  ]);

  // @todo remove after a hook_update.
  $form_state
    ->unsetValue([
    'options',
    'type',
  ]);
  if (!$form_state
    ->hasValue([
    'json',
    'grids',
    'nested',
  ])) {
    $form_state
      ->unsetValue([
      'json',
      'grids',
      'nested',
    ]);
  }

  // Columns.
  $settings = $form_state
    ->getValue([
    'options',
    'settings',
  ]);
  $options_breakpoints = $form_state
    ->getValue([
    'options',
    'breakpoints',
  ]);

  // Validate breakpoint form.
  if (!empty($options_breakpoints)) {
    $this
      ->validateBreakpointForm($form, $form_state);
  }

  // Remove JS settings for static grid layout like Bootstrap/ Foundation.
  if (!empty($framework)) {
    $settings = [];
    $form_state
      ->setValue([
      'options',
      'settings',
    ], []);
  }

  // Map settings into JSON.
  $form_state
    ->setValue([
    'json',
    'settings',
  ], empty($settings) ? '' : $this
    ->jsonify($settings));

  // JS only breakpoints.
  // Only reasonable for GridStack, not Bootstrap, or other static grid.
  // JSON breakpoints to reduce frontend logic for responsive JS.
  $json_breakpoints = [];
  if (!empty($options_breakpoints)) {
    foreach ($options_breakpoints as $breakpoints) {

      // Makes it possible to have 3 out of 5 breakpoints like BS3/Foundation.
      if (empty($breakpoints['width'])) {
        continue;
      }
      if (!empty($breakpoints['column'])) {
        $json_breakpoints[$breakpoints['width']] = empty($framework) ? (int) $breakpoints['column'] : 12;
      }
    }
  }

  // Append the desktop version as well to reduce JS logic.
  $form_state
    ->setValue([
    'json',
    'breakpoints',
  ], empty($json_breakpoints) ? '' : Json::encode($json_breakpoints));

  // Build icon.
  if ($icon = $form_state
    ->getValue([
    'options',
    'icon',
  ])) {
    $id = $form_state
      ->getValue('name');
    if (strpos($icon, 'data:image') !== FALSE) {
      $destination = 'public://gridstack';
      $paths['id'] = $id;
      $paths['target'] = $destination . '/';
      $this->fileSystem
        ->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY);
      $this
        ->saveImage($icon, $paths);

      // Update data URI into file URI.
      if (!empty($paths['uri'])) {
        if (strpos($paths['uri'], 'data:,') !== FALSE) {
          $paths['uri'] = '';
        }
        $form_state
          ->setValue([
          'options',
          'icon',
        ], $paths['uri']);
      }
    }
  }
}