Skip to content

Commit

Permalink
Add support for Google fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Lodder committed Dec 23, 2021
1 parent 8ce2025 commit 86e7dda
Showing 6 changed files with 152 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## WIP
- Add support for Google fonts
- Move to Web Asset Manager for assets

## 1.1.2
3 changes: 0 additions & 3 deletions component.php
Original file line number Diff line number Diff line change
@@ -8,9 +8,6 @@

defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Factory;

/** @var Joomla\CMS\Document\HtmlDocument $this */

$wa = $this->getWebAssetManager();
115 changes: 115 additions & 0 deletions fields/fonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* @package Lightning
*
* @copyright Copyright (C) 2020 JoomJunk. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Http\HttpFactory;
use Joomla\Registry\Registry;

FormHelper::loadFieldClass('list');

class JFormFieldFonts extends Joomla\CMS\Form\Field\ListField
{
/**
* @var string
*/
protected $type = 'Fonts';

/**
* @var string
*/
private $apiUrl = 'https://www.googleapis.com/webfonts/v1/webfonts?key=';

/**
* @var string
*/
protected $layout = 'joomla.form.field.list';

/**
* Method to get the field input markup for a generic list.
* Use the multiple attribute to enable multiselect.
*
* @return string The field input markup.
*/
protected function getInput()
{
$data = $this->getLayoutData();

$data['options'] = (array) $this->getOptions();

return $this->getRenderer($this->layout)->render($data);
}

/**
* Method to get the field options.
*
* @return array The field option objects.
*/
protected function getOptions()
{
$options = new Registry;
$options->set('userAgent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0');
$http = HttpFactory::getHttp($options);

$array = [[
'value' => '',
'text' => 'None',
]];

try
{
$result = $http->get($this->apiUrl . $this->getParams()->get('google-fonts-api-key', ''));
$json = json_decode($result->body);
}
catch (RuntimeException $e)
{
Factory::getApplication()->enqueueMessage('Unable to fetch API data', 'error');
throw new \RuntimeException('Unable to fetch API data.', $e->getCode(), $e);
}

if (isset($json->error))
{
return Factory::getApplication()->enqueueMessage($json->error->message, 'error');
}

foreach ($json->items as $font)
{
$tmp = [
'value' => $font->family,
'text' => $font->family,
];
$array[] = (object) $tmp;
}

return $array;
}

/**
* Method to get the lightning template parameters.
*
* @return Registry The params object.
*/
protected function getParams()
{
$db = Factory::getDbo();

$db->setQuery(
$db->getQuery(true)
->select('params')
->from('#__template_styles')
->where('template = ' . $db->q('lightning'))
->where('client_id = 0')
);
$registry = new Registry;
$registry->loadString($db->loadResult());

return $registry;
}
}
14 changes: 14 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
$pageclass = $app->getMenu()->getActive()->getParams()->get('pageclass_sfx');
$themeSwitcher = (boolean)$this->params->get('theme-switcher', 1);
$fontAwesome = (boolean)$this->params->get('font-awesome-thats-actually-rather-shit', 1);
$googleFont = $this->params->get('google-font', '');

// Load switcher CSS
if ($themeSwitcher)
@@ -36,6 +37,19 @@
$wa->useStyle('fontawesome.css');
}

// Google font
if ($googleFont !== '')
{
$fontFamily = str_replace(' ', '+', $googleFont);
$this->getPreloadManager()->preconnect('https://fonts.googleapis.com', []);
$this->getPreloadManager()->preconnect('https://fonts.gstatic.com', []);
$this->addHeadLink('https://fonts.googleapis.com/css2?family=' . $fontFamily . '&display=swap', 'stylesheet', 'rel');

$wa->addInlineStyle(':root {
--hiq-font-family-base: "' . $googleFont . '";
}');
}

// Load switcher JS
// This should be loaded even if the themeSwitcher is disabled, so that the system preference will still dictate the theme
$wa->useScript('switch.js');
6 changes: 6 additions & 0 deletions language/en-GB/en-GB.tpl_lightning.ini
Original file line number Diff line number Diff line change
@@ -14,6 +14,12 @@ TPL_LIGHTNING_FIELDSET_CSS_OVERRIDES="CSS Overrides"
TPL_LIGHTNING_FIELDSET_LOGO="Logo"
TPL_LIGHTNING_FIELDSET_OPTIMIZE="Optimize"

TPL_LIGHTNING_GOOGLE_FONTS="Google Fonts"
TPL_LIGHTNING_GOOGLE_FONTS_API_KEY="Google Fonts API Key"
TPL_LIGHTNING_GOOGLE_FONTS_API_KEY_DESC="Entering Google Fonts API key will be used to populate the fonts list below"

TPL_LIGHTNING_LOAD_FONT_AWESOME="Load Font Awesome"

TPL_LIGHTNING_LOGO_LABEL="Site logo file"
TPL_LIGHTNING_LOGO_SVG_LABEL="Site logo SVG"
TPL_LIGHTNING_LOGO_SVG_DESC="Add the SVG code for your logo. This will take presidence over the <code>Site logo file</code> option."
17 changes: 16 additions & 1 deletion templateDetails.xml
Original file line number Diff line number Diff line change
@@ -78,11 +78,26 @@
type="radio"
layout="joomla.form.field.radio.switcher"
default="1"
label="Load Font awesome"
label="TPL_LIGHTNING_LOAD_FONT_AWESOME"
>
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>

<field
name="google-fonts-api-key"
type="text"
default=""
label="TPL_LIGHTNING_GOOGLE_FONTS_API_KEY"
description="TPL_LIGHTNING_GOOGLE_FONTS_API_KEY_DESC"
/>

<field
name="google-font"
type="fonts"
default=""
label="TPL_LIGHTNING_GOOGLE_FONTS"
/>
</fieldset>

<fieldset name="logo" label="TPL_LIGHTNING_FIELDSET_LOGO">

0 comments on commit 86e7dda

Please sign in to comment.