Skip to content

Commit

Permalink
Added field for Google API key.
Browse files Browse the repository at this point in the history
  • Loading branch information
colintucker committed Apr 4, 2018
1 parent 2e7582f commit 46b5e04
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/API/GoogleAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ class GoogleAPI
use Injectable;
use Configurable;

/**
* Answers the API key from site or YAML configuration.
*
* @return string
*/
public function getAPIKey()
{
$key = SiteConfig::current_site_config()->GoogleAPIKey;

if (!$key) {
$key = self::config()->api_key;
}

return $key;
}

/**
* Answers true if the receiver has an API key.
*
* @return boolean
*/
public function hasAPIKey()
{
return (boolean) $this->getAPIKey();
}

/**
* Answers the API language from site or YAML configuration.
*
Expand Down
32 changes: 32 additions & 0 deletions src/Extensions/GoogleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class GoogleConfig extends ServicesConfig
* @config
*/
private static $db = [
'GoogleAPIKey' => 'Varchar(128)',
'GoogleAPILanguage' => 'Varchar(8)',
'GoogleVerificationCode' => 'Varchar(64)',
'GoogleAnalyticsTrackingID' => 'Varchar(64)',
Expand Down Expand Up @@ -82,6 +83,15 @@ public function updateCMSFields(FieldList $fields)
'GoogleAPIConfig',
$this->owner->fieldLabel('GoogleAPIConfig'),
[
TextField::create(
'GoogleAPIKey',
$this->owner->fieldLabel('GoogleAPIKey')
)->setRightTitle(
_t(
__CLASS__ . '.GOOGLEAPIKEYRIGHTTITLE',
'Create credentials using the Google API Manager and paste the API key here.'
)
),
DropdownField::create(
'GoogleAPILanguage',
$this->owner->fieldLabel('GoogleAPILanguage'),
Expand Down Expand Up @@ -133,6 +143,7 @@ public function updateFieldLabels(&$labels)
// Update Field Labels:

$labels['Google'] = _t(__CLASS__ . '.GOOGLE', 'Google');
$labels['GoogleAPIKey'] = _t(__CLASS__ . '.GOOGLEAPIKEY', 'Google API Key');
$labels['GoogleAPIConfig'] = _t(__CLASS__ . '.GOOGLEAPI', 'Google API');
$labels['GoogleAPILanguage'] = _t(__CLASS__ . '.LANGUAGE', 'Language');
$labels['GoogleAnalyticsConfig'] = _t(__CLASS__ . '.GOOGLEANALYTICS', 'Google Analytics');
Expand Down Expand Up @@ -164,6 +175,10 @@ public function getBodyAttributes()

$api = GoogleAPI::singleton();

if ($key = $api->getAPIKey()) {
$attributes['data-google-api-key'] = $key;
}

if ($lang = $api->getAPILanguage()) {
$attributes['data-google-api-lang'] = $lang;
}
Expand All @@ -185,6 +200,23 @@ public function getGoogleLanguageOptions()
return $this->owner->config()->google_languages;
}

/**
* Answers a status message array for the CMS interface.
*
* @return string
*/
public function getGoogleStatusMessage()
{
if (!GoogleAPI::singleton()->hasAPIKey()) {

return _t(
__CLASS__ . '.GOOGLEAPIKEYMISSING',
'Google API Key has not been entered into site configuration.'
);

}
}

/**
* Removes any HTML present in the pasted value and answers the verification code.
*
Expand Down

0 comments on commit 46b5e04

Please sign in to comment.