3 namespace Drupal\slick_ui\Form;
6 use Drupal\Core\Entity\EntityForm;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Drupal\slick\Form\SlickAdmin;
11 use Drupal\slick\SlickManagerInterface;
14 * Provides base form for a slick instance configuration form.
16 abstract class SlickFormBase extends EntityForm {
21 * @var \Drupal\slick\Form\SlickAdmin
26 * Constructs a SlickForm object.
28 public function __construct(SlickAdmin $admin, SlickManagerInterface $manager) {
29 $this->admin = $admin;
30 $this->manager = $manager;
36 public static function create(ContainerInterface $container) {
38 $container->get('slick.admin'),
39 $container->get('slick.manager')
46 public function form(array $form, FormStateInterface $form_state) {
47 // Change page title for the duplicate operation.
48 if ($this->operation == 'duplicate') {
49 $form['#title'] = $this->t('<em>Duplicate slick optionset</em>: @label', ['@label' => $this->entity->label()]);
50 $this->entity = $this->entity->createDuplicate();
53 // Change page title for the edit operation.
54 if ($this->operation == 'edit') {
55 $form['#title'] = $this->t('<em>Edit slick optionset</em>: @label', ['@label' => $this->entity->label()]);
58 $slick = $this->entity;
59 $path = drupal_get_path('module', 'slick');
60 $tooltip = ['class' => ['is-tooltip']];
61 $readme = Url::fromUri('base:' . $path . '/README.txt')->toString();
62 $admin_css = $this->manager->configLoad('admin_css', 'blazy.settings');
64 $form['#attributes']['class'][] = 'form--slick';
65 $form['#attributes']['class'][] = 'form--blazy';
66 $form['#attributes']['class'][] = 'form--optionset';
69 '#type' => 'textfield',
70 '#title' => $this->t('Label'),
71 '#default_value' => $slick->label(),
74 '#description' => $this->t("Label for the Slick optionset."),
75 '#attributes' => $tooltip,
76 '#prefix' => '<div class="form__header form__half form__half--first has-tooltip clearfix">',
79 // Keep the legacy CTools ID, i.e.: name as ID.
81 '#type' => 'machine_name',
82 '#default_value' => $slick->id(),
83 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
85 'source' => ['label'],
86 'exists' => '\Drupal\slick\Entity\Slick::load',
88 '#attributes' => $tooltip,
89 '#disabled' => !$slick->isNew(),
90 '#suffix' => '</div>',
95 '#title' => $this->t('Skin'),
96 '#options' => $this->admin->getSkinsByGroupOptions(),
97 '#empty_option' => $this->t('- None -'),
98 '#default_value' => $slick->getSkin(),
99 '#description' => $this->t('Skins allow swappable layouts like next/prev links, split image and caption, etc. However a combination of skins and options may lead to unpredictable layouts, get yourself dirty. See main <a href="@url">README</a> for details on Skins. Only useful for custom work, and ignored/overridden by slick formatters or sub-modules.', ['@url' => $readme]),
100 '#attributes' => $tooltip,
101 '#prefix' => '<div class="form__header form__half form__half--last has-tooltip clearfix">',
106 '#title' => $this->t('Group'),
109 'overlay' => t('Overlay'),
110 'thumbnail' => t('Thumbnail'),
112 '#empty_option' => $this->t('- None -'),
113 '#default_value' => $slick->getGroup(),
114 '#description' => $this->t('Group this optionset to avoid confusion for optionset selections. Leave empty to make it available for all.'),
115 '#attributes' => $tooltip,
118 $form['breakpoints'] = [
119 '#title' => $this->t('Breakpoints'),
120 '#type' => 'textfield',
121 '#default_value' => $form_state->hasValue('breakpoints') ? $form_state->getValue('breakpoints') : $slick->getBreakpoints(),
122 '#description' => $this->t('The number of breakpoints added to Responsive display, max 9. This is not Breakpoint Width (480px, etc).'),
124 'callback' => '::addBreakpoints',
125 'wrapper' => 'edit-breakpoints-ajax-wrapper',
127 'progress' => ['type' => 'fullscreen'],
131 '#attributes' => $tooltip,
135 $form['optimized'] = [
136 '#type' => 'checkbox',
137 '#title' => $this->t('Optimized'),
138 '#default_value' => $slick->optimized(),
139 '#description' => $this->t('Check to optimize the stored options. Anything similar to defaults will not be stored, except those required by sub-modules and theme_slick(). Like you hand-code/ cherry-pick the needed options, and are smart enough to not repeat defaults, and free up memory. The rest are taken care of by JS. Uncheck only if theme_slick() can not satisfy the needs, and more hand-coded preprocess is needed which is less likely in most cases.'),
140 '#access' => $slick->id() != 'default',
141 '#attributes' => $tooltip,
142 '#wrapper_attributes' => ['class' => ['form-item--tooltip-wide']],
145 if ($slick->id() == 'default') {
146 $form['breakpoints']['#suffix'] = '</div>';
149 $form['optimized']['#suffix'] = '</div>';
153 $form['optimized']['#field_suffix'] = ' ';
154 $form['optimized']['#title_display'] = 'before';
157 return parent::form($form, $form_state);
163 public function submitForm(array &$form, FormStateInterface $form_state) {
164 parent::submitForm($form, $form_state);
166 // Optimized if so configured.
167 $slick = $this->entity;
168 $default = $slick->id() == 'default';
169 if (!$default && !$form_state->isValueEmpty('optimized')) {
170 $defaults = $slick::defaultSettings();
171 $options = $form_state->getValue('options');
172 $required = $this->getOptionsRequiredByTemplate();
173 $main = array_diff_assoc($defaults, $required);
174 $settings = $form_state->getValue(['options', 'settings']);
177 $this->typecastOptionset($settings);
179 // Remove wasted dependent options if disabled, empty or not.
180 $slick->removeWastedDependentOptions($settings);
182 $main_settings = array_diff_assoc($settings, $main);
183 $slick->setSettings($main_settings);
185 $responsive_options = ['options', 'responsives', 'responsive'];
186 if ($responsives = $form_state->getValue($responsive_options)) {
187 foreach ($responsives as $delta => &$responsive) {
188 if (!empty($responsive['unslick'])) {
189 $slick->setResponsiveSettings([], $delta);
192 $this->typecastOptionset($responsive['settings']);
193 $slick->removeWastedDependentOptions($responsive['settings']);
195 $responsive_settings = array_diff_assoc($responsive['settings'], $defaults);
196 $slick->setResponsiveSettings($responsive_settings, $delta);
204 * Overrides Drupal\Core\Entity\EntityFormController::save().
206 * @todo revert #1497268, or use config_update instead.
208 public function save(array $form, FormStateInterface $form_state) {
209 $slick = $this->entity;
211 // Prevent leading and trailing spaces in slick names.
212 $slick->set('label', trim($slick->label()));
213 $slick->set('id', $slick->id());
215 $status = $slick->save();
216 $label = $slick->label();
217 $edit_link = $slick->toLink($this->t('Edit'), 'edit-form')->toString();
218 $config_prefix = $slick->getEntityType()->getConfigPrefix();
219 $message = ['@config_prefix' => $config_prefix, '%label' => $label];
222 '@config_prefix' => $config_prefix,
224 'link' => $edit_link,
227 if ($status == SAVED_UPDATED) {
228 // If we edited an existing entity.
230 drupal_set_message($this->t('@config_prefix %label has been updated.', $message));
231 $this->logger('slick')->notice('@config_prefix %label has been updated.', $notice);
234 // If we created a new entity.
235 drupal_set_message($this->t('@config_prefix %label has been added.', $message));
236 $this->logger('slick')->notice('@config_prefix %label has been added.', $notice);
241 * Handles switching the breakpoints based on the input value.
243 public function addBreakpoints($form, FormStateInterface $form_state) {
244 if (!$form_state->isValueEmpty('breakpoints')) {
245 $form_state->setValue('breakpoints_count', $form_state->getValue('breakpoints'));
246 if ($form_state->getValue('breakpoints') >= 6) {
247 $message = $this->t('You are trying to load too many Breakpoints. Try reducing it to reasonable numbers say, between 1 to 5.');
248 drupal_set_message($message, 'warning');
252 return $form['responsives']['responsive'];
256 * Returns the typecast values.
258 * @param array $settings
259 * An array of Optionset settings.
261 public function typecastOptionset(array &$settings = []) {
262 if (empty($settings)) {
266 $slick = $this->entity;
267 $defaults = $slick::defaultSettings();
269 foreach ($defaults as $name => $value) {
270 if (isset($settings[$name])) {
271 // Seems double is ignored, and causes a missing schema, unlike float.
272 $type = gettype($defaults[$name]);
273 $type = $type == 'double' ? 'float' : $type;
275 // Change float to integer if value is no longer float.
276 if ($name == 'edgeFriction') {
277 $type = $settings[$name] == '1' ? 'integer' : 'float';
280 settype($settings[$name], $type);
286 * List of all easing methods available from jQuery Easing v1.3.
289 * An array of available jQuery Easing options as fallback for browsers that
290 * don't support pure CSS easing.
292 public function getJsEasingOptions() {
293 $easings = &drupal_static(__METHOD__, NULL);
295 if (!isset($easings)) {
297 'linear' => 'Linear',
299 'easeInQuad' => 'easeInQuad',
300 'easeOutQuad' => 'easeOutQuad',
301 'easeInOutQuad' => 'easeInOutQuad',
302 'easeInCubic' => 'easeInCubic',
303 'easeOutCubic' => 'easeOutCubic',
304 'easeInOutCubic' => 'easeInOutCubic',
305 'easeInQuart' => 'easeInQuart',
306 'easeOutQuart' => 'easeOutQuart',
307 'easeInOutQuart' => 'easeInOutQuart',
308 'easeInQuint' => 'easeInQuint',
309 'easeOutQuint' => 'easeOutQuint',
310 'easeInOutQuint' => 'easeInOutQuint',
311 'easeInSine' => 'easeInSine',
312 'easeOutSine' => 'easeOutSine',
313 'easeInOutSine' => 'easeInOutSine',
314 'easeInExpo' => 'easeInExpo',
315 'easeOutExpo' => 'easeOutExpo',
316 'easeInOutExpo' => 'easeInOutExpo',
317 'easeInCirc' => 'easeInCirc',
318 'easeOutCirc' => 'easeOutCirc',
319 'easeInOutCirc' => 'easeInOutCirc',
320 'easeInElastic' => 'easeInElastic',
321 'easeOutElastic' => 'easeOutElastic',
322 'easeInOutElastic' => 'easeInOutElastic',
323 'easeInBack' => 'easeInBack',
324 'easeOutBack' => 'easeOutBack',
325 'easeInOutBack' => 'easeInOutBack',
326 'easeInBounce' => 'easeInBounce',
327 'easeOutBounce' => 'easeOutBounce',
328 'easeInOutBounce' => 'easeInOutBounce',
335 * List of available CSS easing methods.
338 * Flag to output the array as is for further processing if TRUE.
341 * An array of CSS easings for select options, or all for the mappings.
343 * @see https://github.com/kenwheeler/slick/issues/118
344 * @see http://matthewlein.com/ceaser/
345 * @see http://www.w3.org/TR/css3-transitions/
347 public function getCssEasingOptions($map = FALSE) {
349 $available_easings = array(
352 'ease' => 'ease|ease',
353 'linear' => 'linear|linear',
354 'ease-in' => 'ease-in|ease-in',
355 'ease-out' => 'ease-out|ease-out',
356 'swing' => 'swing|ease-in-out',
358 // Penner Equations (approximated).
359 'easeInQuad' => 'easeInQuad|cubic-bezier(0.550, 0.085, 0.680, 0.530)',
360 'easeInCubic' => 'easeInCubic|cubic-bezier(0.550, 0.055, 0.675, 0.190)',
361 'easeInQuart' => 'easeInQuart|cubic-bezier(0.895, 0.030, 0.685, 0.220)',
362 'easeInQuint' => 'easeInQuint|cubic-bezier(0.755, 0.050, 0.855, 0.060)',
363 'easeInSine' => 'easeInSine|cubic-bezier(0.470, 0.000, 0.745, 0.715)',
364 'easeInExpo' => 'easeInExpo|cubic-bezier(0.950, 0.050, 0.795, 0.035)',
365 'easeInCirc' => 'easeInCirc|cubic-bezier(0.600, 0.040, 0.980, 0.335)',
366 'easeInBack' => 'easeInBack|cubic-bezier(0.600, -0.280, 0.735, 0.045)',
367 'easeOutQuad' => 'easeOutQuad|cubic-bezier(0.250, 0.460, 0.450, 0.940)',
368 'easeOutCubic' => 'easeOutCubic|cubic-bezier(0.215, 0.610, 0.355, 1.000)',
369 'easeOutQuart' => 'easeOutQuart|cubic-bezier(0.165, 0.840, 0.440, 1.000)',
370 'easeOutQuint' => 'easeOutQuint|cubic-bezier(0.230, 1.000, 0.320, 1.000)',
371 'easeOutSine' => 'easeOutSine|cubic-bezier(0.390, 0.575, 0.565, 1.000)',
372 'easeOutExpo' => 'easeOutExpo|cubic-bezier(0.190, 1.000, 0.220, 1.000)',
373 'easeOutCirc' => 'easeOutCirc|cubic-bezier(0.075, 0.820, 0.165, 1.000)',
374 'easeOutBack' => 'easeOutBack|cubic-bezier(0.175, 0.885, 0.320, 1.275)',
375 'easeInOutQuad' => 'easeInOutQuad|cubic-bezier(0.455, 0.030, 0.515, 0.955)',
376 'easeInOutCubic' => 'easeInOutCubic|cubic-bezier(0.645, 0.045, 0.355, 1.000)',
377 'easeInOutQuart' => 'easeInOutQuart|cubic-bezier(0.770, 0.000, 0.175, 1.000)',
378 'easeInOutQuint' => 'easeInOutQuint|cubic-bezier(0.860, 0.000, 0.070, 1.000)',
379 'easeInOutSine' => 'easeInOutSine|cubic-bezier(0.445, 0.050, 0.550, 0.950)',
380 'easeInOutExpo' => 'easeInOutExpo|cubic-bezier(1.000, 0.000, 0.000, 1.000)',
381 'easeInOutCirc' => 'easeInOutCirc|cubic-bezier(0.785, 0.135, 0.150, 0.860)',
382 'easeInOutBack' => 'easeInOutBack|cubic-bezier(0.680, -0.550, 0.265, 1.550)',
385 foreach ($available_easings as $key => $easing) {
386 list($readable_easing, $css_easing) = array_pad(array_map('trim', explode("|", $easing, 2)), 2, NULL);
387 $css_easings[$key] = $map ? $easing : $readable_easing;
394 * Defines options required by theme_slick(), used with optimized option.
396 public function getOptionsRequiredByTemplate() {
398 'lazyLoad' => 'ondemand',
402 $this->manager->getModuleHandler()->alter('slick_options_required_by_template', $options);
407 * Maps existing jQuery easing value to equivalent CSS easing methods.
409 * @param string $easing
410 * The name of the human readable easing.
413 * A string of unfriendly bezier equivalent, or NULL.
415 public function getBezier($easing = NULL) {
418 $easings = $this->getCssEasingOptions(TRUE);
419 list($readable_easing, $bezier) = array_pad(array_map('trim', explode("|", $easings[$easing], 2)), 2, NULL);
420 $css_easing = $bezier;
421 unset($readable_easing);