diff --git a/modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php b/modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php index 836b723..3c29cd4 100644 --- a/modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php +++ b/modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php @@ -90,7 +90,28 @@ public function buildForm(array $form, FormStateInterface $form_state, FilterFor } // Create a settings form from the existing video formatter. - $form['settings'] = Video::mockInstance($settings)->settingsForm([], new FormState());; + $form['settings'] = Video::mockInstance($settings)->settingsForm([], new FormState()); + $form['settings']['lazy_load'] = [ + '#title' => $this->t('Lazy load video'), + '#type' => 'checkbox', + '#description' => $this->t("Video will load after clicking image."), + '#default_value' => $settings['lazy_load'] ?? 0, + ]; + $form['settings']['image_style'] = [ + '#title' => $this->t('Image Style'), + '#type' => 'select', + '#default_value' => $settings['image_style'], + '#required' => FALSE, + '#options' => image_style_options(), + '#states' => [ + 'visible' => [ + [ + ':input[name*="lazy_load"]' => ['checked' => TRUE], + ], + ], + ], + ]; + $form['settings']['#type'] = 'fieldset'; $form['settings']['#title'] = $this->t('Settings'); @@ -143,6 +164,8 @@ protected function getClientValues(FormStateInterface $form_state, ProviderPlugi foreach ($video_formatter_settings as $key => $default) { $video_formatter_settings[$key] = $form_state->getValue($key); } + $video_formatter_settings['lazy_load'] = $form_state->getValue('lazy_load'); + $video_formatter_settings['image_style'] = $form_state->getValue('image_style'); $provider->downloadThumbnail(); $thumbnail_preview = ImageStyle::load('video_embed_wysiwyg_preview')->buildUrl($provider->getLocalThumbnailUri()); diff --git a/modules/video_embed_wysiwyg/src/Plugin/CKEditorPlugin/VideoEmbedWysiwyg.php b/modules/video_embed_wysiwyg/src/Plugin/CKEditorPlugin/VideoEmbedWysiwyg.php index 0966210..dd20c20 100644 --- a/modules/video_embed_wysiwyg/src/Plugin/CKEditorPlugin/VideoEmbedWysiwyg.php +++ b/modules/video_embed_wysiwyg/src/Plugin/CKEditorPlugin/VideoEmbedWysiwyg.php @@ -65,6 +65,26 @@ public function settingsForm(array $form, FormStateInterface $form_state, Editor '#tree' => TRUE, 'children' => Video::mockInstance($settings)->settingsForm([], new FormState()), ]; + $form['defaults']['children']['lazy_load'] = [ + '#title' => $this->t('Lazy load video'), + '#type' => 'checkbox', + '#description' => $this->t("Video will load after clicking image."), + '#default_value' => $settings['lazy_load'] ?? 0, + ]; + $form['defaults']['children']['image_style'] = [ + '#title' => $this->t('Image Style'), + '#type' => 'select', + '#default_value' => $settings['image_style'] ?? NULL, + '#required' => FALSE, + '#options' => image_style_options(), + '#states' => [ + 'visible' => [ + [ + ':input[name*="lazy_load"]' => ['checked' => TRUE], + ], + ], + ], + ]; return $form; } diff --git a/modules/video_embed_wysiwyg/src/Plugin/Filter/VideoEmbedWysiwyg.php b/modules/video_embed_wysiwyg/src/Plugin/Filter/VideoEmbedWysiwyg.php index f0c50e7..79c7d56 100644 --- a/modules/video_embed_wysiwyg/src/Plugin/Filter/VideoEmbedWysiwyg.php +++ b/modules/video_embed_wysiwyg/src/Plugin/Filter/VideoEmbedWysiwyg.php @@ -88,6 +88,7 @@ public function process($text, $langcode) { } $autoplay = $this->currentUser->hasPermission('never autoplay videos') ? FALSE : $embed_data['settings']['autoplay']; + $lazy_load = $embed_data['settings']['lazy_load'] ?? FALSE; $embed_code = $provider->renderEmbedCode($embed_data['settings']['width'], $embed_data['settings']['height'], $autoplay); $embed_code = [ @@ -106,12 +107,37 @@ public function process($text, $langcode) { $embed_code['#attributes']['class'][] = 'video-embed-field-responsive-video'; } + // Change output if it is lazy load and provider used iframe output, + // as custom providers could implement htm5 video tag. + if ($lazy_load && isset($embed_code['children']['#type']) && $embed_code['children']['#type'] === 'video_embed_iframe') { + $provider->downloadThumbnail(); + $thumbnail = $provider->renderThumbnail($embed_data['settings']['image_style'], ''); + $thumbnail = [$thumbnail]; + // Add a play button. + $thumbnail[] = [ + '#type' => 'html_tag', + '#tag' => 'div', + '#attributes' => [ + 'class' => ['video-embed-field-lazy-play'], + ], + ]; + $embed_code = [ + '#type' => 'container', + '#attributes' => [ + 'class' => ['video-embed-field-lazy'], + 'data-video-embed-field-lazy' => (string) $this->renderer->render($embed_code), + ], + 'children' => $thumbnail, + ]; + $response->addAttachments(['library' => ['video_embed_field/lazy-load']]); + } + // Replace the JSON settings with a video. $text = str_replace($source_text, $this->renderer->render($embed_code), $text); // Add the required responsive video library only when at least one match // is present. - $response->setAttachments(['library' => ['video_embed_field/responsive-video']]); + $response->addAttachments(['library' => ['video_embed_field/responsive-video']]); $response->setCacheContexts(['user.permissions']); }