You are here

protected function GridStackFormBase::validateBreakpointForm in GridStack 8.2

Validate breakpoint form.

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

File

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

Class

GridStackFormBase
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 (isset($breakpoints['column'])) {
        $form_state
          ->setValue([
          'options',
          'breakpoints',
          $key,
          $k,
        ], $value);
      }
    }

    // Remove breakpoint grids if no width provided.
    // Makes it possible to have 3 out of 5 breakpoints like BS3/Foundation.
    if (empty($breakpoints['width'])) {
      $form_state
        ->unsetValue([
        'options',
        'breakpoints',
        $key,
      ]);
    }

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

    // Simplify storage to just array without keys like at frontend.
    // @todo put this into the loop above.
    if ($grids) {
      $exclude_region = $key != $this->entity
        ->getLastBreakpointKey();
      $main_grids = $this->entity
        ->getJsonSummaryBreakpoints($key, $grids_all, $exclude_region);
      $form_state
        ->setValue([
        'options',
        'breakpoints',
        $key,
        'grids',
      ], $main_grids);
      if ($nested) {
        $nested_grids = $this->entity
          ->getJsonSummaryNestedBreakpoints($key, $nested_all);
        $form_state
          ->setValue([
          'options',
          'breakpoints',
          $key,
          'nested',
        ], $nested_grids);
      }
    }

    // Remove useless breakpoint key.
    $form_state
      ->unsetValue([
      'options',
      'breakpoints',
      $key,
      'breakpoint',
    ]);
  }
}