ColorboxSettingsForm.php 18.9 KB
Newer Older
grisendo's avatar
grisendo committed
1
<?php
2

grisendo's avatar
grisendo committed
3 4
namespace Drupal\colorbox\Form;

grisendo's avatar
grisendo committed
5
use Drupal\Core\Form\ConfigFormBase;
frjo's avatar
frjo committed
6
use Drupal\Core\Form\FormStateInterface;
grisendo's avatar
grisendo committed
7 8 9 10

/**
 * General configuration form for controlling the colorbox behaviour..
 */
grisendo's avatar
grisendo committed
11
class ColorboxSettingsForm extends ConfigFormBase {
grisendo's avatar
grisendo committed
12

13 14 15 16 17 18 19 20 21 22
  /**
   * A state that represents the custom settings being enabled.
   */
  const STATE_CUSTOM_SETTINGS = 0;

  /**
   * A state that represents the slideshow being enabled.
   */
  const STATE_SLIDESHOW_ENABLED = 1;

grisendo's avatar
grisendo committed
23 24 25
  /**
   * {@inheritdoc}
   */
26
  public function getFormId() {
frjo's avatar
frjo committed
27
    return 'colorbox_admin_settings_form';
grisendo's avatar
grisendo committed
28 29
  }

30
  /**
31 32 33
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
34
    return ['colorbox.settings'];
35 36
  }

grisendo's avatar
grisendo committed
37 38 39
  /**
   * {@inheritdoc}
   */
frjo's avatar
frjo committed
40
  public function buildForm(array $form, FormStateInterface $form_state) {
grisendo's avatar
grisendo committed
41

42
    $config = $this->configFactory->get('colorbox.settings');
grisendo's avatar
grisendo committed
43

44
    $form['colorbox_custom_settings'] = [
45
      '#type' => 'details',
46
      '#title' => $this->t('Styles and options'),
47
      '#open' => TRUE,
48 49
    ];
    $colorbox_styles = [
50 51 52 53 54 55 56 57 58
      'default' => $this->t('Default'),
      'plain' => $this->t('Plain (mainly for images)'),
      'stockholmsyndrome' => $this->t('Stockholm Syndrome'),
      'example1' => $this->t('Example 1'),
      'example2' => $this->t('Example 2'),
      'example3' => $this->t('Example 3'),
      'example4' => $this->t('Example 4'),
      'example5' => $this->t('Example 5'),
      'none' => $this->t('None'),
59 60
    ];
    $form['colorbox_custom_settings']['colorbox_style'] = [
grisendo's avatar
grisendo committed
61
      '#type' => 'select',
62
      '#title' => $this->t('Style'),
grisendo's avatar
grisendo committed
63 64
      '#options' => $colorbox_styles,
      '#default_value' => $config->get('custom.style'),
65
      '#description' => $this->t('Select the style to use for the Colorbox. The example styles are the ones that come with the Colorbox plugin. Select "None" if you have added Colorbox styles to your theme.  <br> <strong>Examples</strong>: <ul><li><a href="/modules/colorbox/images/admin/example_default.png" target="blank">Default</a></li><li><a href="/modules/colorbox/images/admin/example_plain.png" target="blank">Plain</a></li><li><a href="/modules/colorbox/images/admin/example_stockholm_syndrome.png" target="blank">Stockholm Syndrome</a></li><li><a href="/modules/colorbox/images/admin/colorbox_example_1.png" target="blank">Example 1</a></li><li><a href="/modules/colorbox/images/admin/colorbox_example_2.png" target="blank">Example 2</a></li><li><a href="/modules/colorbox/images/admin/colorbox_example_3.png" target="blank">Example 3</a></li><li><a href="/modules/colorbox/images/admin/colorbox_example_4.png" target="blank">Example 4</a></li><li><a href="/modules/colorbox/images/admin/example_none.png" target="blank">None</a></li></ul>'),
66 67
    ];
    $form['colorbox_custom_settings']['colorbox_custom_settings_activate'] = [
grisendo's avatar
grisendo committed
68
      '#type' => 'radios',
69
      '#title' => $this->t('Options'),
70
      '#options' => [0 => $this->t('Default'), 1 => $this->t('Custom')],
grisendo's avatar
grisendo committed
71
      '#default_value' => $config->get('custom.activate'),
72
      '#description' => $this->t('Use the default or custom options for Colorbox.'),
73 74
    ];
    $form['colorbox_custom_settings']['colorbox_transition_type'] = [
grisendo's avatar
grisendo committed
75
      '#type' => 'radios',
76
      '#title' => $this->t('Transition type'),
77
      '#options' => [
78 79 80
        'elastic' => $this->t('Elastic'),
        'fade' => $this->t('Fade'),
        'none' => $this->t('None'),
81
      ],
grisendo's avatar
grisendo committed
82
      '#default_value' => $config->get('custom.transition_type'),
83
      '#description' => $this->t('The transition type.'),
84
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
85 86
    ];
    $form['colorbox_custom_settings']['colorbox_transition_speed'] = [
grisendo's avatar
grisendo committed
87
      '#type' => 'select',
88
      '#title' => $this->t('Transition speed'),
89
      '#options' => $this->optionsRange(100, 600, 50),
grisendo's avatar
grisendo committed
90
      '#default_value' => $config->get('custom.transition_speed'),
91
      '#description' => $this->t('Sets the speed of the fade and elastic transitions, in milliseconds.'),
92
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
93 94
    ];
    $form['colorbox_custom_settings']['colorbox_opacity'] = [
grisendo's avatar
grisendo committed
95
      '#type' => 'select',
96
      '#title' => $this->t('Opacity'),
97
      '#options' => $this->optionsRange(0, 1, 0.05),
grisendo's avatar
grisendo committed
98
      '#default_value' => $config->get('custom.opacity'),
99
      '#description' => $this->t('The overlay opacity level. Range: 0 to 1.'),
100
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
101 102
    ];
    $form['colorbox_custom_settings']['colorbox_text_current'] = [
grisendo's avatar
grisendo committed
103
      '#type' => 'textfield',
104
      '#title' => $this->t('Current'),
grisendo's avatar
grisendo committed
105 106
      '#default_value' => $config->get('custom.text_current'),
      '#size' => 30,
107
      '#description' => $this->t('Text format for the content group / gallery count. {current} and {total} are detected and replaced with actual numbers while Colorbox runs.'),
108
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
109 110
    ];
    $form['colorbox_custom_settings']['colorbox_text_previous'] = [
grisendo's avatar
grisendo committed
111
      '#type' => 'textfield',
112
      '#title' => $this->t('Previous'),
grisendo's avatar
grisendo committed
113 114
      '#default_value' => $config->get('custom.text_previous'),
      '#size' => 30,
115
      '#description' => $this->t('Text for the previous button in a shared relation group.'),
116
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
117 118
    ];
    $form['colorbox_custom_settings']['colorbox_text_next'] = [
grisendo's avatar
grisendo committed
119
      '#type' => 'textfield',
120
      '#title' => $this->t('Next'),
grisendo's avatar
grisendo committed
121 122
      '#default_value' => $config->get('custom.text_next'),
      '#size' => 30,
123
      '#description' => $this->t('Text for the next button in a shared relation group.'),
124
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
125 126
    ];
    $form['colorbox_custom_settings']['colorbox_text_close'] = [
grisendo's avatar
grisendo committed
127
      '#type' => 'textfield',
128
      '#title' => $this->t('Close'),
grisendo's avatar
grisendo committed
129 130
      '#default_value' => $config->get('custom.text_close'),
      '#size' => 30,
131
      '#description' => $this->t('Text for the close button. The "Esc" key will also close Colorbox.'),
132
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
133 134
    ];
    $form['colorbox_custom_settings']['colorbox_maxwidth'] = [
grisendo's avatar
grisendo committed
135
      '#type' => 'textfield',
136
      '#title' => $this->t('Max width'),
grisendo's avatar
grisendo committed
137 138
      '#default_value' => $config->get('custom.maxwidth'),
      '#size' => 30,
139
      '#description' => $this->t('Set a maximum width for loaded content. Example: "100%", 500, "500px".'),
140
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
141 142
    ];
    $form['colorbox_custom_settings']['colorbox_maxheight'] = [
grisendo's avatar
grisendo committed
143
      '#type' => 'textfield',
144
      '#title' => $this->t('Max height'),
grisendo's avatar
grisendo committed
145 146
      '#default_value' => $config->get('custom.maxheight'),
      '#size' => 30,
147
      '#description' => $this->t('Set a maximum height for loaded content. Example: "100%", 500, "500px".'),
148
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
149 150
    ];
    $form['colorbox_custom_settings']['colorbox_initialwidth'] = [
grisendo's avatar
grisendo committed
151
      '#type' => 'textfield',
152
      '#title' => $this->t('Initial width'),
grisendo's avatar
grisendo committed
153 154
      '#default_value' => $config->get('custom.initialwidth'),
      '#size' => 30,
155
      '#description' => $this->t('Set the initial width, prior to any content being loaded. Example: "100%", 500, "500px".'),
156
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
157 158
    ];
    $form['colorbox_custom_settings']['colorbox_initialheight'] = [
grisendo's avatar
grisendo committed
159
      '#type' => 'textfield',
160
      '#title' => $this->t('Initial height'),
grisendo's avatar
grisendo committed
161 162
      '#default_value' => $config->get('custom.initialheight'),
      '#size' => 30,
163
      '#description' => $this->t('Set the initial height, prior to any content being loaded. Example: "100%", 500, "500px".'),
164
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
165 166
    ];
    $form['colorbox_custom_settings']['colorbox_overlayclose'] = [
167
      '#type' => 'checkbox',
168
      '#title' => $this->t('Overlay close'),
169
      '#default_value' => $config->get('custom.overlayclose'),
170
      '#description' => $this->t('Enable closing Colorbox by clicking on the background overlay.'),
171
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
172 173
    ];
    $form['colorbox_custom_settings']['colorbox_returnfocus'] = [
174
      '#type' => 'checkbox',
175 176 177 178
      '#title' => $this->t('Return focus'),
      '#default_value' => $config->get('custom.returnfocus'),
      '#description' => $this->t('Return focus when Colorbox exits to the element it was launched from.'),
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
179 180
    ];
    $form['colorbox_custom_settings']['colorbox_fixed'] = [
grisendo's avatar
grisendo committed
181
      '#type' => 'checkbox',
182
      '#title' => $this->t('Fixed'),
grisendo's avatar
grisendo committed
183
      '#default_value' => $config->get('custom.fixed'),
184
      '#description' => $this->t("If the Colorbox should be displayed in a fixed position within the visitor's viewport or relative to the document."),
185
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
186 187
    ];
    $form['colorbox_custom_settings']['colorbox_scrolling'] = [
188
      '#type' => 'checkbox',
189
      '#title' => $this->t('Scrollbars'),
190
      '#default_value' => $config->get('custom.scrolling'),
191
      '#description' => $this->t('If unchecked, Colorbox will hide scrollbars for overflowing content. This could be used on conjunction with the resize method for a smoother transition if you are appending content to an already open instance of Colorbox.'),
192
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
193
    ];
grisendo's avatar
grisendo committed
194

195
    $form['colorbox_custom_settings']['colorbox_slideshow_settings'] = [
196
      '#type' => 'details',
197
      '#title' => $this->t('Slideshow settings'),
grisendo's avatar
grisendo committed
198 199
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
200
      '#states' => $this->getState(static::STATE_CUSTOM_SETTINGS),
201 202
    ];
    $form['colorbox_custom_settings']['colorbox_slideshow_settings']['colorbox_slideshow'] = [
grisendo's avatar
grisendo committed
203
      '#type' => 'radios',
204
      '#title' => $this->t('Slideshow'),
205
      '#options' => [0 => $this->t('Off'), 1 => $this->t('On')],
grisendo's avatar
grisendo committed
206
      '#default_value' => $config->get('custom.slideshow.slideshow'),
207
      '#description' => $this->t('An automatic slideshow to a content group / gallery.'),
208 209
    ];
    $form['colorbox_custom_settings']['colorbox_slideshow_settings']['colorbox_slideshowauto'] = [
grisendo's avatar
grisendo committed
210
      '#type' => 'checkbox',
211
      '#title' => $this->t('Slideshow autostart'),
grisendo's avatar
grisendo committed
212
      '#default_value' => $config->get('custom.slideshow.auto'),
213
      '#description' => $this->t('If the slideshow should automatically start to play.'),
214
      '#states' => $this->getState(static::STATE_SLIDESHOW_ENABLED),
215 216
    ];
    $form['colorbox_custom_settings']['colorbox_slideshow_settings']['colorbox_slideshowspeed'] = [
grisendo's avatar
grisendo committed
217
      '#type' => 'select',
218
      '#title' => $this->t('Slideshow speed'),
219
      '#options' => $this->optionsRange(1000, 6000, 500),
grisendo's avatar
grisendo committed
220
      '#default_value' => $config->get('custom.slideshow.speed'),
221
      '#description' => $this->t('Sets the speed of the slideshow, in milliseconds.'),
222
      '#states' => $this->getState(static::STATE_SLIDESHOW_ENABLED),
223 224
    ];
    $form['colorbox_custom_settings']['colorbox_slideshow_settings']['colorbox_text_start'] = [
grisendo's avatar
grisendo committed
225
      '#type' => 'textfield',
226
      '#title' => $this->t('Start slideshow'),
grisendo's avatar
grisendo committed
227 228
      '#default_value' => $config->get('custom.slideshow.text_start'),
      '#size' => 30,
229
      '#description' => $this->t('Text for the slideshow start button.'),
230
      '#states' => $this->getState(static::STATE_SLIDESHOW_ENABLED),
231 232
    ];
    $form['colorbox_custom_settings']['colorbox_slideshow_settings']['colorbox_text_stop'] = [
grisendo's avatar
grisendo committed
233
      '#type' => 'textfield',
234
      '#title' => $this->t('Stop slideshow'),
grisendo's avatar
grisendo committed
235 236
      '#default_value' => $config->get('custom.slideshow.text_stop'),
      '#size' => 30,
237
      '#description' => $this->t('Text for the slideshow stop button.'),
238
      '#states' => $this->getState(static::STATE_SLIDESHOW_ENABLED),
239
    ];
grisendo's avatar
grisendo committed
240

241
    $form['colorbox_advanced_settings'] = [
242
      '#type' => 'details',
243
      '#title' => $this->t('Advanced settings'),
244 245
    ];
    $form['colorbox_advanced_settings']['colorbox_unique_token'] = [
246
      '#type' => 'radios',
247
      '#title' => $this->t('Unique per-request gallery token'),
248
      '#options' => [1 => $this->t('On'), 0 => $this->t('Off')],
249
      '#default_value' => $config->get('advanced.unique_token'),
250
      '#description' => $this->t('If On (default), Colorbox will add a unique per-request token to the gallery id to avoid images being added manually to galleries. The token was added as a security fix but some see the old behavoiur as an feature and this settings makes it possible to remove the token.'),
251 252
    ];
    $form['colorbox_advanced_settings']['colorbox_mobile_detect'] = [
grisendo's avatar
grisendo committed
253
      '#type' => 'radios',
254
      '#title' => $this->t('Mobile detection'),
255
      '#options' => [1 => $this->t('On'), 0 => $this->t('Off')],
grisendo's avatar
grisendo committed
256
      '#default_value' => $config->get('advanced.mobile_detect'),
257
      '#description' => $this->t('If on (default) Colorbox will not be active for devices with the max width set below.'),
258 259
    ];
    $form['colorbox_advanced_settings']['colorbox_mobile_device_width'] = [
grisendo's avatar
grisendo committed
260
      '#type' => 'textfield',
261
      '#title' => $this->t('Device width'),
grisendo's avatar
grisendo committed
262 263
      '#default_value' => $config->get('advanced.mobile_device_width'),
      '#size' => 30,
264
      '#description' => $this->t('Set the mobile device max width. Default: 480px.'),
265 266 267 268 269 270 271
      '#states' => [
        'visible' => [
          ':input[name="colorbox_mobile_detect"]' => ['value' => '1'],
        ],
      ],
    ];
    $form['colorbox_advanced_settings']['colorbox_caption_trim'] = [
grisendo's avatar
grisendo committed
272
      '#type' => 'radios',
273
      '#title' => $this->t('Caption shortening'),
274
      '#options' => [0 => $this->t('Default'), 1 => $this->t('Yes')],
grisendo's avatar
grisendo committed
275
      '#default_value' => $config->get('advanced.caption_trim'),
276
      '#description' => $this->t('If the caption should be made shorter in the Colorbox to avoid layout problems. The default is to shorten for the example styles, they need it, but not for other styles.'),
277 278
    ];
    $form['colorbox_advanced_settings']['colorbox_caption_trim_length'] = [
grisendo's avatar
grisendo committed
279
      '#type' => 'select',
280
      '#title' => $this->t('Caption max length'),
281
      '#options' => $this->optionsRange(40, 120, 5),
grisendo's avatar
grisendo committed
282
      '#default_value' => $config->get('advanced.caption_trim_length'),
283 284 285 286 287 288 289
      '#states' => [
        'visible' => [
          ':input[name="colorbox_caption_trim"]' => ['value' => '1'],
        ],
      ],
    ];
    $form['colorbox_advanced_settings']['colorbox_compression_type'] = [
grisendo's avatar
grisendo committed
290
      '#type' => 'radios',
291
      '#title' => $this->t('Choose Colorbox compression level'),
292
      '#options' => [
293 294
        'minified' => $this->t('Production (Minified)'),
        'source' => $this->t('Development (Uncompressed Code)'),
295
      ],
grisendo's avatar
grisendo committed
296
      '#default_value' => $config->get('advanced.compression_type'),
297
    ];
grisendo's avatar
grisendo committed
298

299 300 301 302 303 304 305 306 307 308 309 310
    $form['colorbox_extras'] = [
      '#type' => 'details',
      '#title' => $this->t('Extras'),
    ];

    $form['colorbox_extras']['colorbox_additional_tips'] = [
      '#markup' => $this->t('You can find new features in the <a href="@url_colorbox_load" target="blank">Colorbox Load</a> and <a href="@url_colorbox_inline" target="blank">Colorbox Inline</a>', [
        '@url_colorbox_load' => 'https://www.drupal.org/project/colorbox_load',
        '@url_colorbox_inline' => 'https://www.drupal.org/project/colorbox_inline',
      ]),
    ];

grisendo's avatar
grisendo committed
311 312 313 314 315 316
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
frjo's avatar
frjo committed
317
  public function submitForm(array &$form, FormStateInterface $form_state) {
grisendo's avatar
grisendo committed
318

319
    $config = $this->configFactory->getEditable('colorbox.settings');
grisendo's avatar
grisendo committed
320 321

    $config
322
      ->set('custom.style', $form_state->getValue('colorbox_style'))
323 324 325 326 327 328 329 330 331
      ->set('custom.activate', $form_state->getValue('colorbox_custom_settings_activate'))
      ->set('custom.transition_type', $form_state->getValue('colorbox_transition_type'))
      ->set('custom.transition_speed', $form_state->getValue('colorbox_transition_speed'))
      ->set('custom.opacity', $form_state->getValue('colorbox_opacity'))
      ->set('custom.text_current', $form_state->getValue('colorbox_text_current'))
      ->set('custom.text_previous', $form_state->getValue('colorbox_text_previous'))
      ->set('custom.text_next', $form_state->getValue('colorbox_text_next'))
      ->set('custom.text_close', $form_state->getValue('colorbox_text_close'))
      ->set('custom.overlayclose', $form_state->getValue('colorbox_overlayclose'))
332
      ->set('custom.returnfocus', $form_state->getValue('colorbox_returnfocus'))
333 334 335 336 337 338 339 340 341 342 343
      ->set('custom.maxwidth', $form_state->getValue('colorbox_maxwidth'))
      ->set('custom.maxheight', $form_state->getValue('colorbox_maxheight'))
      ->set('custom.initialwidth', $form_state->getValue('colorbox_initialwidth'))
      ->set('custom.initialheight', $form_state->getValue('colorbox_initialheight'))
      ->set('custom.fixed', $form_state->getValue('colorbox_fixed'))
      ->set('custom.scrolling', $form_state->getValue('colorbox_scrolling'))
      ->set('custom.slideshow.slideshow', $form_state->getValue('colorbox_slideshow'))
      ->set('custom.slideshow.auto', $form_state->getValue('colorbox_slideshowauto'))
      ->set('custom.slideshow.speed', $form_state->getValue('colorbox_slideshowspeed'))
      ->set('custom.slideshow.text_start', $form_state->getValue('colorbox_text_start'))
      ->set('custom.slideshow.text_stop', $form_state->getValue('colorbox_text_stop'))
344
      ->set('advanced.unique_token', $form_state->getValue('colorbox_unique_token'))
345
      ->set('advanced.mobile_detect', $form_state->getValue('colorbox_mobile_detect'))
346
      ->set('advanced.mobile_device_width', $form_state->getValue('colorbox_mobile_device_width'))
347 348 349
      ->set('advanced.caption_trim', $form_state->getValue('colorbox_caption_trim'))
      ->set('advanced.caption_trim_length', $form_state->getValue('colorbox_caption_trim_length'))
      ->set('advanced.compression_type', $form_state->getValue('colorbox_compression_type'));
grisendo's avatar
grisendo committed
350

351 352
    if ($form_state->getValue('colorbox_image_style')) {
      $config->set('insert.image_style', $form_state->getValue('colorbox_image_style'));
grisendo's avatar
grisendo committed
353 354
    }

355 356
    if ($form_state->getValue('colorbox_insert_gallery')) {
      $config->set('insert.insert_gallery', $form_state->getValue('colorbox_insert_gallery'));
grisendo's avatar
grisendo committed
357 358 359 360 361 362 363
    }

    $config->save();

    parent::submitForm($form, $form_state);
  }

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
  /**
   * Get one of the pre-defined states used in this form.
   *
   * @param string $state
   *   The state to get that matches one of the state class constants.
   *
   * @return array
   *   A corresponding form API state.
   */
  protected function getState($state) {
    $states = [
      static::STATE_CUSTOM_SETTINGS => [
        'visible' => [
          ':input[name="colorbox_custom_settings_activate"]' => ['value' => '1'],
        ],
      ],
      static::STATE_SLIDESHOW_ENABLED => [
        'visible' => [
382
          ':input[name="colorbox_slideshow"]' => ['value' => '1'],
383 384 385 386 387 388
        ],
      ],
    ];
    return $states[$state];
  }

389 390 391
  /**
   * Create a range for a series of options.
   *
392
   * @param int $start
393
   *   The start of the range.
394
   * @param int $end
395
   *   The end of the range.
396
   * @param int $step
397 398 399 400 401 402 403 404 405 406
   *   The interval between elements.
   *
   * @return array
   *   An options array for the given range.
   */
  protected function optionsRange($start, $end, $step) {
    $range = range($start, $end, $step);
    return array_combine($range, $range);
  }

grisendo's avatar
grisendo committed
407
}