/**
  * 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;
 }
 /**
  * {@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 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;
 }