I have a Drupal 8.7 site with the Drupal Commerce 2.14 module.
I created a marketplace (several merchants on the same site). In the store type, I created a "Text (formatted, long)" field with the machine name field_terms_and_conditions
.
In this field, the merchant writes his general conditions of sale, which the customer will have to accept during the order process.
When the customer orders in several stores, this will create several shopping carts (the name of the store is displayed above each shopping cart). It works very well. Now that Drupal Commerce is handling the address book, the orders are very fast.
Currently I have created a view that displays the contents of the field field_terms_and_conditions
on a page. I also created a custom module to display a checkbox with a link to the general conditions of sale in a modal window.
I want to directly render the field field_terms_and_conditions
in the modal window without using a view. How to do this ?
/src/Plugin/Commerce/CheckoutPane/MarketplaceTermsAndConditions.php
:
<?php
namespace Drupal\commerce_marketplace_terms_and_conditions\Plugin\Commerce\CheckoutPane;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Provides the completion message pane.
*
* @CommerceCheckoutPane(
* id = "marketplace_terms_and_conditions",
* label = @Translation("Marketplace Terms and Conditions"),
* default_step = "review",
* )
*/
class MarketplaceTermsAndConditions extends CheckoutPaneBase implements CheckoutPaneInterface {
/**
* {@inheritdoc}
*/
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$store_name = $this->order->getStore()->getName();
$store_id = $this->order->getStoreId();
$pane_form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$attributes = [
'attributes' => [
'class' => 'use-ajax',
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 'auto'
]),
],
];
$link = Link::fromTextAndUrl(
$this->t('terms and conditions of the store "@store_name"', ['@store_name' => $store_name]),
Url::fromUri("internal:/store/$store_id/cgv", $attributes)
)->toString();
$pane_form['marketplace_terms_and_conditions'] = [
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => $this->t('I have read and accept @terms.', ['@terms' => $link]),
'#required' => TRUE,
'#weight' => $this->getWeight(),
];
return $pane_form;
}
}