You are here

public function Slick::removeDefaultValues in Slick Carousel 8

Same name and namespace in other branches
  1. 8.2 src/Entity/Slick.php \Drupal\slick\Entity\Slick::removeDefaultValues()
  2. 7.3 src/Entity/Slick.php \Drupal\slick\Entity\Slick::removeDefaultValues()

Strip out options containing default values so to have real clean JSON.

Return value

array The cleaned out settings.

File

src/Entity/Slick.php, line 236

Class

Slick
Defines the Slick configuration entity.

Namespace

Drupal\slick\Entity

Code

public function removeDefaultValues(array $js) {
  $config = [];
  $defaults = self::defaultSettings();

  // Remove wasted dependent options if disabled, empty or not.
  $this
    ->removeWastedDependentOptions($js);
  $config = array_diff_assoc($js, $defaults);

  // Remove empty lazyLoad, or left to default ondemand, to avoid JS error.
  if (empty($config['lazyLoad'])) {
    unset($config['lazyLoad']);
  }

  // Do not pass arrows HTML to JSON object as some are enforced.
  $excludes = [
    'downArrow',
    'downArrowTarget',
    'downArrowOffset',
    'prevArrow',
    'nextArrow',
  ];
  foreach ($excludes as $key) {
    unset($config[$key]);
  }

  // Clean up responsive options if similar to defaults.
  if ($responsives = $this
    ->getResponsiveOptions()) {
    $cleaned = [];
    foreach ($responsives as $key => $responsive) {
      $cleaned[$key]['breakpoint'] = $responsives[$key]['breakpoint'];

      // Destroy responsive slick if so configured.
      if (!empty($responsives[$key]['unslick'])) {
        $cleaned[$key]['settings'] = 'unslick';
        unset($responsives[$key]['unslick']);
      }
      else {

        // Remove wasted dependent options if disabled, empty or not.
        $this
          ->removeWastedDependentOptions($responsives[$key]['settings']);
        $cleaned[$key]['settings'] = array_diff_assoc($responsives[$key]['settings'], $defaults);
      }
    }
    $config['responsive'] = $cleaned;
  }
  return $config;
}