0

I created a module, but the link is not correct.

My site now shows :

 /store/2?0=/cgv

The correct link should be :

 /store/2/cgv

Why doesn't it work ? where is the error ?
What should I change in the code below, to get the link ?

<?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;
use Drupal\Core\Url;

/**
 * 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 = $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' => 800,
        ]),
      ],
    ];
    $link = Link::createFromRoute(
      $this->t('the general terms and conditions of business'),
      'entity.commerce_store.canonical',
      ['commerce_store' => $store_id, '/cgv'],
      $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;
  }

}
EricLavault
  • 12,130
  • 3
  • 23
  • 45
553aa08930
  • 33
  • 7
  • 1
    I have no idea about drupal, but this looks syntacticly wrong: `['commerce_store' => $store_id, '/cgv']` - I guess it should be `['commerce_store' => $store_id. '/cgv']` (a dot instead of the `,`, so that it'll concatanate and not add another array value.) – Jeff Nov 05 '18 at 13:28
  • @Jeff Thank you, if I replace the comma with a dot, I have this error `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).` – 553aa08930 Nov 05 '18 at 13:36
  • to me drupal is saying that it should be `['commerce_store' => $store_id]`, without the `/cgv`; which is not what you want. I'm afraid I cannot help any further (no idea about drupal...) – Jeff Nov 05 '18 at 13:40
  • What is your definition for the route `entity.commerce_store.canonical`? It sounds like that did not include any such extra parameter to begin with. – misorude Nov 05 '18 at 14:04
  • @misorude https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/function/Url%3A%3AfromRoute/8.2.x#comment-64172 J'ai trouvé ceci mais pas de réponse – 553aa08930 Nov 05 '18 at 17:24

1 Answers1

0

Because $link is not built correctly :

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

$route_parameters: (optional) An associative array of parameter names and values.

You did not specify any name for the 2nd route parameters, so the corresponding array key fallback to the first available numeric indice, that is 0, meaning [ '/cgv' ] becomes [ 0 => '/cgv' ] and you don't get the link you expected.

I think (if I understood your issue correctly) what you need is to define in the first place that specific route handling cgv's for a given commerce_store, that is with the /cgv appended :

$route_collection = new RouteCollection();
$route = (new Route('/commerce_store/{commerce_store}/cgv'))
  ->addDefaults([
    '_controller' => $_controller,
    '_title_callback' => $_title_callback,
  ])
  ->setRequirement('commerce_store', '\d+')
  ->setRequirement('_entity_access', 'commerce_store.view');
$route_collection->add('entity.commerce_store.canonical.cgv', $route);

... so that you can build links based on that specific route :

$link = Link::createFromRoute(
  $this->t('the general terms and conditions of business'), 
  'entity.commerce_store.canonical.cgv',
  ['commerce_store' => $store_id],
  $attributes
)->toString();
EricLavault
  • 12,130
  • 3
  • 23
  • 45