You are here

function fontsquirrel_api_fontyourface_import in @font-your-face 8.3

Implements hook_fontyourface_import().

File

modules/fontsquirrel_api/fontsquirrel_api.module, line 151
Font Squirrel API module file.

Code

function fontsquirrel_api_fontyourface_import($font_context = []) {

  // Individual font could take a lot of time.
  @set_time_limit(3600);
  $context = $font_context;
  if (empty($context['sandbox'])) {
    $context['sandbox']['fonts'] = _fontsquirrel_api_get_fonts_from_api();
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($context['sandbox']['fonts']);
  }
  $font_import = array_pop($context['sandbox']['fonts']);
  if (!empty($font_import)) {
    $metadata = [
      'id' => $font_import->id,
      'path' => $font_import->family_urlname,
      'font_filename' => $font_import->font_filename,
    ];
    $font = new stdClass();
    $font->name = $font_import->family_name;
    $font->url = 'https://www.fontsquirrel.com/fonts/' . $font_import->family_urlname;
    $font->provider = 'fontsquirrel_api';
    $font->css_family = $font_import->family_urlname . 'regular';

    // Font Squirrel fonts have no concept of normal/bold/light/italics fonts.
    $font->css_weight = 400;
    $font->css_style = 'normal';
    $font->foundry = $font_import->foundry_name;
    $font->foundry_url = 'https://www.fontsquirrel.com/foundry/' . $font_import->foundry_urlname;
    $font->license = 'See Font Squirrel license page';
    $font->license_url = $font->url . '#eula';
    $font->classification = [
      str_replace(' ', '-', strtolower($font_import->classification)),
    ];
    $font->language = [
      'English',
    ];
    $font->metadata = $metadata;
    $variants = fontsquirrel_api_get_font_family_info($font_import);
    foreach ($variants as $variant) {
      $variant_metadata = $metadata;
      $variant_metadata['font_filename'] = $variant->filename;
      $variant_metadata['checksum'] = $variant->checksum;
      $variant_metadata['preview_image'] = str_replace('\\', '', $variant->sample_image);
      $variant_font = clone $font;
      $variant_font->name = $variant->family_name . ' ' . $variant->style_name;
      $variant_font->url = $font->url . '#' . $variant->fontface_name;
      $variant_font->css_family = $variant->fontface_name;
      $variant_font->metadata = $variant_metadata;
      fontyourface_save_font($variant_font);
    }
    $context['message'] = "Imported {$context['sandbox']['progress']} (plus variants) of {$context['sandbox']['max']}";
    $context['sandbox']['progress'] = $context['sandbox']['max'] - count($context['sandbox']['fonts']);
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
  else {
    Drupal::messenger()
      ->addMessage(t('Imported @count Font Squirrel fonts (and variants).', [
      '@count' => $context['sandbox']['max'],
    ]));
  }
  return $context;
}