Skip to content

Commit

Permalink
Init standalone PCA address support
Browse files Browse the repository at this point in the history
  • Loading branch information
baikho committed May 17, 2020
1 parent a02001c commit ab62ea6
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use Drupal\pca_address\Form\PcaAddressSettingsForm;

/**
* Provides a PCA address form element.
* Provides an advanced PCA address form element.
*
* Usage example:
* @code
* $form['address'] = [
* '#type' => 'pca_address',
* '#type' => 'pca_address_advanced',
* '#pca_fields' => [
* [
* 'element' => PcaAddressElement::ADDRESS_LOOKUP,
Expand All @@ -43,9 +43,9 @@
*
* @see \Drupal\address\Element\Address
*
* @FormElement("pca_address")
* @FormElement("pca_address_advanced")
*/
class PcaAddress extends Address {
class AddressPcaAddress extends Address {

/**
* {@inheritdoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use Drupal\Core\Form\FormStateInterface;

/**
* Plugin implementation of the 'pca_address' widget.
* Plugin implementation of the 'pca_address_advanced' widget.
*
* @FieldWidget(
* id = "pca_address",
* id = "pca_address_advanced",
* label = @Translation("PCA Address"),
* field_types = {
* "address"
* },
* )
*/
class PcaAddressWidget extends AddressDefaultWidget {
class AddressPcaAddressWidget extends AddressDefaultWidget {

/**
* {@inheritdoc}
Expand Down Expand Up @@ -88,7 +88,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$element = parent::formElement($items, $delta, $element,$form, $form_state);
$widget_settings = $this->getSettings();
// Override to PCA address variant.
$element['address']['#type'] = 'pca_address';
$element['address']['#type'] = 'pca_address_advanced';
// Set field mapping settings.
$element['address']['#pca_fields'] = $widget_settings['pca_fields'];
// Set options settings.
Expand Down
51 changes: 51 additions & 0 deletions src/Element/LoqatePcaAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Drupal\loqate\Element;

use Drupal\Core\Render\Element\FormElement;

/**
* Provides a standalone Loqate PCA address form element.
*
* Usage example:
* @code
* $form['address'] = [
* '#type' => 'pca_address',
* '#pca_fields' => [
* [
* 'element' => PcaAddressElement::ADDRESS_LOOKUP,
* ],
* [
* 'element' => PcaAddressElement::LINE1,
* 'field' => PcaAddressField::LINE1,
* 'mode' => PcaAddressMode::POPULATE,
* ],
* ...
* ],
* '#pca_options' => [
* 'key' => config_key_id, // Defaults to key from config.
* 'countries' => ['codesList' => 'USA,CAN'],
* 'setCountryByIP' => false,
* ...
* ],
* '#show_address_fields' => FALSE,
* '#allow_manual_input' => TRUE,
* ...
* ];
* @endcode
*
* @FormElement("pca_address")
*/
class LoqatePcaAddress extends FormElement {

/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [

];
}

}
97 changes: 97 additions & 0 deletions src/Plugin/Field/FieldType/LoqatePcaAddressItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Drupal\loqate\Plugin\Field\FieldType;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\loqate\PcaAddressFieldMapping\PcaAddressElement;

/**
* PCA address field item.
*
* @FieldType(
* id = "pca_address",
* label = @Translation("PCA Address"),
* description = @Translation("An entity field containing a postal address"),
* category = @Translation("Field"),
* default_widget = "pca_address",
* default_formatter = "pca_address",
* list_class = "\Drupal\Core\Field\FieldItemList",
* )
*/
class LoqatePcaAddressItem extends FieldItemBase {

/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = [];

$properties[PcaAddressElement::LINE1] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Address Line 1'));

$properties[PcaAddressElement::LINE2] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Address Line 2'));

$properties[PcaAddressElement::LOCALITY] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('City/Town'));

$properties[PcaAddressElement::ADMINISTRATIVE_AREA] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('State/Province'));

$properties[PcaAddressElement::POSTAL_CODE] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('ZIP/Postal Code'));

$properties[PcaAddressElement::COUNTRY_CODE] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Country'));

return $properties;
}

/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {

$schema['columns'][PcaAddressElement::LINE1] = [
'type' => 'varchar',
'length' => 255,
'default' => NULL,
];

$schema['columns'][PcaAddressElement::LINE2] = [
'type' => 'varchar',
'length' => 255,
'default' => NULL,
];

$schema['columns'][PcaAddressElement::LOCALITY] = [
'type' => 'varchar',
'length' => 255,
'default' => NULL,
];

$schema['columns'][PcaAddressElement::ADMINISTRATIVE_AREA] = [
'type' => 'varchar',
'length' => 255,
'default' => NULL,
];

$schema['columns'][PcaAddressElement::POSTAL_CODE] = [
'type' => 'varchar',
'length' => 255,
'default' => NULL,
];

$schema['columns'][PcaAddressElement::COUNTRY_CODE] = [
'type' => 'varchar',
'length' => 255,
'default' => NULL,
];

return $schema;
}

}

0 comments on commit ab62ea6

Please sign in to comment.