You are here

function fontscom_api_fontyourface_import in @font-your-face 8.3

Implements hook_fontyourface_import().

File

modules/fontscom_api/fontscom_api.module, line 209
Fonts.com API module file.

Code

function fontscom_api_fontyourface_import($font_context = []) {
  $context = $font_context;
  $limit = 50;
  $config = \Drupal::config('fontscom_api.settings');

  // Only get all fonts when we have set a project and token.
  if (!empty($config
    ->get('token')) && !empty($config
    ->get('project'))) {
    if (empty($context['sandbox'])) {
      $context['sandbox']['progress'] = 1;
      $context['sandbox']['font_count'] = 0;
      $context['sandbox']['max'] = ceil(fontscom_api_get_all_remote_fonts_count() / $limit);
      $context['sandbox']['enabled_fonts'] = fontscom_api_get_all_enabled_fonts();
    }
    $fontscom_fonts = fontscom_api_get_all_fonts($context['sandbox']['progress'], $limit);
    foreach ($fontscom_fonts as $fontscom_font) {
      _fontscom_api_parse_imported_font($fontscom_font);
      $font_data = new stdClass();
      $font_data->metadata = [
        'FontID' => $fontscom_font->FontID,
        'eot' => $fontscom_font->EOTURL,
        'svg' => $fontscom_font->SVGURL,
        'ttf' => $fontscom_font->TTFURL,
        'woff2' => $fontscom_font->WOFF2URL,
        'woff' => $fontscom_font->WOFFURL,
      ];
      $font_data->provider = 'fontscom_api';
      $font_data->name = $fontscom_font->name;
      $font_data->url = 'https://www.fonts.com/fonts/' . $fontscom_font->FontID;
      $font_data->css_family = $fontscom_font->FontCSSName;
      $font_data->language = [
        $fontscom_font->FontLanguage,
      ];
      $font_data->designer = $fontscom_font->Designer;
      $font_data->foundry = $fontscom_font->FontFoundryName;
      if (!empty($fontscom_font->Classification)) {
        $font_data->classification = explode(',', $fontscom_font->Classification);
      }
      $font = fontyourface_save_font($font_data);
      if (isset($context['sandbox']['enabled_fonts'][$fontscom_font->FontID])) {
        $font
          ->activate();
      }
      $context['sandbox']['font_count']++;
    }
    $context['message'] = "Working on batch {$context['sandbox']['progress']} of {$context['sandbox']['max']}";
    $context['sandbox']['progress']++;
    if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
    else {
      Drupal::messenger()
        ->addMessage(t('Imported @count fonts from fonts.com', [
        '@count' => $context['sandbox']['font_count'],
      ]));
    }
  }
  else {
    Drupal::messenger()
      ->addMessage(t('Due to the number of fonts, automated import from install for Fonts.com is disabled. Please enter your Authentication Key and Project ID first before trying to import fonts.'));
  }
  return $context;
}