Как открыть ссылку в новой вкладке? - PullRequest
0 голосов
/ 04 ноября 2018

Я создал пользовательский модуль для 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 голосов
/ 04 ноября 2018

Обычный URL :: путь возврата fromRoute, а не ссылка у вас есть 2 решения ей:

1 - использовать Ссылка

$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()]),

Надеюсь, это поможет.

...