3 namespace Drupal\slick\Entity;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Config\Entity\ConfigEntityBase;
10 * Defines the Slick configuration entity.
14 * label = @Translation("Slick optionset"),
15 * list_path = "admin/config/media/slick",
16 * config_prefix = "optionset",
20 * "status" = "status",
21 * "weight" = "weight",
37 class Slick extends ConfigEntityBase implements SlickInterface {
40 * The legacy CTools ID for the configurable optionset.
47 * The human-readable name for the optionset.
54 * The weight to re-arrange the order of slick optionsets.
58 protected $weight = 0;
61 * The optionset group for easy selections.
65 protected $group = '';
68 * The skin name for the optionset.
75 * The number of breakpoints for the optionset.
79 protected $breakpoints = 0;
82 * The flag indicating to optimize the stored options by removing defaults.
86 protected $optimized = FALSE;
89 * The plugin instance options.
93 protected $options = [];
100 private static $slickId;
105 public function __construct(array $values, $entity_type = 'slick') {
106 parent::__construct($values, $entity_type);
110 * Overrides Drupal\Core\Entity\Entity::id().
112 public function id() {
119 public function getSkin() {
126 public function getBreakpoints() {
127 return $this->breakpoints;
133 public function getGroup() {
140 public function optimized() {
141 return $this->optimized;
147 public function getOptions($group = NULL, $property = NULL) {
149 if (is_array($group)) {
150 return NestedArray::getValue($this->options, (array) $group);
152 elseif (isset($property) && isset($this->options[$group])) {
153 return isset($this->options[$group][$property]) ? $this->options[$group][$property] : NULL;
155 return $this->options[$group];
158 return $this->options;
164 public function getSettings() {
165 // With the Optimized options, all defaults are cleaned out, merge em.
166 return isset($this->options['settings']) ? array_merge(self::defaultSettings(), $this->options['settings']) : self::defaultSettings();
172 public function setSettings(array $settings = []) {
173 $this->options['settings'] = $settings;
180 public function getSetting($name) {
181 return isset($this->getSettings()[$name]) ? $this->getSettings()[$name] : NULL;
187 public function setSetting($name, $value) {
188 $this->options['settings'][$name] = $value;
195 public static function defaultSettings($group = 'settings') {
196 return self::load('default')->options[$group];
200 * Returns the Slick responsive settings.
203 * The responsive options.
205 public function getResponsiveOptions() {
206 if (empty($this->breakpoints)) {
210 if (isset($this->options['responsives']['responsive'])) {
211 $responsives = $this->options['responsives'];
212 if ($responsives['responsive']) {
213 foreach ($responsives['responsive'] as $delta => $responsive) {
214 if (empty($responsives['responsive'][$delta]['breakpoint'])) {
215 unset($responsives['responsive'][$delta]);
217 if (isset($responsives['responsive'][$delta])) {
218 $options[$delta] = $responsive;
227 * Sets the Slick responsive settings.
230 * The class instance that this method is called on.
232 public function setResponsiveSettings($values, $delta = 0, $key = 'settings') {
233 $this->options['responsives']['responsive'][$delta][$key] = $values;
238 * Strip out options containing default values so to have real clean JSON.
241 * The cleaned out settings.
243 public function removeDefaultValues(array $js) {
245 $defaults = self::defaultSettings();
247 // Remove wasted dependent options if disabled, empty or not.
248 $this->removeWastedDependentOptions($js);
249 $config = array_diff_assoc($js, $defaults);
251 // Remove empty lazyLoad, or left to default ondemand, to avoid JS error.
252 if (empty($config['lazyLoad'])) {
253 unset($config['lazyLoad']);
256 // Do not pass arrows HTML to JSON object as some are enforced.
264 foreach ($excludes as $key) {
265 unset($config[$key]);
268 // Clean up responsive options if similar to defaults.
269 if ($responsives = $this->getResponsiveOptions()) {
271 foreach ($responsives as $key => $responsive) {
272 $cleaned[$key]['breakpoint'] = $responsives[$key]['breakpoint'];
274 // Destroy responsive slick if so configured.
275 if (!empty($responsives[$key]['unslick'])) {
276 $cleaned[$key]['settings'] = 'unslick';
277 unset($responsives[$key]['unslick']);
280 // Remove wasted dependent options if disabled, empty or not.
281 $this->removeWastedDependentOptions($responsives[$key]['settings']);
282 $cleaned[$key]['settings'] = array_diff_assoc($responsives[$key]['settings'], $defaults);
285 $config['responsive'] = $cleaned;
291 * Removes wasted dependent options, even if not empty.
293 public function removeWastedDependentOptions(array &$js) {
294 foreach (self::getDependentOptions() as $key => $option) {
295 if (isset($js[$key]) && empty($js[$key])) {
296 foreach ($option as $dependent) {
297 unset($js[$dependent]);
302 if (!empty($js['useCSS']) && !empty($js['cssEaseBezier'])) {
303 $js['cssEase'] = $js['cssEaseBezier'];
305 unset($js['cssEaseOverride'], $js['cssEaseBezier']);
309 * Defines the dependent options.
312 * The dependent options.
314 public static function getDependentOptions() {
315 $down_arrow = ['downArrowTarget', 'downArrowOffset'];
317 'arrows' => ['prevArrow', 'nextArrow', 'downArrow'] + $down_arrow,
318 'downArrow' => $down_arrow,
319 'autoplay' => ['pauseOnHover', 'pauseOnDotsHover', 'autoplaySpeed'],
320 'centerMode' => ['centerPadding'],
321 'dots' => ['dotsClass', 'appendDots'],
322 'swipe' => ['swipeToSlide'],
323 'useCSS' => ['cssEase', 'cssEaseBezier', 'cssEaseOverride'],
324 'vertical' => ['verticalSwiping'],
329 * Returns the trusted HTML ID of a single slick instance.
334 * @todo: Consider Blazy::getHtmlId() instead.
336 public static function getHtmlId($string = 'slick', $id = '') {
337 if (!isset(static::$slickId)) {
338 static::$slickId = 0;
341 // Do not use dynamic Html::getUniqueId, otherwise broken asnavfors.
342 return empty($id) ? Html::getId($string . '-' . ++static::$slickId) : strip_tags($id);
346 * Returns HTML or layout related settings to shut up notices.
349 * The default settings.
351 public static function htmlSettings() {
354 'current_view_mode' => '',
360 'media_switch' => '',
361 'optionset' => 'default',
367 'vertical_tn' => FALSE,
373 * Defines JS options required by theme_slick(), used with optimized option.
375 public static function jsSettings() {
378 'downArrowTarget' => '',
379 'downArrowOffset' => '',
380 'lazyLoad' => 'ondemand',
381 'prevArrow' => '<button type="button" data-role="none" class="slick-prev" aria-label="Previous" tabindex="0" role="button">Previous</button>',
382 'nextArrow' => '<button type="button" data-role="none" class="slick-next" aria-label="Next" tabindex="0" role="button">Next</button>',