function _google_fonts_api_get_fonts_from_api in @font-your-face 8.3
Retrieves fonts from api and parses them for consumption.
Parameters
string $temp_key: An API key to use for testing.
Return value
array List of fonts ready for ingesting as FontInterface objects.
2 calls to _google_fonts_api_get_fonts_from_api()
- google_fonts_api_fontyourface_import in modules/
google_fonts_api/ google_fonts_api.module - Implements hook_fontyourface_import().
- google_fonts_api_form_font_settings_validate in modules/
google_fonts_api/ google_fonts_api.module - Form validation handler for Google Font settings.
File
- modules/
google_fonts_api/ google_fonts_api.module, line 147 - Google Fonts module file.
Code
function _google_fonts_api_get_fonts_from_api($temp_key = '') {
// Return the JSON object with all available fonts.
$google_api_key = $temp_key ?: \Drupal::config('google_fonts_api.settings')
->get('google_api_key');
try {
$uri = 'https://www.googleapis.com/webfonts/v1/webfonts?key=' . $google_api_key;
$response = \Drupal::httpClient()
->get($uri, [
'headers' => [
'Accept' => 'text/plain',
],
'verify' => FALSE,
]);
if ($response
->getStatusCode() == 200) {
$data = (string) $response
->getBody();
$json_results = json_decode($data);
return $json_results->items;
}
else {
// Connection was successful but Google responded with an error code.
throw new \Exception();
}
} catch (Exception $e) {
Drupal::messenger()
->addMessage(t('The list of Google Fonts could not be fetched. Verify that your server can connect to the Google servers (https://www.googleapis.com). Error: %error', [
'%error' => $e
->getMessage(),
]), 'error');
return FALSE;
}
}