You are here

protected function GridStackForm::validateBreakpointForm in GridStack 8

Validate breakpoint form.

1 call to GridStackForm::validateBreakpointForm()
GridStackForm::validateForm in modules/gridstack_ui/src/Form/GridStackForm.php
Form validation handler.

File

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

Class

GridStackForm
Extends base form for gridstack instance configuration form.

Namespace

Drupal\gridstack_ui\Form

Code

protected function validateBreakpointForm(array &$form, FormStateInterface &$form_state) {
  $options_breakpoints = $form_state
    ->getValue([
    'options',
    'breakpoints',
  ]);
  $framework = $form_state
    ->getValue([
    'options',
    'use_framework',
  ]);
  foreach ($options_breakpoints as $key => $breakpoints) {
    foreach ($breakpoints as $k => $value) {

      // Static grids only expect 12 columns, not dynamic ones.
      if (!empty($framework)) {
        $breakpoints['column'] = 12;
        if ($k == 'column') {
          $value = 12;
        }
      }

      // Respect 0 value for future mobile first when Blazy supports it.
      if (!empty($breakpoints['column'])) {
        $form_state
          ->setValue([
          'options',
          'breakpoints',
          $key,
          $k,
        ], $value);
      }
      if (!empty($framework)) {
        $image_style = [
          'options',
          'breakpoints',
          $key,
          'image_style',
        ];
        $form_state
          ->unsetValue($image_style);
      }

      // Remove breakpoint grids if no width provided.
      if (empty($breakpoints['width'])) {
        $form_state
          ->unsetValue([
          'options',
          'breakpoints',
          $key,
        ]);
      }
    }

    // Clean out stuffs, either stored somewhere else, or no use.
    $nested = $form_state
      ->getValue([
      'options',
      'breakpoints',
      $key,
      'nested',
    ]);
    $nested = Json::decode($nested);
    $nested = empty($nested) ? '' : array_filter($nested);
    $grids = $form_state
      ->getValue([
      'options',
      'breakpoints',
      $key,
      'grids',
    ]);
    $grids = Json::decode($grids);
    $grids = empty($grids) ? '' : array_filter($grids);
    if (empty($nested) || empty($grids)) {
      $form_state
        ->unsetValue([
        'options',
        'breakpoints',
        $key,
        'nested',
      ]);
    }
    $form_state
      ->unsetValue([
      'options',
      'breakpoints',
      $key,
      'breakpoint',
    ]);
  }
}