Index: modules/order/src/Element/ProfileSelect.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- modules/order/src/Element/ProfileSelect.php (revision ) +++ modules/order/src/Element/ProfileSelect.php (revision ) @@ -92,6 +92,83 @@ } } + if (\Drupal::currentUser()->isAuthenticated()) { + + /** + * @var \Drupal\profile\ProfileStorage $profileStorage + */ + $profileStorage = \Drupal::service('entity_type.manager')->getStorage('profile'); + + /** + * @var \Drupal\profile\Entity\Profile[] $profiles + */ + $profiles = $profileStorage->loadMultipleByUser(\Drupal::currentUser()->getAccount(), $element['#default_value']->get('type')->getString()); + $optionsList = array(); + + foreach ($profiles as $profile) { + $profileArray = $profile->toArray(); + if (!empty($profileArray['address'])) { + $optionsList[$profile->id()] = '' + .$profileArray['address'][0]['given_name'].' ' + .$profileArray['address'][0]['family_name'].', ' + .$profileArray['address'][0]['address_line1'].', ' + .$profileArray['address'][0]['locality'].' ' + .$profileArray['address'][0]['postal_code'].' '; + } + } + + // Pass on the parents tree for ajax processing in order to reach the correct form element + if (!empty($optionsList)) { + $form_state->setTemporary(array('parent_forms' => $element['#parents'])); + } + + $optionsList['new'] = 'Add new'; + + $defaultSelectValue = $element['#default_value']->id(); + + // Check if a value has been selected + $tmpValues = $form_state->getValues(); + + foreach ($element['#parents'] as $parent) { + if(!empty($tmpValues[$parent])) { + $tmpValues = $tmpValues[$parent]; + } + } + + if (!empty($tmpValues['profile_select'])) { + // Assign selected value as the default select value + $defaultSelectValue = $tmpValues['profile_select']; + + if ($tmpValues['profile_select'] != 'new') { + // Set the default profile to the selected one + $element['#default_value'] = $profileStorage->load($tmpValues['profile_select']); + } else { + // Create a new default for empty values + $element['#default_value'] = $profileStorage->create(); + } + } + // end check + + + $element['profile_select'] = [ + '#type' => 'select', + '#title' => 'Select profile to reuse', + '#options' => $optionsList, + '#default_value' => $defaultSelectValue, + '#ajax' => [ + 'callback' => '\Drupal\commerce_order\Element\ProfileSelect::setPreselectedProfileAjax', + 'event' => 'change', + 'wrapper' => $element['#id'], + 'progress' => array( + 'type' => 'throbber', + 'message' => 'Selecting profile' + ), + ], + ]; + } + + // TODO: Prepopulate the form fields with values from $element['#default_value'] + $element['#profile'] = $element['#default_value']; $form_display = EntityFormDisplay::collectRenderDisplay($element['#profile'], 'default'); $form_display->buildForm($element['#profile'], $element, $form_state); @@ -144,4 +221,17 @@ $element['#profile']->save(); } + public function setPreselectedProfileAjax(array $element, FormStateInterface $form_state){ + // Set default return element + $returnElement = $element; + $parentForms = $form_state->getTemporaryValue('parent_forms'); + // If there are parents, go down the tree until the payment select element reached + if (!empty($parentForms)) { + foreach ($parentForms as $parentForm) { + $returnElement = $returnElement[$parentForm]; + } + } + return $returnElement; + } + }