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 = [];
98 public function __construct(array $values, $entity_type = 'slick') {
99 parent::__construct($values, $entity_type);
103 * Overrides Drupal\Core\Entity\Entity::id().
105 public function id() {
112 public function getSkin() {
119 public function getBreakpoints() {
120 return $this->breakpoints;
126 public function getGroup() {
133 public function optimized() {
134 return $this->optimized;
140 public function getOptions($group = NULL, $property = NULL) {
142 if (is_array($group)) {
143 return NestedArray::getValue($this->options, (array) $group);
145 elseif (isset($property) && isset($this->options[$group])) {
146 return isset($this->options[$group][$property]) ? $this->options[$group][$property] : NULL;
148 return $this->options[$group];
151 return $this->options;
157 public function getSettings() {
158 // With the Optimized options, all defaults are cleaned out, merge em.
159 return isset($this->options['settings']) ? array_merge(self::defaultSettings(), $this->options['settings']) : self::defaultSettings();
165 public function setSettings(array $settings = []) {
166 $this->options['settings'] = $settings;
173 public function getSetting($setting_name) {
174 return isset($this->getSettings()[$setting_name]) ? $this->getSettings()[$setting_name] : NULL;
180 public static function defaultSettings($group = 'settings') {
181 return self::load('default')->options[$group];
185 * Overrides \Drupal\Core\Entity\Entity::create().
187 public static function create(array $values = []) {
188 $optionset = parent::create($values);
190 $optionset->setSettings($optionset->getSettings());
195 * Returns the Slick responsive settings.
197 public function getResponsiveOptions() {
198 if (empty($this->breakpoints)) {
202 if (isset($this->options['responsives']['responsive'])) {
203 $responsives = $this->options['responsives'];
204 if ($responsives['responsive']) {
205 foreach ($responsives['responsive'] as $delta => $responsive) {
206 if (empty($responsives['responsive'][$delta]['breakpoint'])) {
207 unset($responsives['responsive'][$delta]);
209 if (isset($responsives['responsive'][$delta])) {
210 $options[$delta] = $responsive;
219 * Sets the Slick responsive settings.
221 public function setResponsiveSettings($settings, $delta = 0) {
222 $this->options['responsives']['responsive'][$delta]['settings'] = $settings;
227 * Strip out options containing default values so to have real clean JSON.
229 public function removeDefaultValues(array $js) {
231 $defaults = self::defaultSettings();
233 // Remove wasted dependent options if disabled, empty or not.
234 $this->removeWastedDependentOptions($js);
235 $config = array_diff_assoc($js, $defaults);
237 // Remove empty lazyLoad, or left to default ondemand, to avoid JS error.
238 if (empty($config['lazyLoad'])) {
239 unset($config['lazyLoad']);
242 // Do not pass arrows HTML to JSON object as some are enforced.
250 foreach ($excludes as $key) {
251 unset($config[$key]);
254 // Clean up responsive options if similar to defaults.
255 if ($responsives = $this->getResponsiveOptions()) {
257 foreach ($responsives as $key => $responsive) {
258 $cleaned[$key]['breakpoint'] = $responsives[$key]['breakpoint'];
260 // Destroy responsive slick if so configured.
261 if ($responsives[$key]['unslick']) {
262 $cleaned[$key]['settings'] = 'unslick';
263 unset($responsives[$key]['unslick']);
266 // Remove wasted dependent options if disabled, empty or not.
267 $this->removeWastedDependentOptions($responsives[$key]['settings']);
268 $cleaned[$key]['settings'] = array_diff_assoc($responsives[$key]['settings'], $defaults);
271 $config['responsive'] = $cleaned;
277 * Removes wasted dependent options, even if not empty.
279 public function removeWastedDependentOptions(array &$js) {
280 foreach (self::getDependentOptions() as $key => $option) {
281 if (isset($js[$key]) && empty($js[$key])) {
282 foreach ($option as $dependent) {
283 unset($js[$dependent]);
288 if (!empty($js['useCSS']) && !empty($js['cssEaseBezier'])) {
289 $js['cssEase'] = $js['cssEaseBezier'];
291 unset($js['cssEaseOverride'], $js['cssEaseBezier']);
295 * Defines the dependent options.
297 public static function getDependentOptions() {
298 $down_arrow = ['downArrowTarget', 'downArrowOffset'];
300 'arrows' => ['prevArrow', 'nextArrow', 'downArrow'] + $down_arrow,
301 'downArrow' => $down_arrow,
302 'autoplay' => ['pauseOnHover', 'pauseOnDotsHover', 'autoplaySpeed'],
303 'centerMode' => ['centerPadding'],
304 'dots' => ['dotsClass', 'appendDots'],
305 'swipe' => ['swipeToSlide'],
306 'useCSS' => ['cssEase', 'cssEaseBezier', 'cssEaseOverride'],
307 'vertical' => ['verticalSwiping'],
312 * Returns the trusted HTML ID of a single slick instance.
314 * @todo: Consider Blazy::getHtmlId() instead.
316 public static function getHtmlId($string = 'slick', $id = '') {
317 $slick_id = &drupal_static('slick_id', 0);
319 // Do not use dynamic Html::getUniqueId, otherwise broken asnavfors.
320 return empty($id) ? Html::getId($string . '-' . ++$slick_id) : $id;
324 * Returns HTML or layout related settings to shut up notices.
326 public static function htmlSettings() {
329 'current_view_mode' => '',
335 'media_switch' => '',
336 'optionset' => 'default',
342 'vertical_tn' => FALSE,
348 * Defines JS options required by theme_slick(), used with optimized option.
350 public static function jsSettings() {
353 'downArrowTarget' => '',
354 'downArrowOffset' => '',
355 'lazyLoad' => 'ondemand',
356 'prevArrow' => '<button type="button" data-role="none" class="slick-prev" aria-label="Previous" tabindex="0" role="button">Previous</button>',
357 'nextArrow' => '<button type="button" data-role="none" class="slick-next" aria-label="Next" tabindex="0" role="button">Next</button>',