|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\commerce_order\Element; |
| 4 | + |
| 5 | +use Drupal\commerce\Element\CommerceElementBase; |
| 6 | +use Drupal\Core\Entity\Entity\EntityFormDisplay; |
| 7 | +use Drupal\Core\Form\FormStateInterface; |
| 8 | +use Drupal\profile\Entity\ProfileInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Provides a form element for selecting a customer profile. |
| 12 | + * |
| 13 | + * Usage example: |
| 14 | + * @code |
| 15 | + * $form['billing_profile'] = [ |
| 16 | + * '#type' => 'commerce_profile_select', |
| 17 | + * '#default_value' => $profile, |
| 18 | + * '#default_country' => 'FR', |
| 19 | + * '#available_countries' => ['US', 'FR'], |
| 20 | + * ]; |
| 21 | + * @endcode |
| 22 | + * To access the profile in validation or submission callbacks, use |
| 23 | + * $form['billing_profile']['#profile']. Due to Drupal core limitations the |
| 24 | + * profile can't be accessed via $form_state->getValue('billing_profile'). |
| 25 | + * |
| 26 | + * @RenderElement("commerce_profile_select") |
| 27 | + */ |
| 28 | +class ProfileSelect extends CommerceElementBase { |
| 29 | + |
| 30 | + /** |
| 31 | + * {@inheritdoc} |
| 32 | + */ |
| 33 | + public function getInfo() { |
| 34 | + $class = get_class($this); |
| 35 | + return [ |
| 36 | + // The country to select if the address widget doesn't have a default. |
| 37 | + '#default_country' => NULL, |
| 38 | + // A list of country codes. If empty, all countries will be available. |
| 39 | + '#available_countries' => [], |
| 40 | + |
| 41 | + // The profile entity operated on. Required. |
| 42 | + '#default_value' => NULL, |
| 43 | + '#process' => [ |
| 44 | + [$class, 'attachElementSubmit'], |
| 45 | + [$class, 'processForm'], |
| 46 | + ], |
| 47 | + '#element_validate' => [ |
| 48 | + [$class, 'validateElementSubmit'], |
| 49 | + [$class, 'validateForm'], |
| 50 | + ], |
| 51 | + '#commerce_element_submit' => [ |
| 52 | + [$class, 'submitForm'], |
| 53 | + ], |
| 54 | + '#theme_wrappers' => ['container'], |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Builds the element form. |
| 60 | + * |
| 61 | + * @param array $element |
| 62 | + * The form element being processed. |
| 63 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 64 | + * The current state of the form. |
| 65 | + * @param array $complete_form |
| 66 | + * The complete form structure. |
| 67 | + * |
| 68 | + * @throws \InvalidArgumentException |
| 69 | + * Thrown when #default_value is empty or not an entity, or when |
| 70 | + * #available_countries is not an array of country codes. |
| 71 | + * |
| 72 | + * @return array |
| 73 | + * The processed form element. |
| 74 | + */ |
| 75 | + public static function processForm(array $element, FormStateInterface $form_state, array &$complete_form) { |
| 76 | + if (empty($element['#default_value'])) { |
| 77 | + throw new \InvalidArgumentException('The commerce_profile_select element requires the #default_value property.'); |
| 78 | + } |
| 79 | + elseif (isset($element['#default_value']) && !($element['#default_value'] instanceof ProfileInterface)) { |
| 80 | + throw new \InvalidArgumentException('The commerce_profile_select #default_value property must be a profile entity.'); |
| 81 | + } |
| 82 | + if (!is_array($element['#available_countries'])) { |
| 83 | + throw new \InvalidArgumentException('The commerce_profile_select #available_countries property must be an array.'); |
| 84 | + } |
| 85 | + |
| 86 | + $element['#profile'] = $element['#default_value']; |
| 87 | + $form_display = EntityFormDisplay::collectRenderDisplay($element['#profile'], 'default'); |
| 88 | + $form_display->buildForm($element['#profile'], $element, $form_state); |
| 89 | + if (!empty($element['address']['widget'][0])) { |
| 90 | + $widget_element = &$element['address']['widget'][0]; |
| 91 | + // Remove the details wrapper from the address widget. |
| 92 | + $widget_element['#type'] = 'container'; |
| 93 | + // Provide a default country. |
| 94 | + if (!empty($element['#default_country']) && empty($widget_element['address']['#default_value']['country_code'])) { |
| 95 | + $widget_element['address']['#default_value']['country_code'] = $element['#default_country']; |
| 96 | + } |
| 97 | + // Limit the available countries. |
| 98 | + if (!empty($element['#available_countries'])) { |
| 99 | + $widget_element['address']['#available_countries'] = $element['#available_countries']; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return $element; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Validates the element form. |
| 108 | + * |
| 109 | + * @param array $element |
| 110 | + * The form element. |
| 111 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 112 | + * The current state of the form. |
| 113 | + * |
| 114 | + * @throws \Exception |
| 115 | + * Thrown if button-level #validate handlers are detected on the parent |
| 116 | + * form, as a protection against buggy behavior. |
| 117 | + */ |
| 118 | + public static function validateForm(array &$element, FormStateInterface $form_state) { |
| 119 | + $form_display = EntityFormDisplay::collectRenderDisplay($element['#profile'], 'default'); |
| 120 | + $form_display->extractFormValues($element['#profile'], $element, $form_state); |
| 121 | + $form_display->validateFormValues($element['#profile'], $element, $form_state); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Submits the element form. |
| 126 | + * |
| 127 | + * @param array $element |
| 128 | + * The form element. |
| 129 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 130 | + * The current state of the form. |
| 131 | + */ |
| 132 | + public static function submitForm(array &$element, FormStateInterface $form_state) { |
| 133 | + $form_display = EntityFormDisplay::collectRenderDisplay($element['#profile'], 'default'); |
| 134 | + $form_display->extractFormValues($element['#profile'], $element, $form_state); |
| 135 | + $element['#profile']->save(); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments