定制 daften/addressing-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

daften/addressing-bundle

最新稳定版本:2.0.1

Composer 安装命令:

composer require daften/addressing-bundle

包简介

Provides an integration between commerceguys addressing package and Symfony.

README 文档

README

Build Status Maintainability

Requirements

  • jQuery loaded as $
  • jQuery Once loaded properly.

Installation

Add the mapping to your doctrine.yaml file:

doctrine:
    ...
    orm:
        ...
        entity_managers:
            default:
                ...
                mappings:
                    AddressingBundle:
                        is_bundle: true

TODO: Explain why this mapping is needed.

You'll also need to add some configuration or javascript depending on the form you'll use for address information. For this you need to run bin/console assets:install to copy the bundle assets to the public folder.

AddressEmbeddableType

You'll also need to add some javascript code, to make sure the form changes on changing the country code work.

The script below gives an example. You just need to initialize the javascript functionality. All address fields will automatically be covered. This only works when using Symfony 4 with Webpack Encore.

var countryCodeChange = require('../../public/bundles/addressing/js/countryCodeChange');
countryCodeChange.initialize();

AddressEmbeddableGmapsAutocompleteType

You'll also need to add some javascript code, to make sure the autocomplete functionality works.

The script below gives an example. You just need to initialize the javascript functionality. All autocomplete address fields will automatically be covered. This only works when using Symfony 4 with Webpack Encore.

var addressGmapsAutocomplete = require('../../public/bundles/addressing/js/addressGmapsAutocomplete');
addressGmapsAutocomplete.initialize();

You also need to add the Google API key to the .env file with property key GMAPS_API_KEY. You can override this by overruling the service definition for daften.service.gmaps_autocomplete_service.

Usage

Entity property

You need to add an address field as an ORM Embedded property.

<?php

namespace App\Entity;

use App\Repository\InstallationAddressRepository;
use Daften\Bundle\AddressingBundle\Entity\AddressEmbeddable;
use Daften\Bundle\AddressingBundle\Validator\Constraints as AddressingBundleAssert;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: InstallationAddressRepository::class)]
class AddressExample
{

    #[ORM\Embedded(class: AddressEmbeddable::class)]
    #[AddressingBundleAssert\EmbeddedAddressFormatConstraint(fieldOverrides: [
        'addressLine1' => 'required',
        'postalCode' => 'required',
        'locality' => 'required',
        'organization' => 'required',
        'givenName' => 'required',
        'familyName' => 'required',
        'addressLine2' => 'optional',
        'additionalName' => 'hidden',
        'administrativeArea' => 'hidden',
        'dependentLocality' => 'hidden',
        'sortingCode' => 'hidden',
    ])]
    private AddressEmbeddable $address;

    /**
     * AddressExample constructor.
     */
    public function __construct()
    {
        $this->address = new AddressEmbeddable();
    }

    /**
     * @return AddressEmbeddable
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * @param AddressEmbeddable $address
     */
    public function setAddress($address): void
    {
        $this->address = $address;
    }
}

Entity form

AddressEmbeddableType

There are 3 additional options that can be used for this form type:

  • allowed_countries: The countries allowed in the country dropdown. An array where the keys should be the country name and the values should be the 2-character country code.
  • preferred_countries: An array with the preferred countries, using the 2-character country codes.
  • default_country: The default country to show in the country dropdown.

An example form for the AddressExample class given above using the default AddressEmbeddableType with separate fields.

<?php

namespace App\Form;

use App\Entity\AddressExample;
use Daften\Bundle\AddressingBundle\Form\Type\AddressEmbeddableType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Class AddressExampleType
 */
class AddressExampleType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('address', AddressEmbeddableType::class, [
                 'allowed_countries' => [
                     'United States' => 'US',
                     'United Kingdom' => 'UK',
                     'Belgium' => 'BE',
                 ],
                 'preferred_countries' => ['BE', 'US'],
                 'default_country' => 'US',
             ]);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => AddressExample::class,
        ]);
    }
}

AddressEmbeddableGmapsAutocompleteType

There is 1 additional option that can be used for this form type:

  • allowed_countries: The countries allowed for autocompletion. An array where the values should be the 2-character country code.

An example form for the AddressExample class given above using the AddressEmbeddableGmapsAutocompleteType with one autocomplete field.

<?php

namespace App\Form;

use App\Entity\AddressExample;
use Daften\Bundle\AddressingBundle\Form\Type\AddressEmbeddableGmapsAutocompleteType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Class AddressExampleType2
 */
class AddressExampleType2 extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
             ->add('address', AddressEmbeddableGmapsAutocompleteType::class, [
                'label' => 'address',
                'translation_domain' => 'address',
                'allowed_countries' => [
                    'BE',
                    'NL',
                ],
            ]);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => AddressExample::class,
        ]);
    }
}

统计信息

  • 总下载量: 2.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-19