如何在新标签页中打开链接?

时间:2018-11-03 23:44:44

标签: php forms url drupal drupal-modules

我为Drupal 8创建了一个自定义模块

我希望在新标签页中打开链接,但这不起作用。

但是我添加了['attributes' => ['target' => '_blank']]

为什么不起作用?

<?php

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\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) {
    $pane_form['cgv'] = [
      '#type' => 'checkbox',
      '#default_value' => FALSE,
      '#title' => $this->t('I have read and accept <a href="@cgv">the general terms and conditions of business</a>.', ['@cgv' => Url::fromRoute('entity.commerce_store.canonical', ['commerce_store' => 3], ['attributes' => ['target' => '_blank']])->toString()]),
      '#required' => TRUE,
      '#weight' => $this->getWeight(),
    ];
    return $pane_form;
  }

}

1 个答案:

答案 0 :(得分:0)

其正常的URL :: fromRoute返回路径,而不是链接 您有2个解决方案:

1-使用Link

$options = ['absolute' => TRUE, 'attributes' => ['target' => '_blank']];
$link_object = Drupal\Core\Link::createFromRoute(t('the general terms and conditions of business'),
    'entity.node.canonical', ['node' => "123"],
    $options);
$link = $link_object->toString();

和结果:<a href="http://exemple.dev/en/node/123" target="_blank">the general terms and conditions of business</a>

2-使用URL

'#title' => $this->t('I have read and accept <a href="@cgv" target="_blank">the general terms and conditions of business</a>.', ['@cgv' => Url::fromRoute('entity.commerce_store.canonical', ['commerce_store' => 3], ['absolute' => true])->toString()]),

我希望这会有所帮助。