|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_map\Plugin\Field\FieldWidget; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Entity\EntityFieldManagerInterface; |
|
6
|
|
|
use Drupal\Core\Field\FieldDefinitionInterface; |
|
7
|
|
|
use Drupal\Core\Field\FieldItemListInterface; |
|
8
|
|
|
use Drupal\Core\Field\WidgetBase; |
|
9
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
10
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
11
|
|
|
use Drupal\geocoder\GeocoderInterface; |
|
12
|
|
|
use Drupal\geofield\WktGeneratorInterface; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Plugin implementation of the 'df_tools_map_simple_geocoder' widget. |
|
17
|
|
|
* |
|
18
|
|
|
* @FieldWidget( |
|
19
|
|
|
* id = "df_tools_map_simple_geocoder", |
|
20
|
|
|
* label = @Translation("Simple Geocoder"), |
|
21
|
|
|
* field_types = { |
|
22
|
|
|
* "geofield" |
|
23
|
|
|
* }, |
|
24
|
|
|
* ) |
|
25
|
|
|
*/ |
|
26
|
|
|
class SimpleGeocoder extends WidgetBase implements ContainerFactoryPluginInterface { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The entity field manager. |
|
30
|
|
|
* |
|
31
|
|
|
* @var \Drupal\Core\Entity\EntityFieldManagerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $entityFieldManager; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The geocoder. |
|
37
|
|
|
* |
|
38
|
|
|
* @var \Drupal\geocoder\GeocoderInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $geocoder; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The WKT generator. |
|
44
|
|
|
* |
|
45
|
|
|
* @var \Drupal\geofield\WktGeneratorInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $wktGenerator; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityFieldManagerInterface $entity_field_manager, GeocoderInterface $geocoder, WktGeneratorInterface $wkt_generator) { |
|
53
|
|
|
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings); |
|
54
|
|
|
$this->entityFieldManager = $entity_field_manager; |
|
55
|
|
|
$this->geocoder = $geocoder; |
|
56
|
|
|
$this->wktGenerator = $wkt_generator; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
63
|
|
|
return new static( |
|
64
|
|
|
$plugin_id, |
|
65
|
|
|
$plugin_definition, |
|
66
|
|
|
$configuration['field_definition'], |
|
67
|
|
|
$configuration['settings'], |
|
68
|
|
|
$configuration['third_party_settings'], |
|
69
|
|
|
$container->get('entity_field.manager'), |
|
|
|
|
|
|
70
|
|
|
$container->get('geocoder'), |
|
|
|
|
|
|
71
|
|
|
$container->get('geofield.wkt_generator') |
|
|
|
|
|
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function defaultSettings() { |
|
79
|
|
|
return [ |
|
80
|
|
|
'source_field' => '', |
|
81
|
|
|
'show_coordinates' => TRUE, |
|
82
|
|
|
] + parent::defaultSettings(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function settingsForm(array $form, FormStateInterface $form_state) { |
|
89
|
|
|
$elements = parent::settingsForm($form, $form_state); |
|
90
|
|
|
|
|
91
|
|
|
$entity_field_definitions = $this->entityFieldManager->getFieldDefinitions($this->fieldDefinition->getTargetEntityTypeId(), $this->fieldDefinition->getTargetBundle()); |
|
92
|
|
|
|
|
93
|
|
|
$options = []; |
|
94
|
|
|
foreach ($entity_field_definitions as $id => $definition) { |
|
95
|
|
|
if ($definition->getType() == 'string' || $definition->getType() == 'address') { |
|
96
|
|
|
$options[$id] = $definition->getLabel(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$elements['source_field'] = [ |
|
101
|
|
|
'#type' => 'select', |
|
102
|
|
|
'#title' => $this->t('Source Field'), |
|
103
|
|
|
'#default_value' => $this->getSetting('source_field'), |
|
104
|
|
|
'#required' => TRUE, |
|
105
|
|
|
'#options' => $options, |
|
106
|
|
|
'#description' => t('The source Text or Address field to Geocode from.') |
|
107
|
|
|
]; |
|
108
|
|
|
|
|
109
|
|
|
$elements['show_coordinates'] = [ |
|
110
|
|
|
'#type' => 'checkbox', |
|
111
|
|
|
'#title' => t('Show Coordinates'), |
|
112
|
|
|
'#default_value' => $this->getSetting('show_coordinates'), |
|
113
|
|
|
'#description' => t('Whether or not the current coordinates should be shown in the form.') |
|
114
|
|
|
]; |
|
115
|
|
|
|
|
116
|
|
|
return $elements; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
|
|
public function settingsSummary() { |
|
123
|
|
|
$summary = []; |
|
124
|
|
|
$summary[] = $this->t('Source Field: @source', ['@source' => $this->getSetting('source_field')]); |
|
125
|
|
|
|
|
126
|
|
|
$coordinates = $this->getSetting('show_coordinates'); |
|
127
|
|
|
if ($coordinates) { |
|
128
|
|
|
$summary[] = $this->t('Coordinates are shown'); |
|
129
|
|
|
} |
|
130
|
|
|
else { |
|
131
|
|
|
$summary[] = $this->t('Coordinates are hidden'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $summary; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
|
|
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { |
|
141
|
|
|
$element += [ |
|
142
|
|
|
'#type' => 'item', |
|
143
|
|
|
'#title' => $this->t('Coordinates'), |
|
144
|
|
|
'#markup' => t('Latitude: @lat, Longitude: @lon', ['@lat' => $items[$delta]->lat, '@lon' => $items[$delta]->lon]), |
|
145
|
|
|
'#description' => t('These values are set dynamically on submit from the @field field.', ['@field' => $this->getSetting('source_field')]), |
|
146
|
|
|
]; |
|
147
|
|
|
|
|
148
|
|
|
// Hide the coordinates if none are available or the associated setting is |
|
149
|
|
|
// disabled. |
|
150
|
|
|
if (empty($items[$delta]->lat) && empty($items[$delta]->lon) || !$this->getSetting('show_coordinates')) { |
|
|
|
|
|
|
151
|
|
|
$element['#access'] = FALSE; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return ['value' => $element]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
|
|
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { |
|
161
|
|
|
// Retrieve the name of the source 'text' or 'address' field. |
|
162
|
|
|
$source_field = $this->getSetting('source_field'); |
|
163
|
|
|
|
|
164
|
|
|
// Only proceed if a source field is available. |
|
165
|
|
|
if (empty($source_field)) { |
|
166
|
|
|
return $values; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// Get the values of the source field. Otherwise, return an empty array so |
|
170
|
|
|
// that we can loop through it. |
|
171
|
|
|
$source_field_values = $form_state->getValue($source_field, []); |
|
172
|
|
|
|
|
173
|
|
|
// For each source field value, geocode the address and set our coordinates. |
|
174
|
|
|
foreach ($source_field_values as $delta => $value) { |
|
175
|
|
|
// The address information is located in an 'address' key. |
|
176
|
|
|
$address = $value['address']; |
|
177
|
|
|
|
|
178
|
|
|
// Create an empty string to store address information. |
|
179
|
|
|
$string = ''; |
|
180
|
|
|
|
|
181
|
|
|
// Check if this field is an Address field. |
|
182
|
|
|
if (isset($address['address_line1'])) { |
|
183
|
|
|
// Format the address as a single string. |
|
184
|
|
|
$string .= $address['address_line1'] . "\n"; |
|
185
|
|
|
$string .= !empty($address['address_line2']) ? $address['address_line2'] . "\n" : ''; |
|
186
|
|
|
$string .= $address['locality'] . ', '; |
|
187
|
|
|
$string .= str_replace('US-', '', $address['administrative_area']) . ' '; |
|
188
|
|
|
$string .= $address['postal_code'] . "\n"; |
|
189
|
|
|
$string .= $address['country_code']; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// Geocode the source field's value using Google Maps. |
|
193
|
|
|
if (!empty($string) && $collection = $this->geocoder->geocode($string, ['googlemaps'])) { |
|
194
|
|
|
// Set our value in a similar way to Geofield's LatLon Widget. |
|
195
|
|
|
// @see \Drupal\geofield\Plugin\Field\FieldWidget\GeofieldLatLonWidget::massageFormValues() |
|
196
|
|
|
$coordinates = $collection->first()->getCoordinates(); |
|
|
|
|
|
|
197
|
|
|
$point = [$coordinates->getLongitude(), $coordinates->getLatitude()]; |
|
198
|
|
|
$values[]['value'] = $this->wktGenerator->WktBuildPoint($point); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $values; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|