Commit 0056b1ac authored by osman's avatar osman

Added `Disable lazy-load by path` feature.

parent bb4c1f87
errorClass: 'b-error'
errorClass: "b-error"
loadInvisible: false
offset: 100
saveViewportOffsetDelay: 50
selector: 'b-lazy'
selector: "b-lazy"
alter_tag:
img: 0
iframe: 0
skipClass: 'no-b-lazy'
src: 'data-src'
successClass: 'b-loaded'
skipClass: "no-b-lazy"
src: "data-src"
successClass: "b-loaded"
validateDelay: 25
placeholderSrc: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
placeholderSrc: "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
image_fields: false
disabled_paths:
- "/rss.xml"
\ No newline at end of file
......@@ -5,8 +5,9 @@
* Module file for Lazy-load.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
......@@ -35,9 +36,13 @@ function lazy_help($route_name, RouteMatchInterface $route_match) {
* Implements template_preprocess_field().
*/
function lazy_preprocess_field(&$variables) {
$config = \Drupal::config('lazy.settings')->get();
$pages = $config['disabled_paths'];
$path_matches = lazy_disabled_by_path($pages);
$element = $variables['element'];
if ($element['#field_type'] === 'image') {
if (!$path_matches && $element['#field_type'] === 'image') {
$entity = $element['#object'];
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
......@@ -118,8 +123,8 @@ function lazy_is_enabled() {
}
}
$config = \Drupal::config('lazy.settings');
$image_fields = $config->get('image_fields');
$config = \Drupal::config('lazy.settings')->get();
$image_fields = $config['image_fields'];
if (count($image_fields)) {
foreach ($image_fields as $field_name => $bool_value) {
if ($bool_value) {
......@@ -128,7 +133,7 @@ function lazy_is_enabled() {
}
}
return count($status) ? $config->get() : FALSE;
return count($status) ? $config : FALSE;
}
/**
......@@ -160,7 +165,7 @@ function lazy_field_formatter_third_party_settings_form($plugin, $field_definiti
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*/
function lazy_form_entity_view_display_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
function lazy_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$image_fields = [];
......@@ -218,4 +223,32 @@ function lazy_settings_update(array $image_fields) {
\Drupal::service('config.factory')->getEditable('lazy.settings')
->set('image_fields', $fields)
->save();
}
\ No newline at end of file
}
/**
* Checks whether lazy-load is disabled for the current path.
*
* @param $disabled_paths
*
* @return bool
*/
function lazy_disabled_by_path($disabled_paths) {
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = mb_strtolower($disabled_paths);
if (!$pages) {
return TRUE;
}
// Compare the lowercase path alias (if any) and internal path.
$path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path.alias_manager')->getAliasByPath($path);
// Do not trim a trailing slash if that is the complete path.
$path = $path === '/' ? $path : rtrim($path, '/');
$path_alias = mb_strtolower($path_alias);
$path_matcher = \Drupal::service('path.matcher')->matchPath($path, $pages);
$path_alias_matcher = \Drupal::service('path.matcher')->matchPath($path_alias, $pages);
return $path_alias_matcher || (($path !== $path_alias) && $path_matcher);
}
......@@ -41,7 +41,7 @@ class LazyForm extends ConfigFormBase {
!(bool) $config->get('alter_tag')['img']
&& !(bool) $config->get('alter_tag')['iframe']
) {
drupal_set_message($this->t('Lazy-load is currently disabled. Update configuration in global settings to enable it.'), 'warning');
$this->messenger()->addStatus($this->t('Lazy-load is currently disabled. Update configuration in global settings to enable it.'), 'warning');
}
$form['settings'] = [
......@@ -79,6 +79,23 @@ class LazyForm extends ConfigFormBase {
'#required' => TRUE,
];
$form['paths'] = array(
'#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,
);
$form['paths']['disabled_paths'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#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.", [
'%user-wildcard' => '/user/*',
'%front' => '<front>',
]),
);
$form['blazy'] = [
'#type' => 'details',
'#title' => $this->t('bLazy configuration'),
......@@ -163,6 +180,11 @@ class LazyForm extends ConfigFormBase {
'#required' => TRUE,
];
$form['clear_cache'] = [
'#type' => 'checkbox',
'#title' => $this->t('Check the box to clear the cache'),
];
return parent::buildForm($form, $form_state);
}
......@@ -172,6 +194,7 @@ class LazyForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('lazy.settings')
->set('alter_tag', $form_state->getValue('alter_tag'))
->set('disabled_paths', $form_state->getValue('disabled_paths'))
->set('errorClass', $form_state->getValue('errorClass'))
->set('loadInvisible', (bool) $form_state->getValue('loadInvisible'))
->set('offset', (int) $form_state->getValue('offset'))
......@@ -184,6 +207,19 @@ class LazyForm extends ConfigFormBase {
->set('placeholderSrc', $form_state->getValue('placeholderSrc'))
->save();
parent::submitForm($form, $form_state);
if ($form_state->getValue('clear_cache')) {
$this->cacheClear();
}
}
/**
* Clears all caches, then redirects to the previous page.
*/
public function cacheClear() {
drupal_flush_all_caches();
$this->messenger()->addMessage('Cache cleared.');
// return $this->redirect('lazy.config_form');
}
}
......@@ -32,40 +32,45 @@ class LazyFilter extends FilterBase {
$result = new FilterProcessResult($text);
$html_dom = Html::load($text);
foreach ($opt_tags as $tag => $status) {
$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 (empty($opt_tags[$tag])) {
// If the `tag` is not enabled remove the bLazy selector class.
if (($key = array_search($opt_selector, $classes)) !== false) {
unset($classes[$key]);
$element->setAttribute('class', implode(' ', $classes));
if (empty($classes)) {
$element->removeAttribute('class');
$pages = $config['disabled_paths'];
$path_matches = lazy_disabled_by_path($pages);
if (!$path_matches) {
foreach ($opt_tags as $tag => $status) {
$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 (empty($opt_tags[$tag])) {
// If the `tag` is not enabled remove the bLazy selector class.
if (($key = array_search($opt_selector, $classes, FALSE)) !== FALSE) {
unset($classes[$key]);
$element->setAttribute('class', implode(' ', $classes));
if (empty($classes)) {
$element->removeAttribute('class');
}
}
}
}
else {
// `tag` is enabled. Make sure skipClass is not set before proceeding.
if (!in_array($opt_skipClass, $classes) && !in_array($opt_skipClass, $parent_classes)) {
$classes[] = $opt_selector;
$classes = array_unique($classes);
$element->setAttribute('class', implode(' ', $classes));
else {
// `tag` is enabled. Make sure skipClass is not set before proceeding.
if (!in_array($opt_skipClass, $classes, FALSE) && !in_array($opt_skipClass, $parent_classes, FALSE)) {
$classes[] = $opt_selector;
$classes = array_unique($classes);
$element->setAttribute('class', implode(' ', $classes));
$src = $element->getAttribute('src');
$element->removeAttribute('src');
$src = $element->getAttribute('src');
$element->removeAttribute('src');
$element->setAttribute($opt_src, $src);
$element->setAttribute('src', $opt_placeholderSrc);
$element->setAttribute($opt_src, $src);
$element->setAttribute('src', $opt_placeholderSrc);
}
}
}
}
}
$result->setProcessedText(Html::serialize($html_dom));
return $result;
......@@ -80,15 +85,15 @@ class LazyFilter extends FilterBase {
$tags = trim(str_replace(['0'], '', $tags));
$options = ['%img' => '<img>', '%iframe' => '<iframe>'];
switch ($tags) {
case 'img iframe':
return t('Both %img and %iframe elements are lazy-loaded.', $options);
case 'img':
return t('%img elements are lazy-loaded.', $options);
case 'iframe':
return t('%iframe elements are lazy-loaded.', $options);
case 'img iframe':
default:
return t('Both %img and %iframe elements are lazy-loaded.', $options);
}
}
......
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