/**
  * {@inheritdoc}
  */
 public function importTranslations(array $langcodes)
 {
     $available_translations = $this->getAvailableTranslations();
     $available_translations = array_intersect_key($available_translations, array_flip($langcodes));
     foreach ($available_translations as $langcode => $country_codes) {
         $address_formats = $this->storage->loadMultiple($country_codes);
         foreach ($address_formats as $country_code => $address_format) {
             $external_translation = $this->externalRepository->get($country_code, $langcode);
             $config_name = $address_format->getConfigDependencyName();
             $config_translation = $this->languageManager->getLanguageConfigOverride($langcode, $config_name);
             $config_translation->set('format', $external_translation->getFormat());
             $config_translation->save();
         }
     }
 }
예제 #2
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;
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$value instanceof AddressInterface) {
         throw new UnexpectedTypeException($value, 'AddressInterface');
     }
     $address = $value;
     $countryCode = $address->getCountryCode();
     if ($countryCode === null || $countryCode === '') {
         return;
     }
     $values = $this->extractAddressValues($address);
     $addressFormat = $this->addressFormatRepository->get($address->getCountryCode());
     $this->validateFields($values, $addressFormat, $constraint);
     $subdivisions = $this->validateSubdivisions($values, $addressFormat, $constraint);
     $this->validatePostalCode($address->getPostalCode(), $subdivisions, $addressFormat, $constraint);
 }
 /**
  * {@inheritdoc}
  */
 public function format(AddressInterface $address)
 {
     $countryCode = $address->getCountryCode();
     $addressFormat = $this->addressFormatRepository->get($countryCode, $address->getLocale());
     $formatString = $addressFormat->getFormat();
     // Add the country to the bottom or the top of the format string,
     // depending on whether the format is minor-to-major or major-to-minor.
     if (strpos($formatString, AddressField::ADDRESS_LINE1) < strpos($formatString, AddressField::ADDRESS_LINE2)) {
         $formatString .= "\n" . '%country';
     } else {
         $formatString = '%country' . "\n" . $formatString;
     }
     $view = $this->buildView($address, $addressFormat);
     $view = $this->renderView($view);
     // Insert the rendered elements into the format string.
     $replacements = [];
     foreach ($view as $key => $element) {
         $replacements['%' . $key] = $element;
     }
     $output = strtr($formatString, $replacements);
     $output = $this->cleanupOutput($output);
     if (!empty($this->options['html'])) {
         $output = nl2br($output, false);
         // Add the HTML wrapper element.
         $attributes = $this->renderAttributes($this->options['html_attributes']);
         $prefix = '<' . $this->options['html_tag'] . ' ' . $attributes . '>' . "\n";
         $suffix = "\n" . '</' . $this->options['html_tag'] . '>';
         $output = $prefix . $output . $suffix;
     }
     return $output;
 }
 /**
  * Builds the address form for the provided country code.
  *
  * @param string $countryCode        The country code.
  * @param string $administrativeArea The administrative area.
  * @param string $locality           The locality.
  */
 protected function buildForm($form, $countryCode, $administrativeArea, $locality)
 {
     $addressFormat = $this->addressFormatRepository->get($countryCode);
     // A list of needed subdivisions and their parent ids.
     $subdivisions = [AddressField::ADMINISTRATIVE_AREA => 0];
     if (!empty($administrativeArea)) {
         $subdivisions[AddressField::LOCALITY] = $administrativeArea;
     }
     if (!empty($locality)) {
         $subdivisions[AddressField::DEPENDENT_LOCALITY] = $locality;
     }
     $fields = $this->getFormFields($addressFormat, $subdivisions);
     foreach ($fields as $field => $fieldOptions) {
         $type = isset($fieldOptions['choices']) ? 'choice' : 'text';
         $form->add($field, $type, $fieldOptions);
     }
 }
 /**
  * Builds a renderable array for a single address item.
  *
  * @param \Drupal\address\AddressInterface $address
  *   The address.
  * @param string $langcode
  *   The language that should be used to render the field.
  *
  * @return array
  *   A renderable array.
  */
 protected function viewElement(AddressInterface $address, $langcode)
 {
     $country_code = $address->getCountryCode();
     $countries = $this->countryRepository->getList();
     $address_format = $this->addressFormatRepository->get($country_code, $address->getLocale());
     $values = $this->getValues($address, $address_format);
     $element = ['#theme' => 'address_plain', '#recipient' => $values['recipient'], '#organization' => $values['organization'], '#address_line1' => $values['addressLine1'], '#address_line2' => $values['addressLine2'], '#postal_code' => $values['postalCode'], '#sorting_code' => $values['sortingCode'], '#administrative_area' => $values['administrativeArea'], '#locality' => $values['locality'], '#dependent_locality' => $values['dependentLocality'], '#country' => ['code' => $country_code, 'name' => $countries[$country_code]], '#cache' => ['contexts' => ['languages:' . LanguageInterface::TYPE_INTERFACE]]];
     return $element;
 }
 /**
  * Builds a renderable array for a single address item.
  *
  * @param \Drupal\address\AddressInterface $address
  *   The address.
  * @param string $langcode
  *   The language that should be used to render the field.
  *
  * @return array
  *   A renderable array.
  */
 protected function viewElement(AddressInterface $address, $langcode)
 {
     $country_code = $address->getCountryCode();
     $countries = $this->countryRepository->getList();
     $address_format = $this->addressFormatRepository->get($country_code, $address->getLocale());
     $values = $this->getValues($address, $address_format);
     $element = [];
     $element['address_format'] = ['#type' => 'value', '#value' => $address_format];
     $element['country_code'] = ['#type' => 'value', '#value' => $country_code];
     $element['country'] = ['#type' => 'html_tag', '#tag' => 'span', '#attributes' => ['class' => ['country']], '#value' => Html::escape($countries[$country_code]), '#placeholder' => '%country'];
     foreach ($address_format->getUsedFields() as $field) {
         $property = FieldHelper::getPropertyName($field);
         $class = str_replace('_', '-', $property);
         $element[$property] = ['#type' => 'html_tag', '#tag' => 'span', '#attributes' => ['class' => [$class]], '#value' => Html::escape($values[$field]), '#placeholder' => '%' . $field];
     }
     return $element;
 }
 /**
  * {@inheritdoc}
  */
 public function buildConfigurationForm(array $form, FormStateInterface $form_state)
 {
     $form = parent::buildConfigurationForm($form, $form_state);
     $values = $form_state->getUserInput();
     if ($values) {
         $values += $this->defaultConfiguration();
     } else {
         $values = $this->configuration;
     }
     $wrapper_id = Html::getUniqueId('zone-members-ajax-wrapper');
     $form += ['#prefix' => '<div id="' . $wrapper_id . '">', '#suffix' => '</div>', '#after_build' => [[get_class($this), 'clearValues']], '#wrapper_id' => $wrapper_id];
     $form['country_code'] = ['#type' => 'select', '#title' => $this->t('Country'), '#options' => $this->countryRepository->getList(), '#default_value' => $values['country_code'], '#limit_validation_errors' => [], '#required' => TRUE, '#ajax' => ['callback' => [get_class($this), 'ajaxRefresh'], 'wrapper' => $wrapper_id]];
     if (!empty($values['country_code'])) {
         $address_format = $this->addressFormatRepository->get($values['country_code']);
         $form = $this->buildSubdivisionElements($form, $values, $address_format);
         $form = $this->buildPostalCodeElements($form, $values, $address_format);
     }
     return $form;
 }