Commit 7d98315b authored by osman's avatar osman

* Enable `<img>` tag by default if filter-formats enabled.

* Provide `lazy/lazysizes` library for custom solutions.
* replaced `isPathDisabled()` method with `isPathAllowed()` for better readability.
* Add a preview image in setting form.
* Re-format the settings form.
parent 80c1efde
image_fields: { }
alter_tag:
img: 0
img: img
iframe: 0
skipClass: no-lazy
disabled_paths: /rss.xml
......
<?php
/**
* Lazy-load API.
*
* - How to add other image-based field formatters?
* - Services.
*/
/**
* Alter enabled field formatters for lazy-loading.
*
* @param $formatters
* Array of field formatters.
*
* @return array
*/
function hook_lazy_field_formatters_alter(&$formatters) {
$formatters[] = 'hook_formatter_name';
return $formatters;
}
$disabled_paths = '/about/logo';
\Drupal::service('lazy')->isPathAllowed($disabled_paths);
# Lazy-load `lazy/lazy`
lazy:
version: VERSION
js:
......@@ -7,3 +8,41 @@ lazy:
css/lazysizes.css: { }
dependencies:
- core/drupal
# Lazysizes library and its plugins `lazy/lazysizes`
#
# This library is not used within the Lazy-load module.
# They are loaded conditionally and asynchronously from `js/lazy.js`.
# It's provided here only to support customized solutions.
lazysizes:
header: true
remote: https://github.com/aFarkas/lazysizes
version: 5.1.1
license:
name: MIT
url: https://github.com/aFarkas/lazysizes/blob/master/LICENSE
js:
/libraries/lazysizes/plugins/artdirect/ls.artdirect.min.js: { minified: true }
/libraries/lazysizes/plugins/aspectratio/ls.aspectratio.min.js: { minified: true }
/libraries/lazysizes/plugins/attrchange/ls.attrchange.min.js: { minified: true }
/libraries/lazysizes/plugins/bgset/ls.bgset.min.js: { minified: true }
/libraries/lazysizes/plugins/blur-up/ls.blur-up.min.js: { minified: true }
/libraries/lazysizes/plugins/custommedia/ls.custommedia.min.js: { minified: true }
/libraries/lazysizes/plugins/fix-edge-h-descriptor/ls.fix-edge-h-descriptor.min.js: { minified: true }
/libraries/lazysizes/plugins/fix-ios-sizes/fix-ios-sizes.min.js: { minified: true }
/libraries/lazysizes/plugins/include/ls.include.min.js: { minified: true }
/libraries/lazysizes/plugins/native-loading/ls.native-loading.min.js: { minified: true }
/libraries/lazysizes/plugins/noscript/ls.noscript.min.js: { minified: true }
/libraries/lazysizes/plugins/object-fit/ls.object-fit.min.js: { minified: true }
/libraries/lazysizes/plugins/optimumx/ls.optimumx.min.js: { minified: true }
/libraries/lazysizes/plugins/parent-fit/ls.parent-fit.min.js: { minified: true }
/libraries/lazysizes/plugins/print/ls.print.min.js: { minified: true }
/libraries/lazysizes/plugins/progressive/ls.progressive.min.js: { minified: true }
/libraries/lazysizes/plugins/respimg/ls.respimg.min.js: { minified: true }
/libraries/lazysizes/plugins/rias/ls.rias.min.js: { minified: true }
/libraries/lazysizes/plugins/static-gecko-picture/ls.static-gecko-picture.min.js: { minified: true }
/libraries/lazysizes/plugins/twitter/ls.twitter.min.js: { minified: true }
/libraries/lazysizes/plugins/unload/ls.unload.min.js: { minified: true }
/libraries/lazysizes/plugins/unveilhooks/ls.unveilhooks.min.js: { minified: true }
/libraries/lazysizes/plugins/video-embed/ls.video-embed.min.js: { minified: true }
/libraries/lazysizes/lazysizes.min.js: { minified: true }
......@@ -44,10 +44,7 @@ function lazy_help($route_name) {
*/
function lazy_field_types() {
@trigger_error('lazy_field_types() is deprecated in lazy:8.3.x and will be removed from lazy:8.3.1. Use lazy_field_formatters(). See https://www.drupal.org/node/0000000', E_USER_DEPRECATED);
return [
'colorbox',
'image',
];
return lazy_field_formatters();
}
/**
......@@ -137,7 +134,7 @@ function lazy_preprocess_image(&$variables) {
// We don't need `data-lazy` attribute anymore.
unset($variables['attributes']['data-lazy']);
$config = \Drupal::config('lazy.settings')->get();
if ($config && !\Drupal::service('lazy')->isPathDisabled($config['disabled_paths'])) {
if ($config && \Drupal::service('lazy')->isPathAllowed($config['disabled_paths'])) {
// Required attribute for enabling native lazy-loading.
$variables['attributes']['loading'] = 'lazy';
// Set the selector class.
......@@ -166,7 +163,7 @@ function lazy_preprocess_responsive_image(&$variables) {
if (array_key_exists('data-lazy', $variables['attributes'])) {
// The `data-lazy` attribute will be removed in `lazy_preprocess_image()`.
$config = \Drupal::config('lazy.settings')->get();
if ($config && !\Drupal::service('lazy')->isPathDisabled($config['disabled_paths'])) {
if ($config && \Drupal::service('lazy')->isPathAllowed($config['disabled_paths'])) {
foreach ($variables['sources'] as $source) {
if (isset($source['srcset'])) {
$source[$config['lazysizes']['srcsetAttr']] = $source['srcset'];
......@@ -183,7 +180,7 @@ function lazy_preprocess_responsive_image(&$variables) {
function lazy_page_attachments(array &$attachments) {
$config = \Drupal::config('lazy.settings')->get();
$lazy = \Drupal::service('lazy');
if ($config && !$lazy->isPathDisabled($config['disabled_paths'])) {
if ($config && $lazy->isPathAllowed($config['disabled_paths'])) {
$plugins = $lazy->getPlugins();
$config['lazysizes']['plugins'] = array_intersect_key($plugins, $config['lazysizes']['plugins']);
$options = [
......@@ -226,15 +223,15 @@ function lazy_is_enabled() {
* Checks whether lazy-load is disabled for the current path.
*
* @param string $disabled_paths
* List of paths Lazy should be disabled.
* List of paths whether Lazy should be enabled.
*
* @return bool
* Whether Lazy is disabled for the requested path.
* Whether Lazy is allowed for the requested path.
*
* @deprecated in lazy:8.3.x and will be removed from lazy:8.3.1.
* Use \Drupal::service('lazy')->isPathDisabled($disabled_paths)
* Use \Drupal::service('lazy')->isPathAllowed($disabled_paths)
*/
function lazy_disabled_by_path($disabled_paths) {
@trigger_error('lazy_field_types() is deprecated in lazy:8.3.x and will be removed from lazy:8.3.1. Use \Drupal::service(\'lazy\')->isPathDisabled();', E_USER_DEPRECATED);
return \Drupal::service('lazy')->isPathDisabled($disabled_paths);
@trigger_error('lazy_field_types() is deprecated in lazy:8.3.x and will be removed from lazy:8.3.1. Use \Drupal::service(\'lazy\')->isPathAllowed($disabled_paths);', E_USER_DEPRECATED);
return \Drupal::service('lazy')->isPathAllowed($disabled_paths);
}
......@@ -23,7 +23,7 @@ class LazyForm extends ConfigFormBase {
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['lazy.settings'];
return ['lazy.settings', 'image.settings'];
}
/**
......@@ -91,11 +91,44 @@ class LazyForm extends ConfigFormBase {
$image_fields = is_array($config->get('image_fields')) ? $config->get('image_fields') : [];
$this->getEnabledFiltersAndFields($image_fields);
$form['preview'] = [
'#type' => 'details',
'#title' => $this->t('Preview'),
'#open' => FALSE,
];
$form['preview']['spacer'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this->t('Following empty space is intentional. Scroll down for an image preview.'),
'#attributes' => [
'style' => 'height: 3000px;'
],
];
$form['preview']['image'] = [
'#theme' => 'image',
'#uri' => $this->config('image.settings')->get('preview_image'),
'#alt' => t('Preview image'),
'#title' => 'Preview image',
'#attributes' => [
'width' => 480,
'height' => 360,
'style' => 'width: 480px; height: 360px;',
'data-lazy' => TRUE,
],
];
$form['settings'] = [
'#type' => 'fieldset',
'#type' => 'vertical_tabs',
'#title' => $this->t('Settings'),
'#parents' => ['lazy_tabs'],
];
$form['settings']['alter_tag'] = [
$form['text_format'] = [
'#type' => 'details',
'#title' => $this->t('Text-formats only'),
'#group' => 'lazy_tabs',
];
$form['text_format']['alter_tag'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Select the inline elements to be lazy-loaded via filter.'),
'#description' => $this->t('Only selected tags will be lazy-loaded in activated text-formats.'),
......@@ -105,7 +138,13 @@ class LazyForm extends ConfigFormBase {
],
'#default_value' => $config->get('alter_tag'),
];
$form['settings']['skipClass'] = [
$form['shared'] = [
'#type' => 'details',
'#title' => $this->t('Shared settings'),
'#group' => 'lazy_tabs',
];
$form['shared']['skipClass'] = [
'#type' => 'textfield',
'#title' => $this->t('skipClass'),
'#description' => $this->t('Elements having this class name will be ignored.'),
......@@ -113,7 +152,7 @@ class LazyForm extends ConfigFormBase {
'#size' => 20,
'#required' => TRUE,
];
$form['settings']['placeholderSrc'] = [
$form['shared']['placeholderSrc'] = [
'#type' => 'textfield',
'#title' => $this->t('Placeholder image URL'),
'#description' => $this->t('Suggestion: 1x1 pixels transparent GIF: <code>:code</code>. <em>Notice: This feature will be deprecated soon.</em>', [
......@@ -124,21 +163,22 @@ class LazyForm extends ConfigFormBase {
'#maxlength' => 255,
'#required' => FALSE,
];
$form['settings']['paths'] = [
$form['request_path'] = [
'#type' => 'details',
'#title' => $this->t('Disabled pages'),
'#description' => $this->t('Lazy-loading is disabled for both <em>image fields</em> and <em>inline images/iframes</em> on following pages.'),
'#open' => FALSE,
'#title' => $this->t('Visibility'),
'#description' => $this->t('This configuration applies to both <em>image fields</em> and <em>inline images/iframes</em> on following pages.'),
'#group' => 'lazy_tabs',
];
$form['settings']['paths']['disabled_paths'] = array(
$form['request_path']['disabled_paths'] = [
'#type' => 'textarea',
'#title' => $this->t('Pages'),
'#title' => $this->t('Disabled paths'),
'#default_value' => $config->get('disabled_paths'),
'#description' => $this->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. An example path is %user-wildcard for every user page. %front is the front page.", [
'#description' => $this->t("Specify on what pages should lazy-loading disabled. Enter one path per line. The '*' character is a wildcard. An example path is %user-wildcard for every user page. %front is the front page.", [
'%user-wildcard' => '/user/*',
'%front' => '<front>',
]),
);
];
$form['lazysizes'] = [
'#type' => 'details',
......@@ -150,7 +190,7 @@ class LazyForm extends ConfigFormBase {
':examples' => 'http://afarkas.github.io/lazysizes/#examples',
]
),
'#open' => FALSE,
'#group' => 'lazy_tabs',
];
$form['lazysizes']['lazysizes_lazyClass'] = [
'#type' => 'textfield',
......
......@@ -9,7 +9,7 @@ use Drupal\Core\Path\PathMatcherInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class IsEnabled.
* Class Lazy.
*/
class Lazy implements LazyInterface {
......@@ -42,7 +42,7 @@ class Lazy implements LazyInterface {
protected $pathMatcher;
/**
* IsEnabled constructor.
* Lazy constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Core\Path\CurrentPathStack $path_current
......@@ -56,24 +56,10 @@ class Lazy implements LazyInterface {
$this->pathMatcher = $path_matcher;
}
// /**
// * {@inheritdoc}
// */
// public static function create(ContainerInterface $container) {
// return new static(
// $container->get('config.factory'),
// $container->get('path.current'),
// $container->get('path.alias_manager'),
// $container->get('path.matcher')
// );
// }
/**
* List of available plugins.
*
* @return array
* {@inheritdoc}
*/
public function getPlugins() {
public function getPlugins(): array {
return [
'artdirect' => 'artdirect/ls.artdirect',
'aspectratio' => 'aspectratio/ls.aspectratio',
......@@ -160,25 +146,13 @@ class Lazy implements LazyInterface {
/**
* {@inheritdoc}
*/
public function isPathDisabled($disabled_paths): bool {
if (empty($disabled_paths)) {
return FALSE;
}
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$disabled_paths = mb_strtolower($disabled_paths);
// Current path. Do not trim a trailing slash if that is the complete path.
public function isPathAllowed($disabled_paths): bool {
$current_path = $this->pathCurrent->getPath();
$current_path = $current_path === '/' ? $current_path : rtrim($current_path, '/');
// Path alias.
$current_path_matcher = $this->pathMatcher->matchPath($current_path, $disabled_paths);
$path_alias = $this->pathAliasManager->getAliasByPath($current_path);
$path_alias = mb_strtolower($path_alias);
// If either returns TRUE, Lazy should be disabled for this path.
$path_matcher = $this->pathMatcher->matchPath($current_path, $disabled_paths);
$path_alias_matcher = $this->pathMatcher->matchPath($path_alias, $disabled_paths);
return ($path_alias_matcher || $path_matcher);
return !($current_path_matcher || $path_alias_matcher);
}
}
......@@ -7,6 +7,13 @@ namespace Drupal\lazy;
*/
interface LazyInterface {
/**
* List of available plugins.
*
* @return array
*/
public function getPlugins():array ;
/**
* Is Lazy enabled.
*
......@@ -39,6 +46,6 @@ interface LazyInterface {
* @return bool
* Whether Lazy is disabled for the requested path.
*/
public function isPathDisabled($disabled_paths): bool;
public function isPathAllowed($disabled_paths): bool;
}
......@@ -32,9 +32,9 @@ class LazyFilter extends FilterBase {
$result = new FilterProcessResult($text);
$html_dom = Html::load($text);
$path_matches = \Drupal::service('lazy')->isPathDisabled($config['disabled_paths']);
$path_allowed = \Drupal::service('lazy')->isPathAllowed($config['disabled_paths']);
if (!$path_matches) {
if ($path_allowed) {
foreach ($opt_tags as $tag => $status) {
$matches = $html_dom->getElementsByTagName($tag);
foreach ($matches as $element) {
......
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