You are here

function google_webfont_loader_api_init in Webfont Loader 6

Same name and namespace in other branches
  1. 7 google_webfont_loader_api.module \google_webfont_loader_api_init()

Implements hook_init().

File

./google_webfont_loader_api.module, line 33
Google Webfont Loader API primary file The designer/developer creates a set of packages (will use .fontinfo files created in a similar manner to a module or theme .info file) from which the site admin can then choose for their site. The fonts can…

Code

function google_webfont_loader_api_init() {
  global $base_path;

  // First deal with the webfont.js file.
  $webfont_url = "http://ajax.googleapis.com/ajax/libs/webfont/1.5.0/webfont.js";
  if (variable_get('google_webfont_loader_api_cache', 0) && variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC && ($source = _google_webfont_loader_api_cache($webfont_url))) {
    drupal_add_js($source, 'module');

    // Add a drupal setting.
    drupal_add_js(array(
      'google_webfont_loader_api_preloaded' => 1,
    ), 'setting');
  }
  else {
    drupal_add_js(array(
      'google_webfont_loader_api_preloaded' => 0,
    ), 'setting');
  }

  // Retrieve the font list and the font the user chose.
  $font_list = google_webfont_loader_api_get_font_list();
  $user_chosen_font = variable_get('google_webfont_loader_api_font', '');
  if (array_key_exists($user_chosen_font, $font_list)) {
    $font = $font_list[$user_chosen_font];
    $filepath = str_replace('//', '/', dirname($font->filename));
    $font_info = $font->info;
    $loader_js = _google_webfont_loader_api_load_js_from_fontinfo($font_info, $filepath);

    // If there are items in the loader_js, create a setting and load the render
    // css which is useless unless correct information regarding fonts are
    // actually loaded.
    if (count($loader_js) > 0) {
      $loader_js = array(
        'google_webfont_loader_api_setting' => $loader_js,
      );
      drupal_add_js($loader_js, 'setting');
      drupal_add_js(drupal_get_path('module', 'google_webfont_loader_api') . '/google_webfont_loader_api.js');
      if (variable_get('google_webfont_loader_api_display_style', '') == 'hidden') {
        _google_webfont_loader_api_add_hidden_css();
      }

      // Start loading the css that will be rendered.
      if (isset($font_info['render_css'])) {
        if (is_array($font_info['render_css'])) {
          foreach ($font_info['render_css'] as $render_css) {
            drupal_add_css($filepath . '/' . $render_css);
          }
        }
        else {
          drupal_add_css($filepath . '/' . $font_info['render_css']);
        }
      }
    }
  }
}