Skip to content

Addresses ​

Type
Class
Namespace
craft\services
Inherits
craft\services\Addresses » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
4.0.0

Addresses service.

An instance of the Addresses service is globally accessible in Craft via Craft::$app->addresses.

View source

Public Properties ​

PropertyDescription
addressFormatRepository\CommerceGuys\Addressing\AddressFormat\AddressFormatRepository
behaviorsyii\base\Behavior – List of behaviors attached to this component.
countryListarray
countryRepository\CommerceGuys\Addressing\Country\CountryRepository
formatter\CommerceGuys\Addressing\Formatter\FormatterInterface, null – The default address formatter used by formatAddress()
layoutcraft\models\FieldLayout
subdivisionRepositorycraft\addresses\SubdivisionRepository

addressFormatRepository ​

Type
\CommerceGuys\Addressing\AddressFormat\AddressFormatRepository
Default value
null

View source

countryList ​

Type
array
Default value
null
Access
Read-only
Since
4.13.0

View source

countryRepository ​

Type
\CommerceGuys\Addressing\Country\CountryRepository
Default value
null

View source

formatter ​

Type
\CommerceGuys\Addressing\Formatter\FormatterInterface, null
Default value
null
Since
4.5.0

The default address formatter used by formatAddress()

View source

layout ​

Type
craft\models\FieldLayout
Default value
null
Access
Read-only

View source

subdivisionRepository ​

Type
craft\addresses\SubdivisionRepository
Default value
null

View source

Public Methods ​

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
defineAddressSubdivisions()Returns subdivisions for a field based on its parents.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
formatAddress()Formats the address model into the correct sequence and format in HTML.
getAddressFormatRepository()
getAdministrativeAreaTypeLabel()
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getCountryList()Returns a list of countries to be used as options for selection.
getCountryRepository()
getDependentLocalityTypeLabel()
getFieldLabel()Returns the user-facing label for an address field, for a given country code.
getLayout()Returns the address field layout.
getLocalityTypeLabel()
getPostalCodeTypeLabel()
getSubdivisionRepository()
getUsedFields()Returns the address fields that are used by a given country code.
getUsedSubdivisionFields()Returns the subdivision address fields that are used by a given country code.
handleChangedAddressFieldLayout()Handle address field layout changes.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined for this component.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
saveLayout()Save the address field layout
trigger()Triggers an event.

defineAddressSubdivisions() ​

Since
4.5.0

Returns subdivisions for a field based on its parents.

View source

Arguments ​

Returns ​

array

formatAddress() ​

Formats the address model into the correct sequence and format in HTML.

View source

Arguments ​

Returns ​

string

getAddressFormatRepository() ​

View source

Returns ​

\CommerceGuys\Addressing\AddressFormat\AddressFormatRepository

getAdministrativeAreaTypeLabel() ​

View source

Arguments ​

Returns ​

string

getCountryList() ​

Since
4.13.0

Returns a list of countries to be used as options for selection.

View source

Arguments ​

Returns ​

array

getCountryRepository() ​

View source

Returns ​

\CommerceGuys\Addressing\Country\CountryRepository

getDependentLocalityTypeLabel() ​

View source

Arguments ​

Returns ​

string

getFieldLabel() ​

Since
4.3.0

Returns the user-facing label for an address field, for a given country code.

View source

Arguments ​

  • $field (string) – One of the \CommerceGuys\Addressing\AddressFormat\AddressField class constants
  • $countryCode (string)

Returns ​

string

getLayout() ​

Returns the address field layout.

View source

Returns ​

craft\models\FieldLayout

getLocalityTypeLabel() ​

View source

Arguments ​

Returns ​

string

getPostalCodeTypeLabel() ​

View source

Arguments ​

Returns ​

string

getSubdivisionRepository() ​

View source

Returns ​

craft\addresses\SubdivisionRepository

getUsedFields() ​

Since
4.3.0

Returns the address fields that are used by a given country code.

See also \CommerceGuys\Addressing\AddressFormat\AddressFieldView source

Arguments ​

Returns ​

string[]

getUsedSubdivisionFields() ​

Since
4.3.0

Returns the subdivision address fields that are used by a given country code.

See also \CommerceGuys\Addressing\AddressFormat\AddressFieldView source

Arguments ​

Returns ​

string[]

handleChangedAddressFieldLayout() ​

Handle address field layout changes.

View source

Arguments ​

init() ​

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source

saveLayout() ​

Save the address field layout

View source

Arguments ​

Returns ​

boolean

Events ​

EVENT_DEFINE_ADDRESS_COUNTRIES ​

Type
craft\events\DefineAddressCountriesEvent
Since
4.13.0

The event that is triggered when defining country options for an address.

This event is primarily used to modify the list of countries that are available for selection. You can also use the event to add additional countries to the list, however, this will require you to use dependency injection to override the Addresses::getCountryRepository() method and provide your own CountryRepository instance.

See also getCountryList()


EVENT_DEFINE_ADDRESS_SUBDIVISIONS ​

Type
craft\events\DefineAddressSubdivisionsEvent
Since
4.5.0

The event that is triggered when defining subdivisions options for an address field for a given country code, and optionally administrativeArea and locality.

See also defineAddressSubdivisions()


EVENT_DEFINE_FIELD_LABEL ​

Type
craft\events\DefineAddressFieldLabelEvent
Since
4.3.0

The event that is triggered when defining the label of an address field for a given country code.

See also getFieldLabel()


EVENT_DEFINE_USED_FIELDS ​

Type
craft\events\DefineAddressFieldsEvent
Since
4.3.0

The event that is triggered when defining the address fields that are used by a given country code.

See also getUsedFields()


EVENT_DEFINE_USED_SUBDIVISION_FIELDS ​

Type
craft\events\DefineAddressFieldsEvent
Since
4.3.0

The event that is triggered when defining the subdivision address fields that are used by a given country code.

See also getUsedSubdivisionFields()