0

I created a custom module for Drupal 8.6, and I want to post a link to my terms and conditions.

This is the code I am using.

namespace Drupal\commerce_agree_cgv\Plugin\Commerce\CheckoutPane;

use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\commerce_cart\Controller\CartController;
use Drupal\node\Entity\Node;
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) {
    $link = Link::createFromRoute($this->t('my terms'), 'entity.node.canonical', ['node' => 1], ['attributes' => ['target' => '_blank']])->toString();
    $pane_form['cgv'] = [
      '#type' => 'checkbox',
      '#default_value' => FALSE,
      '#title' => $this->t('I have read and accept', ['the general terms and conditions of business' => $link]),
      '#required' => TRUE,
      '#weight' => $this->getWeight(),
    ];
    return $pane_form;
  }

}

The link does not work. When I go to my page, the link is missing.

enter image description here

The server runs on PHP 7.2.

How do I display the link?

0

The code is not calling $this->t() passing the correct arguments. The correct code would be the following one.

$pane_form['cgv'] = [
  '#type' => 'checkbox',
  '#default_value' => FALSE,
  '#title' => $this->t('I have read and accept <a href=":general-terms">the general terms and conditions of business</a>', [':general-terms' => Url::fromRoute('entity.node.canonical', ['node' => 1], ['attributes' => ['target' => '_blank']])->toString()]),
  '#required' => TRUE,
  '#weight' => $this->getWeight(),
];
  • Thank you, this shows a link but it directs to the current page /checkout/17/review – user90334 Nov 3 '18 at 19:58
  • Why the path is not /node /1 ? – user90334 Nov 3 '18 at 20:31
  • In the logs there is Warning: strpos() expects parameter 1 to be string, object given in Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() (line 362 of /var/www/www-domaine-com/web/core/lib/Drupal/Component/Utility/UrlHelper.php) and Warning: htmlspecialchars() expects parameter 1 to be string, object given in Drupal\Component\Utility\Html::escape() (line 424 of /var/www/www-domaine-com/web/core/lib/Drupal/Component/Utility/Html.php) – user90334 Nov 3 '18 at 21:53
  • I edited your answer, now it works. But the link does not open in a new tab. Why ? I would like to open it in a new tab or in a modal window. – user90334 Nov 3 '18 at 22:25
  • You should update your answer, or ask a new one. – Stefanos Petrakis Nov 3 '18 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy