Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a site with Drupal 8.6 and Commerce 2.10

How to add an element at the end of the url ?

With the custom module below, the current path is :

https://www.domaine.com/store/3


In the link below, I want to add at the end of the link /cgv

https://www.domaine.com/store/3/cgv

Here is my custom module :

<?php
    
    namespace Drupal\commerce_agree_cgv\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;
    
    /**
     * Provides the completion message pane.
     *
     * @CommerceCheckoutPane(
     *   id = "agree_cgv",
     *   label = @Translation("Agree CGV"),
     *   default_step = "review",
     * )
     */
    class AgreeCGV extends CheckoutPaneBase implements CheckoutPaneInterface {
    
      /**
       * {@inheritdoc}
       */
      public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
        $store_id = 3;
        $pane_form['#attached']['library'][] = 'core/drupal.dialog.ajax';
        $attributes = [
          'attributes' => [
            'class' => 'use-ajax',
            'data-dialog-type' => 'modal',
            'data-dialog-options' => Json::encode([
              'width' => 800,
            ]),
          ],
        ];
        $link = Link::createFromRoute(
          $this->t('the general terms and conditions of business'),
          'entity.commerce_store.canonical',
          ['commerce_store' => $store_id],
          $attributes
        )->toString();
        $pane_form['cgv'] = [
          '#type' => 'checkbox',
          '#default_value' => FALSE,
          '#title' => $this->t('I have read and accept @cgv.', ['@cgv' => $link]),
          '#required' => TRUE,
          '#weight' => $this->getWeight(),
        ];
        return $pane_form;
      }
    
    }


What I have tried:

I think you have to add something to the line below, but I do not know what:

$link = Link::createFromRoute(
  $this->t('the general terms and conditions of business'),
  'entity.commerce_store.canonical',
  ['commerce_store' => $store_id],
  $attributes
)->toString();


If I change with ['commerce_store' => $store_id, '/cgv'] the link is not corect /store/3?0=/cgv


Who can help me ?

If I replace :

['commerce_store' => $store_id, '/cgv'],


by :

['commerce_store' => $store_id. '/cgv'],


I have this error in my logs :

Symfony\Component\Routing\Exception\InvalidParameterException : Parameter "commerce_store" for route "entity.commerce_store.canonical" must match "\d+" ("3/cgv" given) to generate a corresponding URL. dans Drupal\Core\Routing\UrlGenerator->doGenerate() (ligne 204 de /var/www/www-domaine-com/web/core/lib/Drupal/Core/Routing/UrlGenerator.php).
Posted
Updated 5-Nov-18 2:49am
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900