Skip to content

Commit

Permalink
Merge pull request #7844 from MrKarlDilkington/youtube_lazy_loading
Browse files Browse the repository at this point in the history
Add YouTube block lazy loading
  • Loading branch information
aembler committed Jan 21, 2020
2 parents 839011f + 23fcd05 commit e85fecc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
4 changes: 4 additions & 0 deletions concrete/blocks/youtube/controller.php
Expand Up @@ -123,6 +123,8 @@ public function save($data)
'startTime' => '',

'noCookie' => false,

'lazyLoad' => false
];

$args = [
Expand All @@ -147,6 +149,8 @@ public function save($data)
'startTime' => trim($data['startTime']),

'noCookie' => $data['noCookie'] ? 1 : 0,

'lazyLoad' => $data['lazyLoad'] ? 1 : 0
];
if ($args['sizing'] === 'fixed') {
$args += [
Expand Down
4 changes: 4 additions & 0 deletions concrete/blocks/youtube/db.xml
Expand Up @@ -35,6 +35,10 @@
<default value="0"/>
<notnull/>
</field>
<field name="lazyLoad" type="boolean">
<default value="0"/>
<notnull/>
</field>
<field name="rel" type="boolean">
<default value="0"/>
<notnull/>
Expand Down
33 changes: 21 additions & 12 deletions concrete/blocks/youtube/form_setup_html.php
@@ -1,18 +1,17 @@
<?php defined('C5_EXECUTE') or die('Access Denied.'); ?>
<?php
<?php defined('C5_EXECUTE') or die('Access Denied.');

if (empty($vWidth)) {
$vWidth = 640;
}

if (empty($vHeight)) {
$vHeight = 360;
}

if (empty($sizing)) {
$sizing = '16:9';
}
?>

<?php
echo Core::make('helper/concrete/ui')->tabs([
['video', t('Video'), true],
['settings', t('Settings')],
Expand Down Expand Up @@ -138,49 +137,59 @@
<div class="form-group">
<?php echo $form->text('startTime', isset($startTime) ? $startTime : null); ?>
</div>

</div>
</fieldset>
<fieldset>
<legend><?php echo t('Privacy Options'); ?></legend>
<div class="form-group">
<div class="checkbox">
<label>
<?php echo $form->checkbox('noCookie', 1, $noCookie); ?>
<?php echo t('No Cookie'); ?>
<?php echo $form->checkbox('noCookie', 1, (isset($noCookie) ? $noCookie : false)); ?>
<?php echo t('No cookie') ?>
</label>
</div>
</div>
</fieldset>
<fieldset>
<legend><?php echo t('Loading Options'); ?></legend>
<div class="form-group">
<div class="checkbox">
<label>
<?php echo $form->checkbox('lazyLoad', 1, (isset($lazyLoad) ? $lazyLoad : false)); ?>
<?php echo t('Lazy load video')?>
</label>
</div>
</div>
</fieldset>
</div>

<script>
$(document).ready(function() {
$('#sizing1, #sizing2, #sizing3').change(function(){
$(document).ready(function () {
$('#sizing1, #sizing2, #sizing3').change(function () {
if ($('#sizing3').is(':checked')) {
$('#fixedsizes').removeClass('hidden');
} else {
$('#fixedsizes').addClass('hidden');
}
});

$('#controls').change(function(){
$('#controls').change(function () {
if ($('#controls').is(':checked')) {
$('.controls-only').removeClass('hidden');
} else {
$('.controls-only').addClass('hidden');
}
});

$('#color').change(function(){
$('#color').change(function () {
if ($(this).val() == 'white') {
$('#modestbranding').prop('disabled','disabled').prop('checked',false);
} else {
$('#modestbranding').removeProp('disabled');
}
});

$('#ccm-form-submit-button').click(function(){
$('#ccm-form-submit-button').click(function () {
if (!$('#videoURL').val()) {
$('[data-tab="video"').click();
}
Expand Down
14 changes: 10 additions & 4 deletions concrete/blocks/youtube/view.php
Expand Up @@ -2,6 +2,7 @@

$responsiveClass = 'youtubeBlockResponsive16by9';
$sizeDisabled = '';
$lazyLoadAttribute = '';

if ($vWidth && $vHeight) {
$sizeargs = 'width="' . $vWidth . '" height="' . $vHeight . '"';
Expand All @@ -11,6 +12,10 @@
$responsiveClass = 'youtubeBlockResponsive4by3';
}

if (isset($lazyLoad) && $lazyLoad) {
$lazyLoadAttribute = 'loading="lazy"';
}

$params = [];

if (isset($playlist)) {
Expand Down Expand Up @@ -77,10 +82,11 @@
</div>
<?php
$loc->popActiveContext();
} else {
?>
} else { ?>
<div id="youtube<?= $bID; ?>" class="youtubeBlock <?php echo $responsiveClass; ?>">
<iframe class="youtube-player" <?php echo $sizeargs; ?> src="//<?= $youtubeDomain; ?>/embed/<?= $videoID; ?><?= $paramstring; ?>" allow="autoplay" allowfullscreen></iframe>
<iframe class="youtube-player" <?php echo $sizeargs; ?>
src="//<?= $youtubeDomain; ?>/embed/<?= $videoID; ?><?= $paramstring; ?>"
allow="autoplay" allowfullscreen <?= $lazyLoadAttribute; ?>></iframe>
</div>
<?php
} ?>
}
@@ -0,0 +1,13 @@
<?php
namespace Concrete\Core\Updater\Migrations\Migrations;

use Concrete\Core\Updater\Migrations\AbstractMigration;
use Concrete\Core\Updater\Migrations\RepeatableMigrationInterface;

class Version20200118043285 extends AbstractMigration implements RepeatableMigrationInterface
{
public function upgradeDatabase()
{
$this->refreshBlockType('youtube');
}
}

0 comments on commit e85fecc

Please sign in to comment.