Skip to content

Commit 178afd8

Browse files
author
Will Morgan
committed
Clean up dependency management in Field
1 parent 5b3fd64 commit 178afd8

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

code/GoogleMapField.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ public function setupOptions(array $options) {
115115
* {@inheritdoc}
116116
*/
117117
public function Field($properties = array()) {
118-
$key = $this->options['api_key'] ? "&key=".$this->options['api_key'] : "";
119-
Requirements::javascript(GOOGLEMAPFIELD_BASE .'/javascript/GoogleMapField.js');
120-
Requirements::javascript("//maps.googleapis.com/maps/api/js?callback=googlemapfieldInit".$key);
121-
Requirements::css(GOOGLEMAPFIELD_BASE .'/css/GoogleMapField.css');
122118
$jsOptions = array(
123119
'coords' => array(
124120
$this->recordFieldData('Latitude'),
@@ -132,10 +128,26 @@ public function Field($properties = array()) {
132128

133129
$jsOptions = array_replace_recursive($this->options, $jsOptions);
134130
$this->setAttribute('data-settings', Convert::array2json($jsOptions));
135-
131+
$this->requireDependencies();
136132
return parent::Field($properties);
137133
}
138134

135+
/**
136+
* Set up and include any frontend requirements
137+
* @return void
138+
*/
139+
protected function requireDependencies() {
140+
$gmapsParams = array(
141+
'callback' => 'googlemapfieldInit',
142+
);
143+
if($key = $this->getOption('api_key')) {
144+
$gmapsParams['key'] = $key;
145+
}
146+
Requirements::css(GOOGLEMAPFIELD_BASE .'/css/GoogleMapField.css');
147+
Requirements::javascript(GOOGLEMAPFIELD_BASE .'/javascript/GoogleMapField.js');
148+
Requirements::javascript('//maps.googleapis.com/maps/api/js?' . http_build_query($gmapsParams));
149+
}
150+
139151
/**
140152
* {@inheritdoc}
141153
*/

0 commit comments

Comments
 (0)