cethyworks/google-place-autocomplete-bundle
Installation:
composer require cethyworks/google-place-autocomplete-bundle
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3.x):
Cethyworks\GooglePlaceAutocompleteBundle\CethyworksGooglePlaceAutocompleteBundle::class => ['all' => true],
Configuration:
Create config/packages/cethyworks_google_place_autocomplete.yaml:
cethyworks_google_place_autocomplete:
google:
api_key: 'YOUR_GOOGLE_API_KEY'
First Use Case: Add the form type to a Symfony form:
use Cethyworks\GooglePlaceAutocompleteBundle\Form\SimpleGooglePlaceAutocompleteType;
$builder->add('address', SimpleGooglePlaceAutocompleteType::class);
config/packages/cethyworks_google_place_autocomplete.yaml (configuration)src/Form/SimpleGooglePlaceAutocompleteType.php (basic usage)src/Form/ComplexGooglePlaceAutocompleteType.php (advanced usage)Workflow:
SimpleGooglePlaceAutocompleteType for minimal address input:
$builder->add('location', SimpleGooglePlaceAutocompleteType::class, [
'label' => 'Search Location',
'attr' => ['class' => 'form-control'],
]);
Template Rendering:
Workflow:
ComplexGooglePlaceAutocompleteType to access full place details:
$builder->add('location', ComplexGooglePlaceAutocompleteType::class, [
'mapped' => false, // Store raw data in a separate field
'attr' => ['data-place-details' => 'true'],
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$placeData = $form->get('location')->getData(); // Full Google Place object
}
mapped => false to store raw Google Place data (e.g., geometry, address_components) in a separate field.lat, lng) to your entity:
$builder->add('address', SimpleGooglePlaceAutocompleteType::class, [
'lat_field' => 'latitude',
'lng_field' => 'longitude',
]);
config/packages/cethyworks_google_place_autocomplete.yaml (never hardcode).%env(GOOGLE_PLACE_API_KEY)% and reference it in the config.API Key Restrictions:
CORS Issues:
Deprecated Symfony Features:
JavaScript Conflicts:
data-autocomplete-id to isolate them:
{{ form_widget(form.location, {'attr': {'data-autocomplete-id': 'custom-id'}}) }}
# config/packages/dev/doctrine.yaml
doctrine:
orm:
entity_managers_default:
logging: true
$builder->add('address', SimpleGooglePlaceAutocompleteType::class, [
'fallback_value' => 'Default Address',
]);
Custom Templates: Override the form theme to modify the autocomplete dropdown:
{# templates/form/fields.html.twig #}
{% block google_place_autocomplete_widget %}
{{ parent() }}
<div class="custom-styles">
<!-- Your custom dropdown styling -->
</div>
{% endblock %}
Extend the Type: Create a custom type to add logic (e.g., auto-fetching coordinates):
use Cethyworks\GooglePlaceAutocompleteBundle\Form\SimpleGooglePlaceAutocompleteType;
class ExtendedGooglePlaceAutocompleteType extends SimpleGooglePlaceAutocompleteType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'auto_fetch_coordinates' => false,
]);
parent::configureOptions($resolver);
}
}
Event Listeners:
Subscribe to the google_place.autocomplete.result event to process raw responses:
// src/EventListener/GooglePlaceListener.php
public function onAutocompleteResult(GooglePlaceEvent $event) {
$place = $event->getPlace();
// Manipulate $place data before form submission
}
Register in services.yaml:
services:
App\EventListener\GooglePlaceListener:
tags:
- { name: kernel.event_listener, event: google_place.autocomplete.result }
{# base.html.twig #}
<script src="https://maps.googleapis.com/maps/api/js?key={{ google_api_key }}&libraries=places"></script>
How can I help you explore Laravel packages today?