3 namespace Drupal\blazy_ui\Form;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
10 * Defines blazy admin settings form.
12 class BlazySettingsForm extends ConfigFormBase {
17 public function getFormId() {
18 return 'blazy_settings';
24 protected function getEditableConfigNames() {
25 return ['blazy.settings'];
29 * Implements \Drupal\Core\Form\FormInterface::buildForm().
31 public function buildForm(array $form, FormStateInterface $form_state) {
32 $config = $this->config('blazy.settings');
34 $form['admin_css'] = [
35 '#type' => 'checkbox',
36 '#title' => $this->t('Admin CSS'),
37 '#default_value' => $config->get('admin_css'),
38 '#description' => $this->t('Uncheck to disable blazy related admin compact form styling, only if not compatible with your admin theme.'),
41 $form['responsive_image'] = [
42 '#type' => 'checkbox',
43 '#title' => $this->t('Support Responsive image'),
44 '#default_value' => $config->get('responsive_image'),
45 '#description' => $this->t('Check to support lazyloading for the core Responsive image module. Be sure to use Blazy formatter to have relevant styling.'),
48 $form['one_pixel'] = [
49 '#type' => 'checkbox',
50 '#title' => $this->t('One pixel placeholder'),
51 '#default_value' => $config->get('one_pixel'),
52 '#description' => $this->t('By default a one pixel image is the placeholder for lazyloaded Responsive image. Useful to perform a lot better. Uncheck to disable, and use Drupal-managed smallest/fallback image style instead. Be sure to add proper dimensions or at least min-height/min-width via CSS accordingly to avoid layout reflow since Aspect ratio is not supported with Responsive image yet. Disabling this will result in downloading fallback image as well for non-PICTURE element (double downloads).'),
56 '#type' => 'container',
58 '#title' => $this->t('Blazy JS settings'),
59 '#description' => $this->t('The following are JS related settings.'),
62 $form['blazy']['loadInvisible'] = [
63 '#type' => 'checkbox',
64 '#title' => $this->t('Load invisible'),
65 '#default_value' => $config->get('blazy.loadInvisible'),
66 '#description' => $this->t('Set to true if you want to load invisible (hidden) elements.'),
69 $form['blazy']['offset'] = [
70 '#type' => 'textfield',
71 '#title' => $this->t('Offset'),
72 '#default_value' => $config->get('blazy.offset'),
73 '#description' => $this->t("The offset controls how early you want the elements to be loaded before they're visible. Default is <strong>100</strong>, so 100px before an element is visible it'll start loading."),
74 '#field_suffix' => 'px',
79 $form['blazy']['saveViewportOffsetDelay'] = [
80 '#type' => 'textfield',
81 '#title' => $this->t('Save viewport offset delay'),
82 '#default_value' => $config->get('blazy.saveViewportOffsetDelay'),
83 '#description' => $this->t('Delay for how often it should call the saveViewportOffset function on resize. Default is <strong>50</strong>ms.'),
84 '#field_suffix' => 'ms',
89 return parent::buildForm($form, $form_state);
93 * Implements \Drupal\Core\Form\FormInterface::submitForm().
95 public function submitForm(array &$form, FormStateInterface $form_state) {
96 $this->configFactory->getEditable('blazy.settings')
97 ->set('admin_css', $form_state->getValue('admin_css'))
98 ->set('responsive_image', $form_state->getValue('responsive_image'))
99 ->set('one_pixel', $form_state->getValue('one_pixel'))
100 ->set('blazy.loadInvisible', $form_state->getValue(['blazy', 'loadInvisible']))
101 ->set('blazy.offset', $form_state->getValue(['blazy', 'offset']))
102 ->set('blazy.saveViewportOffsetDelay', $form_state->getValue(['blazy', 'saveViewportOffsetDelay']))
105 // Invalidate the library discovery cache to update the responsive image.
106 \Drupal::service('library.discovery')->clearCachedDefinitions();
108 drupal_set_message($this->t('Be sure to <a href=":clear_cache">clear the cache</a> if trouble to see the updated settings', [':clear_cache' => Url::fromRoute('system.performance_settings')->toString()]));
110 parent::submitForm($form, $form_state);