如何在新标签页中打开 link?
How to open a link in a new tab?
我为 Drupal 8 创建了一个自定义模块
我想让我的 link 在新标签页中打开,但它不起作用。
但是我添加了['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;
}
}
它是正常的 URL::fromRoute return 路径,而不是 link
你有 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()]),
希望对您有所帮助。
我为 Drupal 8 创建了一个自定义模块
我想让我的 link 在新标签页中打开,但它不起作用。
但是我添加了['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;
}
}
它是正常的 URL::fromRoute return 路径,而不是 link 你有 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()]),
希望对您有所帮助。