array( 'title' => 'Lazy-load', 'description' => 'Configure how images and iframes are lazy-loaded.', 'page callback' => 'drupal_get_form', 'page arguments' => array('lazy_configuration_form'), 'file' => 'lazy.admin.inc', 'access arguments' => array('administer filters'), ), ); } /** * Implements hook_init(). */ function lazy_init() { $library = lazy_is_library_installed(); } /** * Implements hook_help(). */ function lazy_help($path, $arg) { switch ($path) { case 'admin/help#lazy': $filepath = dirname(__FILE__) . '/README.md'; if (file_exists($filepath)) { $readme = file_get_contents($filepath); } if (!isset($readme)) { return NULL; } if (module_exists('markdown')) { $filters = module_invoke('markdown', 'filter_info'); $info = $filters['filter_markdown']; if (function_exists($info['process callback'])) { $output = $info['process callback']($readme, NULL); } else { $output = '
' . $readme . '
'; } } else { $output = '
' . $readme . '
'; } return $output; } } /** * Implements hook_filter_info(). */ function lazy_filter_info() { $filters['lazy_filter'] = array( 'title' => t('Lazy-load images and IFRAMEs via bLazy'), 'default settings' => _filter_lazy_defaults(), 'process callback' => '_filter_lazy_process', 'cache' => TRUE, 'tips callback' => '_filter_lazy_tips', 'weight' => 20, ); return $filters; } /** * Callback to return configuration defaults. */ function _filter_lazy_defaults() { return array( 'lazy_filter_errorClass' => variable_get('lazy_filter_errorClass'), 'lazy_filter_loadInvisible' => variable_get('lazy_filter_loadInvisible'), 'lazy_filter_offset' => variable_get('lazy_filter_offset'), 'lazy_filter_saveViewportOffsetDelay' => variable_get('lazy_filter_saveViewportOffsetDelay'), 'lazy_filter_selector' => variable_get('lazy_filter_selector'), 'lazy_filter_alter_tag' => variable_get('lazy_filter_alter_tag'), 'lazy_filter_skipClass' => variable_get('lazy_filter_skipClass'), 'lazy_filter_src' => variable_get('lazy_filter_src'), 'lazy_filter_successClass' => variable_get('lazy_filter_successClass'), 'lazy_filter_validateDelay' => variable_get('lazy_filter_validateDelay'), 'lazy_filter_placeholderSrc' => variable_get('lazy_filter_placeholderSrc'), ); } /** * Implements callback_filter_process(). */ function _filter_lazy_process($text, $filter) { $opt_skipClass = variable_get('lazy_filter_skipClass'); $opt_selector = ltrim(variable_get('lazy_filter_selector'), '.'); $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'); $disabled_paths = preg_split("/\s+/", variable_get('lazy_disabled_paths')); if ($library_installed && !in_array(drupal_get_path_alias(), $disabled_paths)) { $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) : []; if (!in_array($opt_skipClass, $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); } } } $text = filter_dom_serialize($html_dom); } return trim($text); } /** * Implements callback_filter_tips(). */ function _filter_lazy_tips($filter, $format, $long = FALSE) { $tags = variable_get('lazy_filter_alter_tag'); $tags = implode(' ', $tags); $tags = trim(str_replace(array('0'), '', $tags)); switch ($tags) { case 'img iframe': return t('Both %img and %iframe elements are lazy-loaded.', array('%img' => '', '%iframe' => '