Skip to content

Instantly share code, notes, and snippets.

@phproberto
Created June 16, 2012 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phproberto/2939428 to your computer and use it in GitHub Desktop.
Save phproberto/2939428 to your computer and use it in GitHub Desktop.
Generate a JForm field list with all the fonts available in Google Web Fonts
<?php
defined('JPATH_BASE') or die;
jimport('joomla.html.html');
jimport('joomla.form.formfield');
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');
class JFormFieldGooglefont extends JFormFieldList {
/**
* The form field type.
*
* @var string
* @since 1.6
*/
protected $type = 'Googlefont';
// Google API key
const GOOGLE_API_KEY = 'AIzaSyDCuX9p0CoInlBnfj_YmrtxFizvQ5sT1Cs';
/**
* Generate dropdown options
*/
public function getOptions()
{
// Initialize variables.
$options = array();
$jsonResponse = @file_get_contents('https://www.googleapis.com/webfonts/v1/webfonts?key=' . self::GOOGLE_API_KEY);
if ($jsonResponse) {
$decodedResponse = json_decode($jsonResponse);
if (isset($decodedResponse->items)) {
$options[] = JHtml::_('select.option', '', '- Select font -');
foreach ($decodedResponse->items as $font) {
// replace spaces with + in option value
$value = str_replace(' ', '+', $font->family);
$options[] = JHtml::_('select.option', $value, $font->family);
}
} else{
$options[] = JHtml::_('select.option', '', 'ERROR: None or bad JSON received');
}
} else {
$options[] = JHtml::_('select.option', '', 'ERROR: Wrong URL or google API KEY?');
}
return $options;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment