5 * Fonts.com API module file.
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Component\Utility\Html;
11 use Drupal\Component\Utility\UrlHelper;
14 use Drupal\fontyourface\FontInterface;
15 use Drupal\fontyourface\FontDisplayInterface;
16 use Drupal\fontyourface\Entity\Font;
18 define('FONTSCOM_API_BASE_URL', 'https://api.fonts.com');
19 define('FONTSCOM_API_APP_KEY', '1fdb130c-d5c0-4fab-8e2b-271508570323932606');
22 * Implements hook_fontyourface_api().
24 function fontscom_api_fontyourface_api() {
27 'name' => 'Fonts.com',
32 * Implements hook_entity_presave().
34 function fontscom_api_entity_presave(EntityInterface $entity) {
35 if ($entity instanceof Font) {
36 if ($entity->pid->value == 'fontscom_api' && $entity->isEnabled()) {
37 $metadata = $entity->getMetadata();
38 $config = \Drupal::config('fontscom_api.settings');
39 $enabled_fonts = fontscom_api_get_all_enabled_fonts();
40 if (isset($enabled_fonts[$metadata['FontID']])) {
44 fontscom_api_add_font_to_current_project($metadata['FontID']);
47 elseif ($entity->pid->value == 'fontscom_api' && $entity->isDisabled()) {
48 if (!empty($entity->original)) {
49 $original_entity = $entity->original;
50 if ($original_entity->status->value != $entity->status->value) {
51 $metadata = $entity->getMetadata();
52 $config = \Drupal::config('fontscom_api.settings');
53 $enabled_fonts = fontscom_api_get_all_enabled_fonts();
54 if (isset($enabled_fonts[$metadata['FontID']])) {
55 fontscom_api_remove_font_from_current_project($metadata['FontID']);
64 * Implements hook_form_alter().
66 function fontscom_api_form_font_settings_alter(&$form, FormStateInterface $form_state) {
67 $config = \Drupal::config('fontscom_api.settings');
68 $form['fontscom_api'] = [
69 '#type' => 'fieldset',
70 '#title' => t('FONTS.COM SETTINGS'),
72 $form['fontscom_api']['fontscom_api_token'] = [
73 '#type' => 'textfield',
74 '#title' => t('Fonts.com Authentication Key'),
75 '#description' => t('Add your Fonts.com authentication key to import your projects. Available at <a target="_blank" href=":url">:url</a>', [':url' => 'https://www.fonts.com/account#authentification-section']),
76 '#default_value' => $config->get('token'),
79 if (!empty($config->get('token'))) {
80 $projects = fontscom_api_get_projects();
82 if (count($projects) > 0) {
83 $options = ['' => '-- Select a project --'];
84 foreach ($projects as $key => $project) {
85 $options[$project->ProjectKey] = Html::escape($project->ProjectName);
87 $form['fontscom_api']['fontscom_api_project'] = [
89 '#title' => t('Project'),
90 '#options' => $options,
91 '#default_value' => $config->get('project'),
96 $form['#submit'][] = 'fontscom_api_form_font_settings_submit';
100 * Submits Font settings form data.
102 function fontscom_api_form_font_settings_submit(&$form, FormStateInterface $form_state) {
103 $values = $form_state->getValues();
104 $config = \Drupal::configFactory()->getEditable('fontscom_api.settings');
105 $config->set('token', $values['fontscom_api_token']);
106 if (isset($values['fontscom_api_project'])) {
107 $config->set('project', $values['fontscom_api_project']);
110 fontscom_api_get_allowed_api_filters(TRUE);
111 fontscom_api_get_all_remote_fonts_count(TRUE);
112 drupal_set_message(t('Saved Fonts.com Authentication Key'));
116 * Implements hook_page_attachments().
118 function fontscom_api_page_attachments(&$page) {
119 $config = \Drupal::config('fontscom_api.settings');
120 // Only get all fonts when we have set a project and token.
121 if (!empty($config->get('token')) && !empty($config->get('project'))) {
122 $page['#attached']['html_head'][] = [
124 '#type' => 'html_tag',
127 'src' => 'https://fast.fonts.net/jsapi/' . $config->get('project') . '.js',
129 ], 'fontyourface-fontscom-api-' . $config->get('project'),
133 $enabled_fonts = &drupal_static('fontyourface_fonts', []);
135 foreach ($enabled_fonts as $font) {
136 if ($font->pid->value == 'fontscom_api') {
137 if ($font->isDisabled()) {
138 $font_css .= _fontscom_api_generate_font_css($font);
142 if (!empty($font_css)) {
143 $hash = hash('sha256', $font_css);
144 $directory_location = 'fontyourface/fontscom_api';
145 file_prepare_directory($directory_location, FILE_CREATE_DIRECTORY);
146 if (!file_exists($directory_location . '/fontyourface-stylesheet-' . $hash . '.css')) {
147 file_unmanaged_save_data($font_css, $directory_location . '/fontyourface-stylesheet-' . $hash . '.css', FILE_EXISTS_REPLACE);
149 $page['#attached']['html_head'][] = [
151 '#type' => 'html_tag',
154 'rel' => 'stylesheet',
155 'href' => file_create_url($directory_location . '/fontyourface-stylesheet-' . $hash . '.css'),
158 ], 'fontyourface-fontscom-api-preview-fonts',
164 * Implements hook_fontyourface_font_css().
166 function fontscom_api_fontyourface_font_css(FontInterface $font, FontDisplayInterface $font_style = NULL, $separator = ' ') {
167 if ($font->pid->value == 'fontscom_api') {
170 // Enclose font family definition in single quotes if not already enclosed.
171 if ($font->css_family->value[0] === "'") {
172 $family_list = $font->css_family->value;
175 $family_list = "'" . $font->css_family->value . "'";
178 if ($font_style !== NULL) {
179 if ($font_style->css_fallbacks) {
180 $family_list .= ', ' . $font_style->css_fallbacks;
184 $css[] = 'font-family: ' . $family_list . ';';
185 $css[] = 'font-style: 400;';
186 $css[] = 'font-weight: normal;';
188 return implode($separator, $css);
193 * Implements hook_fontyourface_import().
195 function fontscom_api_fontyourface_import($font_context = []) {
196 $context = $font_context;
198 $config = \Drupal::config('fontscom_api.settings');
199 // Only get all fonts when we have set a project and token.
200 if (!empty($config->get('token')) && !empty($config->get('project'))) {
201 if (empty($context['sandbox'])) {
202 $context['sandbox']['progress'] = 1;
203 $context['sandbox']['font_count'] = 0;
204 $context['sandbox']['max'] = ceil(fontscom_api_get_all_remote_fonts_count() / $limit);
205 $context['sandbox']['enabled_fonts'] = fontscom_api_get_all_enabled_fonts();
207 $fontscom_fonts = fontscom_api_get_all_fonts($context['sandbox']['progress'], $limit);
208 foreach ($fontscom_fonts as $fontscom_font) {
209 _fontscom_api_parse_imported_font($fontscom_font);
210 $font_data = new stdClass();
211 $font_data->metadata = [
212 'FontID' => $fontscom_font->FontID,
213 'eot' => $fontscom_font->EOTURL,
214 'svg' => $fontscom_font->SVGURL,
215 'ttf' => $fontscom_font->TTFURL,
216 'woff2' => $fontscom_font->WOFF2URL,
217 'woff' => $fontscom_font->WOFFURL,
219 $font_data->provider = 'fontscom_api';
220 $font_data->name = $fontscom_font->name;
221 $font_data->url = 'https://www.fonts.com/fonts/' . $fontscom_font->FontID;
222 $font_data->css_family = $fontscom_font->FontCSSName;
223 $font_data->language = [
224 $fontscom_font->FontLanguage,
226 $font_data->designer = $fontscom_font->Designer;
227 $font_data->foundry = $fontscom_font->FontFoundryName;
228 if (!empty($fontscom_font->Classification)) {
229 $font_data->classification = explode(',', $fontscom_font->Classification);
231 $font = fontyourface_save_font($font_data);
232 if (isset($context['sandbox']['enabled_fonts'][$fontscom_font->FontID])) {
235 $context['sandbox']['font_count']++;
237 $context['message'] = "Working on batch {$context['sandbox']['progress']} of {$context['sandbox']['max']}";
238 $context['sandbox']['progress']++;
239 if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
240 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
243 drupal_set_message(t('Imported @count fonts from fonts.com', ['@count' => $context['sandbox']['font_count']]));
247 drupal_set_message(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.'));
253 * Provides headers with api parameters.
255 * @param string $path
256 * Fonts.com API endpoint.
259 * Header with fonts.com token for API request.
261 function fontscom_api_headers($path) {
262 $config = \Drupal::config('fontscom_api.settings');
264 $fontscom_token = $config->get('token');
266 if (empty($fontscom_token)) {
270 list($public_key, $private_key) = explode('--', $fontscom_token);
272 $encoded = base64_encode(hash_hmac('md5', $public_key . '|' . $path, $private_key, TRUE));
273 $auth = urlencode($public_key . ':' . $encoded);
275 return ['Authorization' => $auth, 'AppKey' => FONTSCOM_API_APP_KEY];
280 * Returns list of projects.
282 function fontscom_api_get_projects() {
285 $path = '/rest/json/Projects/?wfspstart=0&wfsplimit=100';
286 $uri = FONTSCOM_API_BASE_URL . $path;
287 $response = \Drupal::httpClient()->get($uri, ['headers' => fontscom_api_headers($path), 'verify' => FALSE]);
288 $data = json_decode((string) $response->getBody());
290 catch (Exception $e) {
291 drupal_set_message(t('There was an error retrieving project list from Fonts.com. Error: %error', ['%error' => $e->getMessage()]), 'error');
295 if ($data->Projects->TotalRecords > 0) {
296 $project = $data->Projects->Project;
297 $projects = fontscom_api_unknown_to_array($project);
303 * Returns an array, regardless of input.
305 * @param mixed $unknown
306 * A parameter of unknown type.
309 * If parameter is already an array, return as-is. Otherwise, return array
310 * with param as first value.
312 function fontscom_api_unknown_to_array($unknown) {
313 if (is_array($unknown)) {
321 * Gets a list of all fonts, in given range.
324 * Pager request start value.
326 * Pager request limit. Max 50.
329 * Array of fonts.com font objects.
331 function fontscom_api_get_all_fonts($start = 0, $limit = 50) {
338 'wfspstart' => $start,
339 'wfsplimit' => $limit,
343 $filters = fontscom_api_get_allowed_api_filters();
344 if ($filters->FreeOrPaid == 0) {
345 $query['wfsfree'] = 'true';
349 $path = '/rest/json/AllFonts/?' . UrlHelper::buildQuery($query);
350 $uri = FONTSCOM_API_BASE_URL . $path;
351 $response = \Drupal::httpClient()->get($uri, ['headers' => fontscom_api_headers($path), 'verify' => FALSE]);
352 $data = json_decode((string) $response->getBody());
354 catch (Exception $e) {
355 drupal_set_message(t('There was an error importing fonts from Fonts.com. Error: %error', ['%error' => $e->getMessage()]), 'error');
358 return $data->AllFonts->Font;
362 * Gets total font count.
365 * If the cache should be flushed and force an API request.
368 * Total number of fonts on fonts.com.
370 function fontscom_api_get_all_remote_fonts_count($reset = FALSE) {
372 if (!$reset && $cache = \Drupal::cache()->get('fontscom_api_remote_fonts_count')) {
376 $filters = fontscom_api_get_allowed_api_filters();
377 $path = '/rest/json/AllFonts/?wfspstart=0&wfsplimit=1';
378 if ($filters->FreeOrPaid == 0) {
379 $path .= '&wfsfree=true';
381 $uri = FONTSCOM_API_BASE_URL . $path;
382 $response = \Drupal::httpClient()->get($uri, ['headers' => fontscom_api_headers($path), 'verify' => FALSE]);
383 $data = json_decode((string) $response->getBody());
385 catch (Exception $e) {
386 drupal_set_message(t('There was an error retrieving total Font count from Fonts.com. Error: %error', ['%error' => $e->getMessage()]), 'error');
389 \Drupal::cache()->set('fontscom_api_remote_fonts_count', $data->AllFonts->TotalRecords);
390 return $data->AllFonts->TotalRecords;
394 * Retrieves list of allowed api filters.
397 * If the cache should be flushed and force an API request.
400 * Key-value store of allowed filters from fonts.com.
402 function fontscom_api_get_allowed_api_filters($reset = FALSE) {
404 if (!$reset && $cache = \Drupal::cache()->get('fontscom_api_allowed_api_filters')) {
408 $path = '/rest/json/AllFilterValues/';
409 $uri = FONTSCOM_API_BASE_URL . $path;
410 $response = \Drupal::httpClient()->get($uri, ['headers' => fontscom_api_headers($path), 'verify' => FALSE]);
411 $data = json_decode((string) $response->getBody());
413 catch (Exception $e) {
414 drupal_set_message(t('There was an error retrieving Font filters from Fonts.com. Error: %error', ['%error' => $e->getMessage()]), 'error');
417 \Drupal::cache()->set('fontscom_api_allowed_api_filters', $data->FilterValues);
418 return $data->FilterValues;
422 * Retrieves list of all enabled fonts from Fonts.com.
425 * Array of enabled fonts.com font objects.
427 function fontscom_api_get_all_enabled_fonts() {
429 $config = \Drupal::config('fontscom_api.settings');
430 $path = '/rest/json/Fonts/?wfspid=' . $config->get('project');
431 $uri = FONTSCOM_API_BASE_URL . $path;
432 $response = \Drupal::httpClient()->get($uri, ['headers' => fontscom_api_headers($path), 'verify' => FALSE]);
433 $data = json_decode((string) $response->getBody());
435 catch (Exception $e) {
436 drupal_set_message(t('There was an error retrieving total Font count from Fonts.com. Error: %error', ['%error' => $e->getMessage()]), 'error');
440 foreach ($data->Fonts->Font as $font) {
441 $enabled_fonts[$font->FontID] = $font;
443 return $enabled_fonts;
447 * Adds font to fonts.com project package.
453 * True if font added successfully. FALSE otherwise.
455 function fontscom_api_add_font_to_current_project($fid) {
457 $config = \Drupal::config('fontscom_api.settings');
458 $path = '/rest/json/Fonts/?wfspid=' . $config->get('project');
459 $uri = FONTSCOM_API_BASE_URL . $path;
460 $response = \Drupal::httpClient()->post($uri, [
461 'headers' => fontscom_api_headers($path),
463 'body' => 'wfsfid=' . $fid,
465 $data = json_decode((string) $response->getBody());
466 fontscom_api_publish_updated_project();
469 catch (Exception $e) {
470 drupal_set_message(t('There was an error adding font to Fonts.com project. Error: %error', ['%error' => Psr7\str($e->getResponse())]), 'error');
476 * Removes font from fonts.com project package.
482 * True if font removed successfully. FALSE otherwise.
484 function fontscom_api_remove_font_from_current_project($fid) {
486 $config = \Drupal::config('fontscom_api.settings');
487 $path = '/rest/json/Fonts/?wfspid=' . $config->get('project') . '&wfsfid=' . $fid;
488 $uri = FONTSCOM_API_BASE_URL . $path;
489 $response = \Drupal::httpClient()->delete($uri, ['headers' => fontscom_api_headers($path), 'verify' => FALSE]);
490 $data = json_decode((string) $response->getBody());
491 fontscom_api_publish_updated_project();
494 catch (Exception $e) {
495 drupal_set_message(t('There was an error removing font from Fonts.com project. Error: %error', ['%error' => Psr7\str($e->getResponse())]), 'error');
501 * Updates fonts.com project package so updated font list is used.
504 * True if projects updated successfully. FALSE otherwise.
506 function fontscom_api_publish_updated_project() {
508 $path = '/rest/json/Publish/';
509 $uri = FONTSCOM_API_BASE_URL . $path;
510 $response = \Drupal::httpClient()->get($uri, ['headers' => fontscom_api_headers($path), 'verify' => FALSE]);
513 catch (Exception $e) {
514 drupal_set_message(t('There was an error publishing project on Fonts.com. Error: %error', ['%error' => Psr7\str($e->getResponse())]), 'error');
520 * Parses and adds additional data to fonts.com font object.
522 * @param object $fontscom_font
523 * Fonts.com font object.
525 function _fontscom_api_parse_imported_font($fontscom_font) {
526 $fontscom_font->name = $fontscom_font->FontName;
527 $fontscom_font->css_style = 'normal';
528 if (stripos('Italic', $fontscom_font->FontName) !== FALSE) {
529 $fontscom_font->css_style = 'italic';
532 $fontscom_font->css_weight = 400;
533 if (stripos('Extra Light', $fontscom_font->FontName) !== FALSE || stripos('Ultra Light', $fontscom_font->FontName) !== FALSE) {
534 $fontscom_font->css_weight = 100;
536 if (stripos('Thin', $fontscom_font->FontName) !== FALSE) {
537 $fontscom_font->css_weight = 200;
539 if (stripos('Light', $fontscom_font->FontName) !== FALSE) {
540 $fontscom_font->css_weight = 300;
542 if (stripos('Medium', $fontscom_font->FontName) !== FALSE) {
543 $fontscom_font->css_weight = 500;
545 elseif (stripos('SemiBold', $fontscom_font->FontName) !== FALSE || stripos('Semi Bold', $fontscom_font->FontName) !== FALSE) {
546 $fontscom_font->css_weight = 600;
548 elseif (stripos('Bold', $fontscom_font->FontName) !== FALSE) {
549 $fontscom_font->css_weight = 700;
551 elseif (stripos('Heavy', $fontscom_font->FontName) !== FALSE) {
552 $fontscom_font->css_weight = 800;
554 elseif (stripos('Black', $fontscom_font->FontName) !== FALSE) {
555 $fontscom_font->css_weight = 900;
560 * Generates @font-face css for fonts.com font.
562 * @param Drupal\fontyourface\FontInterface $font
563 * Font compatible with FontInterface.
568 function _fontscom_api_generate_font_css(FontInterface $font) {
569 $metadata = $font->getMetadata();
570 $data = "@font-face {\n";
571 $data .= "font-family: '{$font->css_family->value}';\n";
574 if ($metadata['eot']) {
575 $data .= "src: url('{$metadata['eot']}');\n";
576 $lines[] = "url('{$metadata['eot']}?#iefix') format('embedded-opentype')";
578 if ($metadata['ttf']) {
579 $lines[] = "url('{$metadata['ttf']}') format('truetype')";
581 if ($metadata['woff']) {
582 $lines[] = "url('{$metadata['woff']}') format('woff')";
584 if ($metadata['svg']) {
585 $lines[] = "url('{$metadata['svg']}#{$css_family}') format('svg')";
588 $data .= 'src: ' . implode(', ', $lines) . ";\n";
589 $data .= "font-weight: normal;\n";
590 $data .= "font-style: normal;\n";
591 return $data . "}\n";