Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function __construct($options = null)
 {
     parent::__construct($options);
     // Validate all fields by default.
     if (empty($this->fields)) {
         $this->fields = AddressField::getAll();
     }
 }
 /**
  * {@inheritDoc}
  */
 public function __construct($options = NULL)
 {
     parent::__construct($options);
     // Validate all fields by default.
     if (empty($this->fields)) {
         $this->fields = array_values(AddressField::getAll());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getGroupedFields()
 {
     if (!isset($this->groupedFields)) {
         $this->groupedFields = [];
         $expression = '/\\%(' . implode('|', AddressField::getAll()) . ')/';
         $formatLines = explode("\n", $this->format);
         foreach ($formatLines as $index => $formatLine) {
             preg_match_all($expression, $formatLine, $foundTokens);
             foreach ($foundTokens[0] as $token) {
                 $this->groupedFields[$index][] = substr($token, 1);
             }
         }
         // The indexes won't be sequential if there were any rows
         // without tokens, so reset them.
         $this->groupedFields = array_values($this->groupedFields);
     }
     return $this->groupedFields;
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function form(array $form, FormStateInterface $form_state)
 {
     $form = parent::form($form, $form_state);
     $address_format = $this->entity;
     $country_code = $address_format->getCountryCode();
     if ($country_code == 'ZZ') {
         $form['countryCode'] = ['#type' => 'item', '#title' => $this->t('Country'), '#markup' => $this->t('Generic')];
     } else {
         $form['countryCode'] = ['#type' => 'select', '#title' => $this->t('Country'), '#default_value' => $country_code, '#required' => TRUE, '#options' => $this->countryRepository->getList(), '#disabled' => !$address_format->isNew()];
     }
     $form['format'] = ['#type' => 'textarea', '#title' => $this->t('Format'), '#description' => $this->t('Available tokens: @tokens', ['@tokens' => implode(', ', AddressField::getTokens())]), '#default_value' => $address_format->getFormat(), '#required' => TRUE];
     $form['requiredFields'] = ['#type' => 'checkboxes', '#title' => t('Required fields'), '#options' => LabelHelper::getGenericFieldLabels(), '#default_value' => $address_format->getRequiredFields()];
     $form['uppercaseFields'] = ['#type' => 'checkboxes', '#title' => t('Uppercase fields'), '#description' => t('Uppercased on envelopes to facilitate automatic post handling.'), '#options' => LabelHelper::getGenericFieldLabels(), '#default_value' => $address_format->getUppercaseFields()];
     $form['postalCodePattern'] = ['#type' => 'textfield', '#title' => $this->t('Postal code pattern'), '#description' => $this->t('Regular expression used to validate postal codes.'), '#default_value' => $address_format->getPostalCodePattern()];
     $form['postalCodePrefix'] = ['#type' => 'textfield', '#title' => $this->t('Postal code prefix'), '#description' => $this->t('Added to postal codes when formatting an address for international mailing.'), '#default_value' => $address_format->getPostalCodePrefix(), '#size' => 5];
     $form['postalCodeType'] = ['#type' => 'select', '#title' => $this->t('Postal code type'), '#default_value' => $address_format->getPostalCodeType(), '#options' => LabelHelper::getPostalCodeLabels(), '#empty_value' => ''];
     $form['dependentLocalityType'] = ['#type' => 'select', '#title' => $this->t('Dependent locality type'), '#default_value' => $address_format->getDependentLocalityType(), '#options' => LabelHelper::getDependentLocalityLabels(), '#empty_value' => ''];
     $form['localityType'] = ['#type' => 'select', '#title' => $this->t('Locality type'), '#default_value' => $address_format->getLocalityType(), '#options' => LabelHelper::getLocalityLabels(), '#empty_value' => ''];
     $form['administrativeAreaType'] = ['#type' => 'select', '#title' => $this->t('Administrative area type'), '#default_value' => $address_format->getAdministrativeAreaType(), '#options' => LabelHelper::getAdministrativeAreaLabels(), '#empty_value' => ''];
     return $form;
 }
 /**
  * Gets the address values used for rendering.
  *
  * @param \Drupal\address\AddressInterface $address
  *   The address.
  * @param \Drupal\address\Entity\AddressFormatInterface $address_format
  *   The address format.
  *
  * @return array
  *   The values, keyed by address field.
  */
 protected function getValues(AddressInterface $address, AddressFormatInterface $address_format)
 {
     $values = [];
     foreach (AddressField::getAll() as $field) {
         $getter = 'get' . ucfirst($field);
         $values[$field] = $address->{$getter}();
     }
     // Replace the subdivision values with the names of any predefined ones.
     foreach ($address_format->getUsedSubdivisionFields() as $field) {
         if (empty($values[$field])) {
             // This level is empty, so there can be no sublevels.
             break;
         }
         $subdivision = $this->subdivisionRepository->get($values[$field], $address->getLocale());
         if (!$subdivision) {
             // This level has no predefined subdivisions, stop.
             break;
         }
         $values[$field] = $subdivision->getCode();
         if (!$subdivision->hasChildren()) {
             // The current subdivision has no children, stop.
             break;
         }
     }
     return $values;
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public static function defaultFieldSettings()
 {
     return ['available_countries' => [], 'fields' => array_values(AddressField::getAll()), 'langcode_override' => ''] + parent::defaultFieldSettings();
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function setUppercaseFields(array $uppercaseFields)
 {
     AddressField::assertAllExist($uppercaseFields);
     $this->uppercaseFields = $uppercaseFields;
     return $this;
 }
 /**
  * Gets the address values used for rendering.
  *
  * @param \Drupal\address\AddressInterface $address
  *   The address.
  * @param \Drupal\address\Entity\AddressFormatInterface $address_format
  *   The address format.
  *
  * @return array
  *   The values, keyed by address field.
  */
 protected function getValues(AddressInterface $address, AddressFormatInterface $address_format)
 {
     $values = [];
     foreach (AddressField::getAll() as $field) {
         $getter = 'get' . ucfirst($field);
         $values[$field] = $address->{$getter}();
     }
     foreach ($address_format->getUsedSubdivisionFields() as $field) {
         $value = $values[$field];
         // The template needs access to both the subdivision code and name.
         $values[$field] = ['code' => '', 'name' => $value];
         if (empty($value)) {
             // This level is empty, so there can be no sublevels.
             break;
         }
         $subdivision = $this->subdivisionRepository->get($value, $address->getLocale());
         if (!$subdivision) {
             // This level has no predefined subdivisions, stop.
             break;
         }
         // Replace the subdivision values with the predefined ones.
         $values[$field] = ['code' => $subdivision->getCode(), 'name' => $subdivision->getName()];
         if (!$subdivision->hasChildren()) {
             // The current subdivision has no children, stop.
             break;
         }
     }
     return $values;
 }
Exemplo n.º 9
0
 /**
  * Builds the format-specific address elements.
  *
  * @param array $element
  *   The existing form element array.
  * @param array $values
  *   An array of address values, keyed by property name.
  *
  * @return array
  *   The modified form element array containing the format specific elements.
  */
 protected function addressElements(array $element, array $values)
 {
     $address_format = $this->addressFormatRepository->get($values['country_code']);
     $required_fields = $address_format->getRequiredFields();
     $labels = LabelHelper::getFieldLabels($address_format);
     foreach ($address_format->getGroupedFields() as $line_index => $line_fields) {
         if (count($line_fields) > 1) {
             // Used by the #pre_render callback to group fields inline.
             $element['container' . $line_index] = ['#type' => 'container', '#attributes' => ['class' => ['address-container-inline']]];
         }
         foreach ($line_fields as $field_index => $field) {
             $property = FieldHelper::getPropertyName($field);
             $class = str_replace('_', '-', $property);
             $element[$property] = ['#type' => 'textfield', '#title' => $labels[$field], '#default_value' => isset($values[$property]) ? $values[$property] : '', '#required' => in_array($field, $required_fields), '#size' => isset($this->sizeAttributes[$field]) ? $this->sizeAttributes[$field] : 60, '#attributes' => ['class' => [$class], 'autocomplete' => FieldHelper::getAutocompleteAttribute($field)]];
             if (count($line_fields) > 1) {
                 $element[$property]['#group'] = $line_index;
             }
         }
     }
     // Hide the label for the second address line.
     if (isset($element['address_line2'])) {
         $element['address_line2']['#title_display'] = 'invisible';
     }
     // Hide fields that have been disabled in the address field settings.
     $enabled_fields = array_filter($this->getFieldSetting('fields'));
     $disabled_fields = array_diff(AddressField::getAll(), $enabled_fields);
     foreach ($disabled_fields as $field) {
         $property = FieldHelper::getPropertyName($field);
         $element[$property]['#access'] = FALSE;
     }
     // Add predefined options to the created subdivision elements.
     $element = $this->processSubdivisionElements($element, $values, $address_format);
     return $element;
 }
 /**
  * Extracts the address values.
  *
  * @param AddressInterface $address The address.
  *
  * @return array An array of values keyed by field constants.
  */
 protected function extractAddressValues(AddressInterface $address)
 {
     $values = [];
     foreach (AddressField::getAll() as $field) {
         $getter = 'get' . ucfirst($field);
         $values[$field] = $address->{$getter}();
     }
     return $values;
 }
Exemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
 {
     $fields = self::entityKeysBaseFieldDefinitions($entity_type);
     $fields['type'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('Type'))->setDescription(t('The store type.'))->setSetting('target_type', 'commerce_store_type')->setReadOnly(TRUE);
     $fields['uid'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('Owner'))->setDescription(t('The store owner.'))->setDefaultValueCallback('Drupal\\commerce_store\\Entity\\Store::getCurrentUserId')->setSetting('target_type', 'user')->setDisplayOptions('form', ['type' => 'entity_reference_autocomplete', 'weight' => 50]);
     $fields['name'] = BaseFieldDefinition::create('string')->setLabel(t('Name'))->setDescription(t('The store name.'))->setRequired(TRUE)->setTranslatable(TRUE)->setSettings(['default_value' => '', 'max_length' => 255])->setDisplayOptions('form', ['type' => 'string_textfield', 'weight' => 0])->setDisplayConfigurable('view', TRUE)->setDisplayConfigurable('form', TRUE);
     $fields['mail'] = BaseFieldDefinition::create('email')->setLabel(t('Email'))->setDescription(t('Store email notifications are sent from this address.'))->setRequired(TRUE)->setDisplayOptions('form', ['type' => 'email_default', 'weight' => 1])->setSetting('display_description', TRUE)->setDisplayConfigurable('view', TRUE)->setDisplayConfigurable('form', TRUE);
     $fields['default_currency'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('Default currency'))->setDescription('The default currency of the store.')->setCardinality(1)->setRequired(TRUE)->setSetting('target_type', 'commerce_currency')->setSetting('handler', 'default')->setTranslatable(TRUE)->setDisplayOptions('form', ['type' => 'options_select', 'weight' => 2])->setDisplayConfigurable('view', TRUE)->setDisplayConfigurable('form', TRUE);
     // Disable the recipient and organization fields on the store address.
     $disabled_fields = [AddressField::RECIPIENT, AddressField::ORGANIZATION];
     $fields['address'] = BaseFieldDefinition::create('address')->setLabel(t('Address'))->setDescription(t('The store address.'))->setCardinality(1)->setRequired(TRUE)->setSetting('fields', array_diff(AddressField::getAll(), $disabled_fields))->setDisplayOptions('form', ['type' => 'address_default', 'settings' => [], 'weight' => 3])->setDisplayConfigurable('view', TRUE)->setDisplayConfigurable('form', TRUE);
     $fields['billing_countries'] = BaseFieldDefinition::create('list_string')->setLabel(t('Supported billing countries'))->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)->setSetting('allowed_values_function', ['\\Drupal\\commerce_store\\Entity\\Store', 'getAvailableCountries'])->setDisplayOptions('form', ['type' => 'options_select', 'weight' => 4])->setDisplayConfigurable('view', TRUE)->setDisplayConfigurable('form', TRUE);
     return $fields;
 }
 /**
  * @covers \CommerceGuys\Addressing\Validator\Constraints\AddressFormatValidator
  *
  * @uses \CommerceGuys\Addressing\Repository\AddressFormatRepository
  * @uses \CommerceGuys\Addressing\Repository\SubdivisionRepository
  * @uses \CommerceGuys\Addressing\Repository\DefinitionTranslatorTrait
  * @uses \CommerceGuys\Addressing\Model\Address
  * @uses \CommerceGuys\Addressing\Model\AddressFormat
  * @uses \CommerceGuys\Addressing\Model\FormatStringTrait
  * @uses \CommerceGuys\Addressing\Model\Subdivision
  * @uses \CommerceGuys\Addressing\Collection\LazySubdivisionCollection
  */
 public function testConstraintFields()
 {
     $allFields = AddressField::getAll();
     $this->constraint->fields = array_diff($allFields, [AddressField::RECIPIENT]);
     $address = new Address();
     $address = $address->withCountryCode('CN')->withAdministrativeArea('CN-11')->withLocality('CN-11-30524e')->withPostalCode('123456')->withAddressLine1('Yitiao Lu');
     $this->validator->validate($address, $this->constraint);
     $this->assertNoViolation();
     $this->constraint->fields = array_diff($allFields, [AddressField::POSTAL_CODE]);
     $address = $address->withPostalCode('INVALID')->withRecipient('John Smith');
     $this->validator->validate($address, $this->constraint);
     $this->assertNoViolation();
     $this->constraint->fields = array_diff($allFields, [AddressField::ADMINISTRATIVE_AREA]);
     $address = $address->withAdministrativeArea('INVALID')->withPostalCode('123456');
     $this->validator->validate($address, $this->constraint);
     $this->assertNoViolation();
     $address = $address->withAdministrativeArea('CN-11')->withLocality('INVALID');
     $this->validator->validate($address, $this->constraint);
     $this->assertNoViolation();
 }