Skip to content

Allow override of default address formatter #13242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 13, 2023

Conversation

lukeholder
Copy link
Member

@lukeholder lukeholder commented May 24, 2023

Description

Allows replacement of the default formatter that is used by the Addresses service.

Custom Formatter class:

<?php

namespace modules;

use CommerceGuys\Addressing\AddressInterface;

class MyFormatter extends \CommerceGuys\Addressing\Formatter\DefaultFormatter
{
    public function format(AddressInterface $address, array $options = []): string
    {
        return parent::format($address, $options) . $address->phone;
    }
}

Inject into app config:

app.php

'components' => [
    'addresses' => [
        'class' => \craft\services\Addresses::class,
        'formatter' => new \modules\MyFormatter(
            new \CommerceGuys\Addressing\AddressFormat\AddressFormatRepository(),
            new \CommerceGuys\Addressing\Country\CountryRepository(),
            new \CommerceGuys\Addressing\Subdivision\SubdivisionRepository()
        )
    ],
],

Related issues and discussions

Craft:

#12615

Commerce issues:

craftcms/commerce#3152
craftcms/commerce#3082

@bossanova808
Copy link

What are the odds of this one going in fairly soon?

@brandonkelly brandonkelly changed the base branch from develop to 4.5 June 13, 2023 16:10
@brandonkelly brandonkelly merged commit 9b2ede4 into 4.5 Jun 13, 2023
@brandonkelly brandonkelly deleted the feature/extendable-address-formatter branch June 13, 2023 16:15
@brandonkelly
Copy link
Member

brandonkelly commented Jun 13, 2023

@bossanova808 Just merged into the 4.5 branch.

If you want to start testing Craft 4.5 (still in development), you can change your craftcms/cms constraint in composer.json to 4.5.x-dev as 4.5.0 and run composer update.

@peteeveleigh
Copy link

Is there an estimated release date for 4.5? Are we talking days/weeks/months?
We could really use that address card formatter asap.

@brandonkelly
Copy link
Member

@peteeveleigh Maybe a month or so. It’s pretty stable though, and there’s no new migrations (yet, at least), so if you really need a feature, it should be safe to switch over to the 4.5 branch. To do so change your craftcms/cms requirement in composer.json to 4.5.x-dev as 4.5.0-alpha and run composer update.

@peteeveleigh
Copy link

@brandonkelly Thanks for the update

@jonleverrier
Copy link

jonleverrier commented Aug 24, 2023

Something to take note of in the example code above. This will add the phone field to all addresses, even those that appear in PDF's (Invoices etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants