You are here

function lazyloader_page_build in Image Lazyloader 7

Implements hook_page_build().

Initialize the trigger for lazyloader.

File

./lazyloader.module, line 147
Lazyloader Module

Code

function lazyloader_page_build(&$vars) {
  if (_lazy_loader_enabled()) {

    // add noscript markup as a fallback
    $vars['page_bottom']['lazyload']['#markup'] = <<<'NOSCRIPT'
      <noscript>
        <style type="text/css" media="all">
          img[data-src] { display: none !important; }
        </style>
      </noscript>
NOSCRIPT;

    // inline load this js with ajax / page compatibility in mind
    $distance = trim(variable_get('lazyloader_distance', LAZYLOADER_DISTANCE));
    $icon = _lazyloader_icon_path();
    $lazyloader_init = <<<Script
      (function (\$) {
        Drupal.behaviors.lazyloader = {
          attach: function (context, settings) {
            \$("img[data-src]").lazyloader({distance: {<span class="php-variable">$distance</span>}, icon: "{<span class="php-variable">$icon</span>}" });
          }
        };
      }(jQuery));
Script;
    $vars['page_bottom']['lazyload']['#attached']['js'][] = array(
      'data' => $lazyloader_init,
      'type' => 'inline',
      'scope' => 'footer',
    );
    $settings = array(
      'lazyloader' => array(
        'loadImageDelay' => variable_get('lazyloader_load_image_delay', LAZYLOADER_LOAD_IMAGE_DELAY),
      ),
    );
    $vars['page_bottom']['lazyload']['#attached']['js'][] = array(
      'data' => $settings,
      'type' => 'setting',
    );
  }
}