You are here

function fontsquirrel_batch_import in @font-your-face 7.2

Batch processing function - import font variants.

1 string reference to 'fontsquirrel_batch_import'
fontsquirrel_fontyourface_import in modules/fontsquirrel/fontsquirrel.module
Implements hook_fontyourface_import().

File

modules/fontsquirrel/fontsquirrel.module, line 297

Code

function fontsquirrel_batch_import($families, &$context) {
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($families);
    $context['sandbox']['families'] = $families;
  }

  // Import variants of one family.
  $family = array_shift($context['sandbox']['families']);
  $variants_raw = drupal_http_request('http://www.fontsquirrel.com/api/familyinfo/' . $family);
  $variants = json_decode($variants_raw->data);
  foreach ($variants as $key => $variant) {
    $metadata = array(
      'id' => $variant->family_id,
      'path' => $variant->family_urlname,
      'font_filename' => $variant->filename,
    );
    $font = new StdClass();
    $font->name = $variant->family_name . ' ' . $variant->style_name;
    $font->url = 'http://www.fontsquirrel.com/fonts/' . $variant->family_urlname;

    // To make URL unique, add a fake anchor, except for the first variant.
    if ($key != 0) {
      $font->url .= '#' . drupal_strtolower($variant->fontface_name);
    }
    $font->css_family = $variant->fontface_name;
    $font->provider = 'fontsquirrel';
    $font->foundry = $variant->foundry_name;
    $font->foundry_url = 'http://www.fontsquirrel.com/foundry/' . $variant->foundry_urlname;
    $font->license = 'See Font Squirrel license page';
    $font->license_url = 'http://www.fontsquirrel.com/fonts/' . $variant->family_urlname . '#eula';
    $font->tags = array(
      $variant->classification,
    );
    $font->metadata = serialize($metadata);
    fontyourface_save_font($font);

    // Add a tag if this is a monocase font.
    if ($variant->is_monocase == 'Y') {
      fontyourface_add_tags_to_font(array(
        'monocase',
      ), $font->fid);
    }
  }

  // Update progress indicator.
  $context['sandbox']['progress']++;
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}