Skip to content

Commit a464849

Browse files
author
Will Morgan
committed
Refactor __construct for shorter method, override
1 parent 08dc4e5 commit a464849

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

code/GoogleMapField.php

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,46 @@ public function __construct(DataObject $data, $title, $options = array()) {
4545
// Set up fieldnames
4646
$this->setupOptions($options);
4747

48+
$this->setupChildren();
49+
50+
parent::__construct($this->getName(), $title);
51+
}
52+
53+
// Auto generate a name
54+
public function getName() {
4855
$fieldNames = $this->getOption('field_names');
56+
return sprintf(
57+
'%s_%s_%s',
58+
$this->data->class,
59+
$fieldNames['Latitude'],
60+
$fieldNames['Longitude']
61+
);
62+
}
63+
64+
/**
65+
* Merge options preserving the first level of array keys
66+
* @param array $options
67+
*/
68+
public function setupOptions(array $options) {
69+
$this->options = static::config()->default_options;
70+
foreach($this->options as $name => &$value) {
71+
if(isset($options[$name])) {
72+
if(is_array($value)) {
73+
$value = array_merge($value, $options[$name]);
74+
}
75+
else {
76+
$value = $options[$name];
77+
}
78+
}
79+
}
80+
}
4981

50-
// Auto generate a name
51-
$name = sprintf('%s_%s_%s', $data->class, $fieldNames['Latitude'], $fieldNames['Longitude']);
82+
/**
83+
* Set up child hidden fields, and optionally the search box.
84+
* @return FieldList the children
85+
*/
86+
public function setupChildren() {
87+
$name = $this->getName();
5288

5389
// Create the latitude/longitude hidden fields
5490
$this->latField = HiddenField::create(
@@ -83,25 +119,7 @@ public function __construct(DataObject $data, $title, $options = array()) {
83119
);
84120
}
85121

86-
parent::__construct($name, $title);
87-
}
88-
89-
/**
90-
* Merge options preserving the first level of array keys
91-
* @param array $options
92-
*/
93-
public function setupOptions(array $options) {
94-
$this->options = static::config()->default_options;
95-
foreach($this->options as $name => &$value) {
96-
if(isset($options[$name])) {
97-
if(is_array($value)) {
98-
$value = array_merge($value, $options[$name]);
99-
}
100-
else {
101-
$value = $options[$name];
102-
}
103-
}
104-
}
122+
return $this->children;
105123
}
106124

107125
/**

0 commit comments

Comments
 (0)