View source
<?php
namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane;
use Drupal\commerce\InlineFormManager;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BillingInformation extends CheckoutPaneBase implements CheckoutPaneInterface {
protected $inlineFormManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow, EntityTypeManagerInterface $entity_type_manager, InlineFormManager $inline_form_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager);
$this->inlineFormManager = $inline_form_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $checkout_flow, $container
->get('entity_type.manager'), $container
->get('plugin.manager.commerce_inline_form'));
}
public function buildPaneSummary() {
$summary = [];
if ($profile = $this->order
->getBillingProfile()) {
$profile_view_builder = $this->entityTypeManager
->getViewBuilder('profile');
$summary = $profile_view_builder
->view($profile, 'default');
}
return $summary;
}
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$profile = $this->order
->getBillingProfile();
if (!$profile) {
$profile_storage = $this->entityTypeManager
->getStorage('profile');
$profile = $profile_storage
->create([
'type' => 'customer',
'uid' => 0,
]);
}
$inline_form = $this->inlineFormManager
->createInstance('customer_profile', [
'profile_scope' => 'billing',
'available_countries' => $this->order
->getStore()
->getBillingCountries(),
'address_book_uid' => $this->order
->getCustomerId(),
'copy_on_save' => FALSE,
], $profile);
$pane_form['profile'] = [
'#parents' => array_merge($pane_form['#parents'], [
'profile',
]),
'#inline_form' => $inline_form,
];
$pane_form['profile'] = $inline_form
->buildInlineForm($pane_form['profile'], $form_state);
return $pane_form;
}
public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
$inline_form = $pane_form['profile']['#inline_form'];
$profile = $inline_form
->getEntity();
$this->order
->setBillingProfile($profile);
}
}