You are here

function _google_webfont_loader_api_load_js_from_fontinfo in Webfont Loader 8

Same name and namespace in other branches
  1. 6 google_webfont_loader_api.module \_google_webfont_loader_api_load_js_from_fontinfo()
  2. 7 google_webfont_loader_api.module \_google_webfont_loader_api_load_js_from_fontinfo()

Parse a fontinfo description and create a javascript settings loader.

This is the real meat of the functionality as fonts need to get added.

1 call to _google_webfont_loader_api_load_js_from_fontinfo()
_google_webfont_loader_api_load_font in ./google_webfont_loader_api.module
Loads font into the webfont stack.

File

./google_webfont_loader_api.module, line 173
Google Webfont Loader API primary file.

Code

function _google_webfont_loader_api_load_js_from_fontinfo($font_info, $filepath) {
  $url = file_create_url($filepath);
  if (strpos(strrev($url), '/') !== 0) {
    $url .= '/';
  }
  $loader_js = array();

  // Load all the information from the font info file into the loader_js.
  // loader_js is a settings format recognized by the webfont loader.
  // You can specify your google fonts, typekit id, or custom fonts
  // (with their fontface css locations)
  foreach ($font_info as $key => $key_info) {
    $info = $key_info;
    if (!is_array($key_info)) {
      $info = array(
        $key_info,
      );
    }
    if ($key == 'google_families' && is_array($info)) {
      $loader_js['google'] = array(
        'families' => $info,
      );
    }
    if ($key == "typekit") {
      $loader_js['typekit'] = array(
        'id' => $info[0],
      );
    }
    if ($key == 'fontdeck') {
      $loader_js['fontdeck'] = array(
        'id' => $info[0],
      );
    }
    if ($key == 'monotype') {
      $loader_js['monotype'] = array(
        'projectId' => $info[0],
      );
    }

    // A custom style css must be specified if using custom families.
    if ($key == "custom_families" && is_array($font_info['custom_style_css'])) {
      $loader_js['custom'] = array(
        'families' => $info,
        'urls' => array(),
      );
      foreach ($font_info['custom_style_css'] as $custom_style) {
        if (\Drupal\Core\File\FileSystem::uriScheme($custom_style)) {
          $loader_js['custom']['urls'][] = file_create_url($custom_style);
        }
        else {
          $loader_js['custom']['urls'][] = $url . $custom_style;
        }
      }
    }
  }
  return _google_webfont_loader_api_add_js($loader_js);
}