X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fmodules%2Fcontrib%2Fvideo_embed_field%2Fsrc%2FPlugin%2FField%2FFieldFormatter%2FLazyLoad.php;fp=web%2Fmodules%2Fcontrib%2Fvideo_embed_field%2Fsrc%2FPlugin%2FField%2FFieldFormatter%2FLazyLoad.php;h=4376a68be6ebc792d6c61abc33307e374028d9e5;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hp=0000000000000000000000000000000000000000;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0;p=yaffs-website diff --git a/web/modules/contrib/video_embed_field/src/Plugin/Field/FieldFormatter/LazyLoad.php b/web/modules/contrib/video_embed_field/src/Plugin/Field/FieldFormatter/LazyLoad.php new file mode 100644 index 000000000..4376a68be --- /dev/null +++ b/web/modules/contrib/video_embed_field/src/Plugin/Field/FieldFormatter/LazyLoad.php @@ -0,0 +1,182 @@ +thumbnailFormatter = $thumbnail_formatter; + $this->videoFormatter = $video_formatter; + $this->renderer = $renderer; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + $formatter_manager = $container->get('plugin.manager.field.formatter'); + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('renderer'), + $formatter_manager->createInstance('video_embed_field_thumbnail', $configuration), + $formatter_manager->createInstance('video_embed_field_video', $configuration) + ); + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + $element = []; + $thumbnails = $this->thumbnailFormatter->viewElements($items, $langcode); + $videos = $this->videoFormatter->viewElements($items, $langcode); + foreach ($items as $delta => $item) { + $itemThumb = [$thumbnails[$delta]]; + // Add a play button. + $itemThumb[] = [ + '#type' => 'html_tag', + '#tag' => 'div', + '#attributes' => [ + 'class' => ['video-embed-field-lazy-play'] + ], + ]; + $element[$delta] = [ + '#type' => 'container', + '#attributes' => [ + 'data-video-embed-field-lazy' => (string) $this->renderer->render($videos[$delta]), + 'class' => ['video-embed-field-lazy'], + ], + '#attached' => [ + 'library' => [ + 'video_embed_field/lazy-load', + ], + ], + // Ensure the cache context from the video formatter which was rendered + // early still exists in the renderable array for this formatter. + '#cache' => [ + 'contexts' => ['user.permissions'], + ], + 'children' => $itemThumb, + ]; + } + return $element; + } + + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + return Thumbnail::defaultSettings() + Video::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $element = parent::settingsForm($form, $form_state); + $element += $this->thumbnailFormatter->settingsForm([], $form_state); + $element += $this->videoFormatter->settingsForm([], $form_state); + return $element; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary[] = $this->t('Thumbnail that lazy loads video embed code.'); + $summary[] = implode(',', $this->videoFormatter->settingsSummary()); + $summary[] = implode(',', $this->thumbnailFormatter->settingsSummary()); + return $summary; + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return parent::calculateDependencies() + $this->thumbnailFormatter->calculateDependencies() + $this->videoFormatter->calculateDependencies(); + } + + /** + * {@inheritdoc} + */ + public function onDependencyRemoval(array $dependencies) { + $parent = parent::onDependencyRemoval($dependencies); + $thumbnail = $this->thumbnailFormatter->onDependencyRemoval($dependencies); + $video = $this->videoFormatter->onDependencyRemoval($dependencies); + $this->setSetting('image_style', $this->thumbnailFormatter->getSetting('image_style')); + return $parent || $thumbnail || $video; + } + +}