You are here

public function GridStackForm::validateForm in GridStack 8

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

File

modules/gridstack_ui/src/Form/GridStackForm.php, line 686

Class

GridStackForm
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',
  ]);

  // Remove old options.grids for options.breakpoints.lg.grids, etc.
  // @todo: Remove BC.
  if ($form_state
    ->hasValue([
    'options',
    'grids',
  ])) {
    $form_state
      ->unsetValue([
      'options',
      'grids',
    ]);
  }
  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) {
      foreach ($breakpoints as $k => $value) {
        if (empty($breakpoints['width'])) {
          continue;
        }

        // Respect 0 value for future mobile first when Blazy supports it.
        if ($k != 'image_style' && !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));

  // Remove unused settings.
  $form_state
    ->unsetValue([
    'template',
    'image_style',
  ]);

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

      // Update data URI into file URI.
      if (!empty($paths['uri'])) {

        // @todo: Remove this when corrected.
        if (strpos($paths['uri'], 'data:,') !== FALSE) {
          $paths['uri'] = '';
        }
        $form_state
          ->setValue([
          'options',
          'icon',
        ], $paths['uri']);
      }
    }
  }
}