MaterioCommerceAgreeTerms.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Drupal\materio_commerce\Plugin\Commerce\CheckoutPane;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
  5. use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
  6. use Drupal\node\Entity\Node;
  7. use Drupal\Core\Link;
  8. /**
  9. * Provides the completion message pane.
  10. *
  11. * @CommerceCheckoutPane(
  12. * id = "materio_commerce_agree_terms",
  13. * label = @Translation("Agree to the terms and conditions"),
  14. * default_step = "review",
  15. * )
  16. */
  17. class MaterioCommerceAgreeTerms extends CheckoutPaneBase implements CheckoutPaneInterface {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function defaultConfiguration() {
  22. return [
  23. 'path' => NULL,
  24. 'link_text' => 'Terms and Conditions',
  25. 'prefix_text' => 'I agree with the %terms',
  26. 'invalid_text' => 'You must agree with the %terms before continuing',
  27. 'new_window' => 1,
  28. ] + parent::defaultConfiguration();
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function buildConfigurationSummary() {
  34. $prefix = $this->configuration['prefix_text'];
  35. $link_text = $this->configuration['link_text'];
  36. $invalid_text = $this->configuration['invalid_text'];
  37. $new_window = $this->configuration['new_window'];
  38. $path = $this->configuration['path'];
  39. $summary = '';
  40. if (!empty($prefix)) {
  41. $summary = $this->t('Prefix text: @text', ['@text' => $prefix]) . '<br/>';
  42. }
  43. if (!empty($link_text)) {
  44. $summary .= $this->t('Link text: @text', ['@text' => $link_text]) . '<br/>';
  45. }
  46. if (!empty($invalid_text)) {
  47. $summary .= $this->t('Error text: @text', ['@text' => $invalid_text]) . '<br/>';
  48. }
  49. if (!empty($window_target)) {
  50. $window_text = ($new_window === 1) ? $this->t('New window') : $this->t('Same window');
  51. $summary .= $this->t('Window opens in: @opens', ['@text' => $window_text]) . '<br/>';
  52. }
  53. if (!empty($path)) {
  54. $summary .= $this->t('Terms page: @path', ['@path' => $path['route_name']]);
  55. }
  56. return $summary;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  62. $form = parent::buildConfigurationForm($form, $form_state);
  63. $form['prefix_text'] = [
  64. '#type' => 'textfield',
  65. '#title' => $this->t('Prefix text'),
  66. '#default_value' => $this->configuration['prefix_text'],
  67. ];
  68. $form['link_text'] = [
  69. '#type' => 'textfield',
  70. '#title' => $this->t('Link text'),
  71. '#default_value' => $this->configuration['link_text'],
  72. '#required' => TRUE,
  73. ];
  74. $form['invalid_text'] = [
  75. '#type' => 'textfield',
  76. '#title' => $this->t('Invalid text'),
  77. '#default_value' => $this->configuration['invalid_text'],
  78. ];
  79. $form['new_window'] = [
  80. '#type' => 'checkbox',
  81. '#title' => $this->t('Open window link in new window'),
  82. '#default_value' => $this->configuration['new_window'],
  83. ];
  84. if ($this->configuration['path']) {
  85. $path = $this->configuration['path'];
  86. }
  87. else {
  88. $path = NULL;
  89. }
  90. $form['path'] = [
  91. '#type' => 'path',
  92. '#default_value' => $path,
  93. '#required' => TRUE
  94. ];
  95. return $form;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  101. parent::submitConfigurationForm($form, $form_state);
  102. // Todo validate existing path
  103. // Get route name and parameters from path.
  104. // $url_object = \Drupal::service('path.validator')->getUrlIfValid($path);
  105. // $route_name = $url_object->getRouteName();
  106. // $route_parameters = $url_object->getrouteParameters();
  107. if (!$form_state->getErrors()) {
  108. $values = $form_state->getValue($form['#parents']);
  109. $this->configuration['prefix_text'] = $values['prefix_text'];
  110. $this->configuration['link_text'] = $values['link_text'];
  111. $this->configuration['invalid_text'] = $values['invalid_text'];
  112. $this->configuration['new_window'] = $values['new_window'];
  113. $this->configuration['path'] = $values['path'];
  114. }
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
  120. $prefix_text = $this->configuration['prefix_text'];
  121. $link_text = $this->configuration['link_text'];
  122. $path = $this->configuration['path'];
  123. if ($path) {
  124. // $node = Node::load($nid);
  125. $attributes = [];
  126. if ($this->configuration['new_window']) {
  127. $attributes = ['attributes' => ['target' => '_blank']];
  128. }
  129. $link = Link::createFromRoute(
  130. $this->t($link_text),
  131. $path['route_name'],
  132. $path['route_parameters'],
  133. $attributes
  134. )->toString();
  135. if ($prefix_text) {
  136. $pane_form['terms_and_conditions'] = [
  137. '#type' => 'checkbox',
  138. '#default_value' => FALSE,
  139. '#title' => $this->t($prefix_text, ['%terms' => $link]),
  140. '#required' => TRUE,
  141. '#weight' => $this->getWeight(),
  142. ];
  143. }
  144. else {
  145. $pane_form['terms_and_conditions'] = [
  146. '#type' => 'checkbox',
  147. '#default_value' => FALSE,
  148. '#title' => $link,
  149. '#required' => TRUE,
  150. '#weight' => $this->getWeight(),
  151. ];
  152. }
  153. }
  154. return $pane_form;
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  160. $values = $form_state->getValue($pane_form['#parents']);
  161. if (!$values['terms_and_conditions']) {
  162. $form_state->setError($pane_form, $this->configuration['invalid_text']);
  163. }
  164. }
  165. }