【问题标题】:Why are characters inserted in my PHP link?为什么在我的 PHP 链接中插入字符?
【发布时间】:2018-11-05 13:06:38
【问题描述】:

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

我的网站现在显示:

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

}

【问题讨论】:

  • 我对drupal一无所知,但这看起来在语法上是错误的:['commerce_store' =&gt; $store_id, '/cgv'] - 我想它应该是['commerce_store' =&gt; $store_id. '/cgv'](一个点而不是,,所以它会连接和不要添加另一个数组值。)
  • @Jeff 谢谢,如果我用点替换逗号,我有这个错误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-&gt;doGenerate() (ligne 204 de /var/www/www-domaine-com/web/core/lib/Drupal/Core/Routing/UrlGenerator.php).
  • 对我来说,drupal 说它应该是['commerce_store' =&gt; $store_id],没有/cgv;这不是你想要的。恐怕我无法再提供任何帮助(不知道 drupal ......)
  • 你对路由entity.commerce_store.canonical的定义是什么?听起来好像一开始就没有包含任何这样的额外参数。
  • @misorude api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/… J'ai trouvé ceci mais pas de réponse

标签: php drupal drupal-8 drupal-routes


【解决方案1】:

因为$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 =&gt; '/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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2013-12-17
    • 2015-10-02
    相关资源
    最近更新 更多