Я создал модуль, но ссылка неверная.
Мой сайт теперь показывает:
/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;
}
}
@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->doGenerate() (ligne 204 de /var/www/www-domaine-com/web/core/lib/Drupal/Core/Routing/UrlGenerator.php).
мне drupal говорит, что это должен быть ['commerce_store' => $store_id], без /cgv; что не то, что вы хотите. Боюсь, что ничем не могу помочь (не знаю о drupal ...)
Как вы определяете маршрут entity.commerce_store.canonical? Похоже, что здесь не было никаких дополнительных параметров.
@misorude api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.p hp /… J'ai Trouvé ceci mais pas de réponse






Поскольку $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: (optional) An associative array of parameter names and values.
Вы не указали имя для параметров 2-го маршрута, поэтому соответствующий ключ массива откатывается к первому доступному числовому индексу, то есть 0, то есть [ '/cgv' ] становится [ 0 => '/cgv' ], и вы не получаете ожидаемую ссылку.
Я думаю (если я правильно понял вашу проблему), вам нужно в первую очередь определить этот конкретный маршрут, обрабатывающий cgv для данного commerce_store, то есть с добавленным /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();
Я понятия не имею о drupal, но это выглядит синтаксически неверным:
['commerce_store' => $store_id, '/cgv']- я думаю, это должен быть['commerce_store' => $store_id. '/cgv'](точка вместо,, чтобы он объединял и не добавлял другое значение массива).