0

我创建了一个模块,但链接不正确。

我的网站现在显示:

 /store/2?0=/cgv

正确的链接应该是:

 /store/2/cgv

为什么它不起作用?错误在哪里?
我应该在下面的代码中更改什么以获得链接?

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

}
4

1 回答 1

0

因为$link没有正确构建:

$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:(可选)参数名称和值的关联数组。

您没有为第二个路由参数指定任何名称,因此相应的数组键回退到第一个可用的数字索引,也就是说0,含义[ '/cgv' ]变为[ 0 => '/cgv' ]并且您没有获得预期的链接。

我认为(如果我正确理解了您的问题)您需要首先定义为给定 commerce_store 处理 cgv 的特定路由,即/cgv附加:

$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);

...这样您就可以根据该特定路线建立链接:

$link = Link::createFromRoute(
  $this->t('the general terms and conditions of business'), 
  'entity.commerce_store.canonical.cgv',
  ['commerce_store' => $store_id],
  $attributes
)->toString();
于 2018-11-07T14:09:02.310 回答