How dravasp/addressautocomplete works
Project Type and Purpose
• This is a Magento 2.4.x module (“Magento_AddressAutoComplete”) that extends the Magento checkout by adding address‐autocomplete functionality using the Google Places/Maps API.
• It plugs into the Magento dependency‐injection and layout systems, retrieves configuration from the admin interface, injects that config into the frontend as part of the checkout jsLayout, and then loads Google’s JavaScript API to power the autocomplete UI.
High‐Level Components
a. Magento 2 Core Platform
• ComponentRegistrar (registration.php)
• Dependency Injection Container (di.xml)
• CompositeConfigProvider (Magento\Checkout\Model\CompositeConfigProvider)
• Layout & View Layer (Knockout JS, PHTML templates)
• Admin Configuration Persistence (etc/config.xml → Magento’s system configuration DB tables)
b. AddressAutoComplete Module
• etc/module.xml – module declaration
• etc/di.xml – injects AutoCompleteConfigProvider into CompositeConfigProvider
• etc/config.xml – defines system-admin config fields (enabled, API key, geolocation, long postcode)
• etc/csp_whitelist.xml – allows loading Google domains under CSP
• Helper/Data.php – retrieves store‐scope config values from Magento’s ScopeConfig
• Model/AutoCompleteConfigProvider.php – implements ConfigProviderInterface and exposes config to window.checkoutConfig
• ViewModel/Autocomplete.php – optional PHP view model for template use
• view/frontend/layout/checkout_index_index.xml – adds the JS component to Magento’s checkout jsLayout
• view/frontend/web/js/autocomplete.js – frontend logic to load Google Maps API, instantiate autocomplete, handle place_changed events
• templates/address/autocomplete.phtml – simple input markup if needed
c. External Service
• Google Maps JavaScript API (Places library)
Relationships and Data Flows
1. Admin user sets module configuration in Magento admin (enabled, API key, etc.) → stored in Magento’s configuration tables.
2. On checkout page render:
a. Magento’s CompositeConfigProvider reads AutoCompleteConfigProvider (wired via etc/di.xml).
b. AutoCompleteConfigProvider calls Helper/Data to fetch each config value.
c. CompositeConfigProvider merges the module config into the global window.checkoutConfig JS object.
3. checkout_index_index.xml updates the checkout page’s jsLayout to include the Magento_AddressAutoComplete JS component.
4. At page load, autocomplete.js reads window.checkoutConfig.address_autocomplete.api_key → dynamically injects a <script> tag to load Google Maps API.
5. Google Maps API invokes initAutocomplete() callback → binds a Google Places Autocomplete instance to the address input field.
6. User selects address suggestion → the JS fillInAddress() handler populates Magento’s street_1, street_2 fields.
7. Rest of Magento checkout flow continues with the completed address.
Architectural Patterns & Principles
• MVC / MVVM: PHP Model (ConfigProvider), ViewModel (block arguments), Template (PHTML), JS View (Knockout/RequireJS component).
• Dependency Injection: etc/di.xml wiring the ConfigProvider into Magento’s CompositeConfigProvider.
• Configuration‐as‐Code: config.xml defines system configuration fields, csp_whitelist.xml enforces Content Security Policy.
• Progressive Enhancement: the module only loads JS if api_key is present and the module is enabled.