/**
  * 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;
 }
예제 #2
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;
 }