Commit c27fb006 authored by osman's avatar osman

Enable the module only when the library is installed.

parent 39c2e79c
# Lazy-load
This is a simple Drupal module to lazy-load all inline images and/or iframes
defined by content authors in entity content, usually the text-format-enabled
version of textarea fields. i.e. Node and Block body content.
......
......@@ -128,6 +128,15 @@ function lazy_configuration_form($form, $form_state) {
'#value' => t('Save configuration'),
);
// Notify the user if the library is not available, and disable the form.
$library_installed = variable_get('lazy_library_installed');
if (!$library_installed) {
drupal_set_message(t('The bLazy library could not be found.'), 'warning');
drupal_set_message(t('Follow the instructions in the README.md file or in the <a href="!url">help</a> page for installing the bLazy library.', array('!url' => url('admin/help/lazy'))), 'status');
$form['submit']['#attributes']['disabled'] = TRUE;
}
return $form;
}
......
......@@ -39,7 +39,7 @@ function lazy_enable() {
'%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);
$message = t('The <a href="!config">Lazy-load</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');
}
......@@ -60,6 +60,7 @@ function lazy_uninstall() {
variable_del('lazy_filter_validateDelay');
variable_del('lazy_filter_placeholderSrc');
variable_del('lazy_filter_image_fields');
variable_del('lazy_library_installed');
$module = 'lazy';
$name = 'lazy_filter';
......@@ -90,6 +91,8 @@ function lazy_requirements($phase) {
$requirements['blazy']['description'] = $blazy['error message'];
$requirements['blazy']['severity'] = REQUIREMENT_ERROR;
}
variable_set('lazy_library_installed', $blazy['installed']);
}
return $requirements;
......@@ -127,3 +130,11 @@ function lazy_update_7100() {
variable_set('lazy_filter_defaults', $defaults);
variable_set('lazy_filter_image_fields', $defaults['lazy_filter_image_fields']);
}
/**
* Disable module, if bLazy library is not installed yet.
*/
function lazy_update_7101() {
$blazy = libraries_detect('blazy');
variable_set('lazy_library_installed', $blazy['installed']);
}
......@@ -21,6 +21,13 @@ function lazy_menu() {
);
}
/**
* Implements hook_init().
*/
function lazy_init() {
$library = lazy_is_library_installed();
}
/**
* Implements hook_help().
*/
......@@ -102,29 +109,32 @@ function _filter_lazy_process($text, $filter) {
$opt_tags = variable_get('lazy_filter_alter_tag');
$opt_src = (variable_get('lazy_filter_src') !== 'src') ? variable_get('lazy_filter_src') : 'data-filterlazy-src';
$opt_placeholderSrc = variable_get('lazy_filter_placeholderSrc');
$library_installed = variable_get('lazy_library_installed');
$html_dom = filter_dom_load($text);
if ($library_installed) {
$html_dom = filter_dom_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) : [];
foreach ($opt_tags as $tag) {
$matches = $html_dom->getElementsByTagName($tag);
foreach ($matches as $element) {
$classes = $element->getAttribute('class');
$classes = (strlen($classes) > 0) ? explode(' ', $classes) : [];
if (!in_array($opt_skipClass, $classes)) {
$classes[] = $opt_selector;
$element->setAttribute('class', implode(' ', $classes));
if (!in_array($opt_skipClass, $classes)) {
$classes[] = $opt_selector;
$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);
}
}
}
}
$text = filter_dom_serialize($html_dom);
$text = filter_dom_serialize($html_dom);
}
return trim($text);
}
......@@ -163,37 +173,50 @@ function lazy_is_filter_enabled() {
return count($enabled_formats) ? TRUE : FALSE;
}
/**
* Is Blazy library installed.
*/
function lazy_is_library_installed() {
$blazy = libraries_detect('blazy');
variable_set('lazy_library_installed', $blazy['installed']);
return $blazy['installed'];
}
/**
* Implements hook_page_build().
*/
function lazy_page_build(&$page) {
$filter_enabled = lazy_is_filter_enabled();
$field_enabled = variable_get('lazy_filter_image_fields');
if ($filter_enabled || $field_enabled) {
$path = drupal_get_path('module', 'lazy');
$page['page_bottom']['lazy'] = array(
'#attached' => array(),
);
$attached = &$page['page_bottom']['lazy']['#attached'];
$settings = array(
'errorClass' => variable_get('lazy_filter_errorClass'),
'loadInvisible' => variable_get('lazy_filter_loadInvisible'),
'offset' => intval(variable_get('lazy_filter_offset')),
'saveViewportOffsetDelay' => intval(variable_get('lazy_filter_saveViewportOffsetDelay')),
'selector' => '.' . variable_get('lazy_filter_selector'),
'src' => variable_get('lazy_filter_src'),
'successClass' => variable_get('lazy_filter_successClass'),
'validateDelay' => intval(variable_get('lazy_filter_validateDelay')),
);
$attached['js'][] = array(
'data' => array('lazy' => array('bLazy' => $settings)),
'type' => 'setting',
);
$attached['libraries_load'][] = array('blazy');
$attached['js'][$path . '/lazy.js'] = array('every_page' => TRUE);
$library_installed = variable_get('lazy_library_installed');
if ($library_installed) {
if ($filter_enabled || $field_enabled) {
$path = drupal_get_path('module', 'lazy');
$page['page_bottom']['lazy'] = array(
'#attached' => array(),
);
$attached = &$page['page_bottom']['lazy']['#attached'];
$settings = array(
'errorClass' => variable_get('lazy_filter_errorClass'),
'loadInvisible' => variable_get('lazy_filter_loadInvisible'),
'offset' => intval(variable_get('lazy_filter_offset')),
'saveViewportOffsetDelay' => intval(variable_get('lazy_filter_saveViewportOffsetDelay')),
'selector' => '.' . variable_get('lazy_filter_selector'),
'src' => variable_get('lazy_filter_src'),
'successClass' => variable_get('lazy_filter_successClass'),
'validateDelay' => intval(variable_get('lazy_filter_validateDelay')),
);
$attached['js'][] = array(
'data' => array('lazy' => array('bLazy' => $settings)),
'type' => 'setting',
);
$attached['libraries_load'][] = array('blazy');
$attached['js'][$path . '/lazy.js'] = array('every_page' => TRUE);
}
}
}
......@@ -252,7 +275,7 @@ function lazy_library_get_version($library, $options) {
* Implements hook_theme_registry_alter().
*/
function lazy_theme_registry_alter(&$theme_registry) {
if (variable_get('lazy_filter_image_fields') && isset($theme_registry['image'])) {
if (variable_get('lazy_library_installed') && variable_get('lazy_filter_image_fields') && isset($theme_registry['image'])) {
$theme_registry['image']['function'] = 'theme_lazy_image';
}
}
......@@ -267,7 +290,7 @@ function theme_lazy_image($variables) {
$opt_placeholderSrc = variable_get('lazy_filter_placeholderSrc');
// Skip Blazy rendering if variables contain `.no-b-lazy` CSS class name.
if (!empty($variables['attributes']['class']) && in_array($opt_skipClass, $variables['attributes']['class'])) {
if (!variable_get('lazy_library_installed') || (!empty($variables['attributes']['class']) && in_array($opt_skipClass, $variables['attributes']['class']))) {
return theme('image', $variables);
}
else {
......
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