View source
<?php
namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Utility\Token;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CompletionMessage extends CheckoutPaneBase {
protected $token;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow = NULL) {
$instance = new static($configuration, $plugin_id, $plugin_definition, $checkout_flow, $container
->get('entity_type.manager'));
$instance
->setToken($container
->get('token'));
return $instance;
}
public function setToken(Token $token) {
$this->token = $token;
}
public function defaultConfiguration() {
return [
'message' => [
'value' => "Your order number is [commerce_order:order_number].\r\nYou can view your order on your account page when logged in.",
'format' => 'plain_text',
],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['message'] = [
'#type' => 'text_format',
'#title' => $this
->t('Message'),
'#description' => $this
->t('Shown the end of checkout, after the customer has placed their order.'),
'#default_value' => $this->configuration['message']['value'],
'#format' => $this->configuration['message']['format'],
'#element_validate' => [
'token_element_validate',
],
'#token_types' => [
'commerce_order',
],
'#required' => TRUE,
];
$form['token_help'] = [
'#theme' => 'token_tree_link',
'#token_types' => [
'commerce_order',
],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['message'] = $values['message'];
}
}
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$message = $this->token
->replace($this->configuration['message']['value'], [
'commerce_order' => $this->order,
]);
$pane_form['message'] = [
'#theme' => 'commerce_checkout_completion_message',
'#order_entity' => $this->order,
'#message' => [
'#type' => 'processed_text',
'#text' => $message,
'#format' => $this->configuration['message']['format'],
],
];
return $pane_form;
}
}