0

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;
  }

}
0

Sounds fun, Here is an option to do this.

1) make a controller : routingfile:

my_module.store_terms:
  path: '/store-terms/{store_id}'
  defaults:
    _controller: '\Drupal\my_module\Controller\StoreTermsController::showTerms'
    _title: 'Terms'
  requirements:
    _permission: 'access content'

In the controller:

use Drupal\Core\Ajax\AjaxResponse;

use Drupal\Core\Ajax\OpenModalDialogCommand;

public function showTerms($store_id) {
  $store = \Drupal::entityTypeManager()->getStorage('commerce_store')->load($store_id);

  $terms = $store->field_terms_and_condition->value;

  $render_array = [
    '#markup' => $terms
  ];
  $response = new AjaxResponse();
  // Show a dialog box.
  $dialogText = $render_array;
  $dialogText['#attached']['library'][] = 'core/drupal.dialog.ajax';
  $response->addCommand(new OpenModalDialogCommand('Terms and Conditions', 
   $dialogText, ['width' => '500']));
  return $response;
}

then back in your buildPaneForm method:

$attributes = [
  'attributes' => [
    'class' => 'use-ajax',
    ],
];
$link = Link::fromTextAndUrl(
  t('terms and conditions of the store "@store_name"', ['@store_name' => 
   $store_name]),
  Url::fromUri("internal:/store-terms/".$store_id , $attributes)
  )->toString();
  • @Targgart Jensen Sorry, I mis explained. I mean how not to create a page ? I just want to display the field in a modal without creating a page. – Mathieu Sep 19 '19 at 0:05
  • thats cool, I have never seen how todo this without creating a controller (or view page) for the ajax call back to work. - but good luck I suppose you could make some hidden div and put the content in there the add js to open a modal and show it ... ect but that is more work then just adding a controller – Taggart Jensen Sep 19 '19 at 0:14
  • OK thanks I will continue to think – Mathieu Sep 19 '19 at 0:43
  • dont think too hard as ajax link is expecting a controller – Taggart Jensen Sep 20 '19 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.