Commit b9b683e1 authored by osman's avatar osman

Initial 8.x development snapshot

parent 39c2e79c
{
"name": "drupal/lazy",
"type": "drupal-module",
"description": "Lazy-loading images",
"keywords": ["Drupal", "bLazy", "Lazy-load"],
"license": "GPL-2.0+",
"homepage": "https://www.drupal.org/project/lazy",
"authors": [
{
"name": "Osman Gormus",
"homepage": "https://www.drupal.org/u/osman",
"role": "Maintainer"
}
],
"support": {
"issues": "https://www.drupal.org/project/issues/lazy",
"source": "http://cgit.drupalcode.org/lazy"
},
"require": {
"dinbror/blazy": "1.8.2"
},
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8"
},
{
"type": "package",
"package": {
"name": "dinbror/blazy",
"version": "1.8.2",
"type": "drupal-library",
"dist": {
"url": "https://github.com/dinbror/blazy/archive/1.8.2.zip",
"type": "zip"
}
}
}
]
}
errorClass: 'b-error'
loadInvisible: false
offset: 100
saveViewportOffsetDelay: 50
selector: 'b-lazy'
alter_tag:
img: 0
iframe: 0
skipClass: 'no-b-lazy'
src: 'data-src'
successClass: 'b-loaded'
validateDelay: 25
placeholderSrc: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
image_fields: false
(function ($) {
(function () {
'use strict';
Drupal.behaviors.lazy = {
attach: function (context, settings) {
var options = settings.lazy.bLazy ? settings.lazy.bLazy : {};
var options = settings.lazy ? settings.lazy : {};
new Blazy(options);
}
};
})(jQuery);
})();
<?php
/**
* @file
* Configuration form for Lazy-load.
*/
/**
* Lazy-load configuration form.
*/
function lazy_configuration_form($form, $form_state) {
$defaults = variable_get('lazy_filter_defaults');
$filter_enabled = lazy_is_filter_enabled();
$description = t('The %filter filter must be enabled for at least one <a href="!path">text-format</a>.', array(
'!path' => url('admin/config/content/formats'),
'%filter' => 'Lazy-load',
));
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Global settings'),
'#collapsible' => FALSE,
);
$form['settings']['lazy_filter_alter_tag'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the inline elements to be lazy-loaded via filter.'),
'#options' => array(
'img' => t('Images (%img tags)', array('%img' => '<img>')),
'iframe' => t('Iframes (%iframe tags)', array('%iframe' => '<iframe>')),
),
'#default_value' => variable_get('lazy_filter_alter_tag', $defaults['lazy_filter_alter_tag']),
'#description' => $filter_enabled ? '' : $description,
'#disabled' => !$filter_enabled,
);
$form['settings']['lazy_filter_image_fields'] = array(
'#type' => 'checkbox',
'#title' => t('Enable on image fields attached to fieldable entities. For example, content-types, blocks.'),
'#description' => t('Default is %val', array('%val' => ($defaults['lazy_filter_loadInvisible']) ? 'checked' : 'unchecked')),
'#default_value' => variable_get('lazy_filter_image_fields', $defaults['lazy_filter_image_fields']),
'#return_value' => TRUE,
);
$form['settings']['lazy_filter_placeholderSrc'] = array(
'#type' => 'textfield',
'#title' => t('Placeholder image URL'),
'#description' => t('Default is %val', array('%val' => $defaults['lazy_filter_placeholderSrc'])),
'#default_value' => variable_get('lazy_filter_placeholderSrc', $defaults['lazy_filter_placeholderSrc']),
);
$form['blazy'] = array(
'#type' => 'fieldset',
'#title' => t('bLazy configuration'),
'#description' => t('<p><a href="!url">bLazy</a> is a lightweight lazy loading and multi-serving image script created by Bjoern Klinggaard. See its website for usage details and demos.</p>', array(
'!url' => 'http://dinbror.dk/blog/blazy/',
)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['blazy']['lazy_filter_loadInvisible'] = array(
'#type' => 'checkbox',
'#title' => t('loadInvisible'),
'#description' => t('If checked loads invisible (hidden) elements. Default is %val', array('%val' => ($defaults['lazy_filter_loadInvisible']) ? 'checked' : 'unchecked')),
'#default_value' => variable_get('lazy_filter_loadInvisible', $defaults['lazy_filter_loadInvisible']),
'#return_value' => TRUE,
);
$form['blazy']['lazy_filter_offset'] = array(
'#type' => module_exists('elements') ? 'numberfield' : 'textfield',
'#title' => t('offset'),
'#description' => t('The offset controls how early you want the elements to be loaded before they’re visible. Default is %val, so %val pixel before an element is visible it’ll start loading.', array('%val' => $defaults['lazy_filter_offset'])),
'#default_value' => variable_get('lazy_filter_offset', $defaults['lazy_filter_offset']),
);
$form['blazy']['lazy_filter_saveViewportOffsetDelay'] = array(
'#type' => module_exists('elements') ? 'numberfield' : 'textfield',
'#title' => t('saveViewportOffsetDelay'),
'#description' => t('Delay for how often it should call the saveViewportOffset function on resize. Default is %val', array('%val' => $defaults['lazy_filter_saveViewportOffsetDelay'])),
'#default_value' => variable_get('lazy_filter_saveViewportOffsetDelay', $defaults['lazy_filter_saveViewportOffsetDelay']),
);
$form['blazy']['lazy_filter_validateDelay'] = array(
'#type' => module_exists('elements') ? 'numberfield' : 'textfield',
'#title' => t('validateDelay'),
'#description' => t('Delay for how often it should call the validate function on scroll/resize. Default is %val', array('%val' => $defaults['lazy_filter_validateDelay'])),
'#default_value' => variable_get('lazy_filter_validateDelay', $defaults['lazy_filter_validateDelay']),
);
$form['blazy']['lazy_filter_selector'] = array(
'#type' => 'textfield',
'#title' => t('Selector class'),
'#description' => t('Element selector for elements that should lazy load. Do not include a leading period. Default is %val', array('%val' => $defaults['lazy_filter_selector'])),
'#default_value' => variable_get('lazy_filter_selector', $defaults['lazy_filter_selector']),
);
$form['blazy']['lazy_filter_skipClass'] = array(
'#type' => 'textfield',
'#title' => t('skipClass'),
'#description' => t('Elements having this class name will be ignored. Default is %val', array('%val' => $defaults['lazy_filter_skipClass'])),
'#default_value' => variable_get('lazy_filter_skipClass', $defaults['lazy_filter_skipClass']),
);
$form['blazy']['lazy_filter_errorClass'] = array(
'#type' => 'textfield',
'#title' => t('errorClass'),
'#description' => t('The classname an element will get if something goes wrong. Default is %val', array('%val' => $defaults['lazy_filter_errorClass'])),
'#default_value' => variable_get('lazy_filter_errorClass', $defaults['lazy_filter_errorClass']),
);
$form['blazy']['lazy_filter_successClass'] = array(
'#type' => 'textfield',
'#title' => t('successClass'),
'#description' => t('The classname an element will get when loaded. Default is %val', array('%val' => $defaults['lazy_filter_successClass'])),
'#default_value' => variable_get('lazy_filter_successClass', $defaults['lazy_filter_successClass']),
);
$form['blazy']['lazy_filter_src'] = array(
'#type' => 'textfield',
'#title' => t('src'),
'#description' => t('Attribute where the original element source will be assigned. Do not change this unless attribute is used for other purposes. Default is %val', array('%val' => $defaults['lazy_filter_src'])),
'#default_value' => variable_get('lazy_filter_src', $defaults['lazy_filter_src']),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Submit handler for lazy_configuration_form().
*/
function lazy_configuration_form_submit($form, &$form_state) {
$int_value = array(
'lazy_filter_offset',
'lazy_filter_saveViewportOffsetDelay',
'lazy_filter_validateDelay',
);
$bool_value = array(
'lazy_filter_loadInvisible',
);
// Exclude unnecessary elements.
form_state_values_clean($form_state);
foreach ($form_state['values'] as $key => $value) {
if (in_array($key, $int_value)) {
$value = intval($value);
}
if (in_array($key, $bool_value)) {
$value = boolval($value);
}
variable_set($key, $value);
}
drupal_set_message(t('The configuration options have been saved.'));
}
name = Lazy-load
description = "This module integrates bLazy script to lazy load inline images and/or iframes via input-filters."
core = 7.x
dependencies[] = drupal:filter
dependencies[] = libraries
package = "Input filters"
configure = admin/config/content/lazy
name: 'Lazy-load'
type: module
description: 'This module integrates bLazy script to lazy load inline images and/or iframes via input-filters.'
core: 8.x
package: 'Input filters'
configure: lazy.config_form
dependencies:
- drupal:filter
......@@ -5,125 +5,37 @@
* Install, update, and uninstall functions for the Lazy-load module.
*/
/**
* Implements hook_install().
*/
function lazy_install() {
$defaults = array(
'lazy_filter_errorClass' => 'b-error',
'lazy_filter_loadInvisible' => FALSE,
'lazy_filter_offset' => 100,
'lazy_filter_saveViewportOffsetDelay' => 50,
'lazy_filter_selector' => 'b-lazy',
'lazy_filter_alter_tag' => array('img' => 'img', 'iframe' => 'iframe'),
'lazy_filter_skipClass' => 'no-b-lazy',
'lazy_filter_src' => 'data-src',
'lazy_filter_successClass' => 'b-loaded',
'lazy_filter_validateDelay' => 25,
'lazy_filter_placeholderSrc' => 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
'lazy_filter_image_fields' => FALSE,
);
variable_set('lazy_filter_defaults', $defaults);
foreach ($defaults as $key => $value) {
variable_set($key, $value);
}
}
/**
* Implements hook_enable().
*/
function lazy_enable() {
$options = array(
'!config' => url('admin/config/content/lazy'),
'%filter' => 'Lazy-load',
'!path' => url('admin/config/content/formats'),
);
$message = t('The <a href="!config">Lazy</a> module was installed. The new %filter filter should be enabled in desired <a href="!path">text-formats</a>.', $options);
drupal_set_message($message, 'warning');
}
/**
* Implements hook_uninstall().
*/
function lazy_uninstall() {
variable_del('lazy_filter_defaults');
variable_del('lazy_filter_errorClass');
variable_del('lazy_filter_loadInvisible');
variable_del('lazy_filter_offset');
variable_del('lazy_filter_saveViewportOffsetDelay');
variable_del('lazy_filter_selector');
variable_del('lazy_filter_alter_tag');
variable_del('lazy_filter_skipClass');
variable_del('lazy_filter_src');
variable_del('lazy_filter_successClass');
variable_del('lazy_filter_validateDelay');
variable_del('lazy_filter_placeholderSrc');
variable_del('lazy_filter_image_fields');
$module = 'lazy';
$name = 'lazy_filter';
db_delete('filter')
->condition('module', $module)
->condition('name', $name)
->execute();
}
/**
* Implements hook_requirements().
*/
function lazy_requirements($phase) {
$requirements = array();
$t = get_t();
$requirements = [];
if ($phase == 'runtime') {
$blazy = libraries_detect('blazy');
$requirements['blazy'] = array(
'title' => $t('bLazy'),
);
if ($blazy['installed']) {
$requirements['blazy']['value'] = $blazy['version'];
$requirements['blazy']['severity'] = REQUIREMENT_OK;
}
else {
$requirements['blazy']['value'] = $blazy['error'];
$requirements['blazy']['description'] = $blazy['error message'];
$requirements['blazy']['severity'] = REQUIREMENT_ERROR;
$library = Drupal::service('library.discovery')
->getLibraryByName('lazy', 'lazy-blazy');
$has_blazy = FALSE;
if ($library['js']) {
foreach ($library['js'] as $js) {
if ($js['type'] == 'file') {
if (file_exists(DRUPAL_ROOT . '/' . $js['data'])) {
$has_blazy = TRUE;
}
}
}
}
}
return $requirements;
}
$requirements['blazy'] = [
'title' => t('bLazy library'),
];
$requirements['blazy']['value'] = $has_blazy ? t('Enabled') : t('Not found');
$requirements['blazy']['severity'] = $has_blazy ? REQUIREMENT_OK : REQUIREMENT_WARNING;
/**
* Update module default values.
*/
function lazy_update_7100() {
// Get current default values.
$defaults = variable_get('lazy_filter_defaults');
// Update it from old string value to new array value.
$defaults['lazy_filter_alter_tag'] = array('img' => 'img', 'iframe' => 'iframe');
// Fix already existing value with new format.
$lazy_filter_alter_tag = variable_get('lazy_filter_alter_tag');
switch ($lazy_filter_alter_tag) {
case 'img':
variable_set('lazy_filter_alter_tag', array('img' => 'img', 'iframe' => 0));
break;
case 'iframe':
variable_set('lazy_filter_alter_tag', array('img' => 0, 'iframe' => 'iframe'));
break;
default:
variable_set('lazy_filter_alter_tag', $defaults['lazy_filter_alter_tag']);
break;
if (!$has_blazy) {
$requirements['blazy']['description'] = t('Lazy-load modules requires the bLazy library. See README.md file for instructions.');
}
}
// Add new setting to support image fields.
$defaults['lazy_filter_image_fields'] = FALSE;
// Update the defaults with new values.
variable_set('lazy_filter_defaults', $defaults);
variable_set('lazy_filter_image_fields', $defaults['lazy_filter_image_fields']);
return $requirements;
}
lazy:
version: VERSION
js:
js/lazy.js: { }
dependencies:
- core/drupalSettings
- lazy/lazy-blazy
lazy-blazy:
header: true
remote: https://github.com/dinbror/blazy
version: 1.8.2
license:
name: MIT
url: https://github.com/dinbror/blazy/blob/master/LICENSE
gpl-compatible: true
js:
# //cdn.jsdelivr.net/blazy/latest/blazy.min.js: { type: external, minified: true }
/libraries/blazy/blazy.min.js: { minified: true }
lazy.config_form:
title: 'Lazy-load configuration'
parent: system.admin_config_content
description: 'Configure how images and iframes are lazy-loaded in content.'
route_name: lazy.config_form
This diff is collapsed.
lazy.config_form:
path: 'admin/config/content/lazy'
defaults:
_title: 'Lazy-load'
_form: 'Drupal\lazy\Form\LazyForm'
requirements:
_permission: 'administer filters'
<?php
namespace Drupal\lazy\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Configure Lazy settings for this site.
*/
class LazyForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'lazy_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['lazy.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('lazy.settings');
$filter_enabled = lazy_is_enabled();
$description = $this->t('The %filter filter must be enabled for at least one <a href=":path">text-format</a>.', [
':path' => Url::fromRoute('filter.admin_overview')->toString(),
'%filter' => 'Lazy-load',
]);
if (
!boolval($config->get('alter_tag')['img'])
&& !boolval($config->get('alter_tag')['iframe'])
&& !boolval($config->get('image_fields'))
) {
drupal_set_message($this->t('buildForm: Lazy-load is currently disabled. Update configuration in global settings to enable it.'), 'warning');
}
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this->t('Global settings'),
'#open' => TRUE,
];
$form['settings']['alter_tag'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Select the inline elements to be lazy-loaded via filter.'),
'#options' => [
'img' => $this->t('Enable for images (%img tags)', ['%img' => '<img>']),
'iframe' => $this->t('Enable for iframes (%iframe tags)', ['%iframe' => '<iframe>']),
],
'#default_value' => $config->get('alter_tag'),
'#description' => $filter_enabled ? '' : $description,
'#disabled' => !$filter_enabled,
];
$form['settings']['image_fields'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable on image fields attached to fieldable entities. For example, content-types, blocks, paragraphs.'),
'#default_value' => $config->get('image_fields'),
'#return_value' => TRUE,
];
$form['settings']['placeholderSrc'] = [
'#type' => 'textfield',
'#title' => $this->t('Placeholder image URL'),
'#default_value' => $config->get('placeholderSrc'),
'#maxlength' => 255,
'#required' => TRUE,
];
$form['blazy'] = [
'#type' => 'details',
'#title' => $this->t('bLazy configuration'),
'#description' => $this->t('<p><a href=":url">bLazy</a> is a lightweight lazy loading and multi-serving image script created by Bjoern Klinggaard. See its website for usage details and demos.</p>', [
':url' => 'http://dinbror.dk/blog/blazy/',
]),
'#open' => FALSE,
];
$form['blazy']['loadInvisible'] = [
'#type' => 'checkbox',
'#title' => $this->t('loadInvisible'),
'#description' => $this->t('If checked loads invisible (hidden) elements.'),
'#default_value' => $config->get('loadInvisible'),
'#return_value' => TRUE,
];
$form['blazy']['offset'] = [
'#type' => 'number',
'#title' => $this->t('offset'),
'#description' => $this->t('The offset controls how early you want the elements to be loaded before they’re visible.'),
'#default_value' => $config->get('offset'),
'#min' => 0,
'#required' => TRUE,
];
$form['blazy']['saveViewportOffsetDelay'] = [
'#type' => 'number',
'#title' => $this->t('saveViewportOffsetDelay'),
'#description' => $this->t('Delay for how often it should call the saveViewportOffset function on resize.'),
'#default_value' => $config->get('saveViewportOffsetDelay'),
'#min' => 0,
'#required' => TRUE,
];
$form['blazy']['validateDelay'] = [
'#type' => 'number',
'#title' => $this->t('validateDelay'),
'#description' => $this->t('Delay for how often it should call the validate function on scroll/resize.'),
'#default_value' => $config->get('validateDelay'),
'#min' => 0,
'#required' => TRUE,
];
$form['blazy']['selector'] = [
'#type' => 'textfield',
'#title' => $this->t('Selector class'),
'#description' => $this->t('Element selector for elements that should lazy load. Do not include a leading period.'),
'#default_value' => $config->get('selector'),
'#required' => TRUE,
];
$form['blazy']['skipClass'] = [
'#type' => 'textfield',
'#title' => $this->t('skipClass'),
'#description' => $this->t('Elements having this class name will be ignored.'),
'#default_value' => $config->get('skipClass'),
'#required' => TRUE,
];
$form['blazy']['errorClass'] = [
'#type' => 'textfield',
'#title' => $this->t('errorClass'),
'#description' => $this->t('The classname an element will get if something goes wrong.'),
'#default_value' => $config->get('errorClass'),
'#required' => TRUE,
];
$form['blazy']['successClass'] = [
'#type' => 'textfield',
'#title' => $this->t('successClass'),
'#description' => $this->t('The classname an element will get when loaded.'),
'#default_value' => $config->get('successClass'),
'#required' => TRUE,
];
$form['blazy']['src'] = [
'#type' => 'textfield',
'#title' => $this->t('src'),
'#description' => $this->t('Attribute where the original element source will be assigned. Do not change this unless attribute is used for other purposes.'),
'#default_value' => $config->get('src'),
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('lazy.settings')
->set('alter_tag', $form_state->getValue('alter_tag'))
->set('errorClass', $form_state->getValue('errorClass'))
->set('loadInvisible', $form_state->getValue('loadInvisible'))
->set('offset', $form_state->getValue('offset'))
->set('saveViewportOffsetDelay', $form_state->getValue('saveViewportOffsetDelay'))
->set('selector', $form_state->getValue('selector'))
->set('skipClass', $form_state->getValue('skipClass'))
->set('src', $form_state->getValue('src'))
->set('successClass', $form_state->getValue('successClass'))
->set('validateDelay', $form_state->getValue('validateDelay'))
->set('placeholderSrc', $form_state->getValue('placeholderSrc'))
->set('image_fields', $form_state->getValue('image_fields'))
->save();
parent::submitForm($form, $form_state);
}
}
<?php
namespace Drupal\lazy\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
/**
* Provides a filter to lazy-load images.
*
* @Filter(
* id = "lazy_filter",
* title = @Translation("Lazyload images"),
* description = @Translation("Lazy load images via bLazy"),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
* weight = 20
* )
*/
class LazyFilter extends FilterBase {
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
$config = \Drupal::config('lazy.settings');
$opt_skipClass = $config->get('skipClass');
$opt_selector = ltrim($config->get('selector'), '.');
$opt_tags = $config->get('alter_tag');
$opt_src = ($config->get('src') !== 'src') ? $config->get('src') : 'data-filterlazy-src';
$opt_placeholderSrc = $config->get('placeholderSrc');
$result = new FilterProcessResult($text);
$html_dom = Html::load($text);
foreach ($opt_tags as $tag) {
$matches = $html_dom->getElementsByTagName($tag);
foreach ($matches as $element) {
$classes = $element->getAttribute('class');
$classes = (strlen($classes) > 0) ? explode(' ', $classes) : [];
$parent_classes = $element->parentNode->getAttribute('class');
$parent_classes = (strlen($parent_classes) > 0) ? explode(' ', $parent_classes) : [];
if (!in_array($opt_skipClass, $classes) && !in_array($opt_skipClass, $parent_classes)) {
$classes[] = $opt_selector;
$element->setAttribute('class', implode(' ', $classes));
$src = $element->getAttribute('src');
$element->removeAttribute('src');
$element->setAttribute($opt_src, $src);
$element->setAttribute('src', $opt_placeholderSrc);
}
}
}
$result->setProcessedText(Html::serialize($html_dom));
return $result;
}
/**
* {@inheritdoc}
*/
public function tips($long = FALSE) {
$tags = \Drupal::config('lazy.blazy')->get('alter_tag');
$tags = implode(' ', $tags);
$tags = trim(str_replace(['0'], '', $tags));
switch ($tags) {
case 'img iframe':
return t('Both %img and %iframe elements are lazy-loaded.', ['%img' => '<img>', '%iframe' => '<iframe>']);
case 'img':
return t('%img elements are lazy-loaded.', ['%img' => '<img>', '%iframe' => '<iframe>']);
case 'iframe':
return t('%iframe elements are lazy-loaded.', ['%img' => '<img>', '%iframe' => '<iframe>']);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment