/**
  * {@inheritdoc}
  */
 protected function addViolation($field, $message, $invalid_value, AddressFormatInterface $address_format)
 {
     $labels = LabelHelper::getFieldLabels($address_format);
     $label = $labels[$field];
     $this->context->buildViolation($message, ['@name' => $label])->atPath(FieldHelper::getPropertyName($field))->setInvalidValue($invalid_value)->addViolation();
 }
 /**
  * Builds the subdivision form elements.
  *
  * @param array $form
  *   The form.
  * @param array $values
  *   The form values.
  * @param \Drupal\address\Entity\AddressFormatInterface $address_format
  *  The address format for the selected country.
  *
  * @return array
  *   The form with the added subdivision elements.
  */
 protected function buildSubdivisionElements(array $form, array $values, AddressFormatInterface $address_format)
 {
     $depth = $this->subdivisionRepository->getDepth($values['country_code']);
     if ($depth === 0) {
         // No predefined data found.
         return $form;
     }
     $labels = LabelHelper::getFieldLabels($address_format);
     $subdivision_fields = $address_format->getUsedSubdivisionFields();
     $current_depth = 1;
     foreach ($subdivision_fields as $index => $field) {
         $property = FieldHelper::getPropertyName($field);
         $parent_property = $index ? FieldHelper::getPropertyName($subdivision_fields[$index - 1]) : NULL;
         if ($parent_property && empty($values[$parent_property])) {
             // No parent value selected.
             break;
         }
         $parent_id = $parent_property ? $values[$parent_property] : NULL;
         $subdivisions = $this->subdivisionRepository->getList($values['country_code'], $parent_id);
         if (empty($subdivisions)) {
             break;
         }
         $form[$property] = ['#type' => 'select', '#title' => $labels[$field], '#options' => $subdivisions, '#default_value' => $values[$property], '#empty_option' => $this->t('- All -')];
         if ($current_depth < $depth) {
             $form[$property]['#ajax'] = ['callback' => [get_class($this), 'ajaxRefresh'], 'wrapper' => $form['#wrapper_id']];
         }
         $current_depth++;
     }
     return $form;
 }
 /**
  * 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;
 }
Exemplo n.º 4
0
 /**
  * Processes the subdivision elements, adding predefined values where found.
  *
  * @param array $element
  *   The existing form element array.
  * @param array $values
  *   An array of address values, keyed by property name.
  * @param \Drupal\address\Entity\AddressFormatInterface $address_format
  *   The address format.
  *
  * @return array
  *   The processed form element array.
  */
 protected function processSubdivisionElements(array $element, array $values, AddressFormatInterface $address_format)
 {
     $depth = $this->subdivisionRepository->getDepth($values['country_code']);
     if ($depth === 0) {
         // No predefined data found.
         return $element;
     }
     $subdivision_properties = [];
     foreach ($address_format->getUsedSubdivisionFields() as $field) {
         $subdivision_properties[] = FieldHelper::getPropertyName($field);
     }
     // Load and insert the subdivisions for each parent id.
     $currentDepth = 1;
     foreach ($subdivision_properties as $index => $property) {
         if (!isset($element[$property]) || !Element::isVisibleElement($element[$property])) {
             break;
         }
         $parent_property = $index ? $subdivision_properties[$index - 1] : NULL;
         if ($parent_property && empty($values[$parent_property])) {
             break;
         }
         $parent_id = $parent_property ? $values[$parent_property] : NULL;
         $subdivisions = $this->subdivisionRepository->getList($values['country_code'], $parent_id);
         if (empty($subdivisions)) {
             break;
         }
         $element[$property]['#type'] = 'select';
         $element[$property]['#options'] = $subdivisions;
         $element[$property]['#empty_value'] = '';
         unset($element[$property]['#size']);
         if ($currentDepth < $depth) {
             $element[$property]['#ajax'] = ['callback' => [get_class($this), 'ajaxRefresh'], 'wrapper' => $element['#wrapper_id']];
         }
         $currentDepth++;
     }
     return $element;
 }