Implements hook_page_alter().

Fichier

advagg_font/advagg_font.module, line 62

Code

function advagg_font_page_alter() {
    // Skip if advagg is disabled.
    if (!advagg_enabled()) {
        return;
    }
    $advagg_font_ffo = variable_get('advagg_font_fontfaceobserver', ADVAGG_FONT_FONTFACEOBSERVER);
    // Fontface Observer is disabled.
    if (empty($advagg_font_ffo)) {
        return;
    }
    // Add settings.
    drupal_add_js(array(
        'advagg_font' => array(),
        'advagg_font_storage' => variable_get('advagg_font_storage', ADVAGG_FONT_STORAGE),
        'advagg_font_cookie' => variable_get('advagg_font_cookie', ADVAGG_FONT_COOKIE),
        'advagg_font_no_fout' => variable_get('advagg_font_no_fout', ADVAGG_FONT_NO_FOUT),
    ), array(
        'type' => 'setting',
    ));
    // Add inline script for reading the cookies and adding the fonts already
    // loaded to the html class.
    if (variable_get('advagg_font_cookie', ADVAGG_FONT_COOKIE) || variable_get('advagg_font_storage', ADVAGG_FONT_STORAGE)) {
        $inline_script_min = 'for(var fonts=document.cookie.split("advaggf"),i=0;i<fonts.length;i++){var font=fonts[i].split("="),pos=font[0].indexOf("ont_");-1!==pos&&(window.document.documentElement.className+=" "+font[0].substr(4).replace(/[^a-zA-Z0-9\\-]/g,""))}if(void 0!==Storage){fonts=JSON.parse(localStorage.getItem("advagg_fonts"));var current_time=(new Date).getTime();for(var key in fonts)fonts[key]>=current_time&&(window.document.documentElement.className+=" "+key.replace(/[^a-zA-Z0-9\\-]/g,""))}';
        drupal_add_js($inline_script_min, array(
            'type' => 'inline',
            'group' => JS_LIBRARY - 1,
            'weight' => -50000,
            'scope' => 'above_css',
            'scope_lock' => TRUE,
            'movable' => FALSE,
            'no_defer' => TRUE,
        ));
    }
    // Get library data for fontfaceobserver.
    $library = advagg_get_library('fontfaceobserver', 'advagg_font');
    // If libraries_load() does not exist load library externally.
    if (!is_callable('libraries_load')) {
        $advagg_font_ffo = 6;
    }
    // Add fontfaceobserver.js.
    if ($advagg_font_ffo != 6 && empty($library['installed'])) {
        // The fontfaceobserver library is not installed; use external variant.
        $advagg_font_ffo = 6;
    }
    if ($advagg_font_ffo == 6) {
        // Use the external variant.
        foreach ($library['variants']['external']['files']['js'] as $data => $options) {
            drupal_add_js($data, $options);
        }
    }
    else {
        // Load the fontfaceobserver library.
        if ($advagg_font_ffo == 2) {
            // Use the inline variant.
            libraries_load('fontfaceobserver', 'inline');
        }
        else {
            libraries_load('fontfaceobserver');
        }
    }
    // Add advagg_font.js; sets cookie and changes the class of the top level
    // element once a font has been downloaded.
    $file_path = drupal_get_path('module', 'advagg_font') . '/advagg_font.js';
    drupal_add_js($file_path, array(
        'async' => TRUE,
        'defer' => TRUE,
    ));
}