drupal8 url link

这篇博客介绍了在Drupal8中如何创建内部路径链接和外部URL链接。通过使用`Url::fromRoute()`和`Drupal::l()`方法,可以方便地生成指向内部管理页面的链接。同时,展示了如何构建外部链接以及如何利用`Link::fromTextAndUrl()`方法创建带有特定样式的项目链接。
摘要由CSDN通过智能技术生成
  1. // Internal path (defined by a route in Drupal 8).
  2. use Drupal\Core\Url;
  3. $url = Url::fromRoute('book.admin');
  4. $internal_link = \Drupal::l(t('Book admin'), $url);
  5.  
  6. // External Uri.
  7. use Drupal\Core\Url;
  8. $url = Url::fromUri('http://www.example.com/');
  9. $external_link = \Drupal::l(t('External link'), $url);
  10.  
  11. ==================================or==================================================
  12. use Drupal\Core\Url;
  13. use Drupal\Core\Link;
  14. $url = Url::fromRoute('entity.node.edit_form', array('node' => NID));
  15. $project_link = Link::fromTextAndUrl(t('Open Project'), $url);
  16. $project_link = $project_link->toRenderable();
  17. // If you need some attributes.
  18. $project_link['#attributes'] = array('class' => array('button', 'button-action', 'button--primary', 'button--small'));

  1. print render($project_link);
https://chromatichq.com/blog/creating-links-within-twig-templates-using-path-and-url
// Link to the default frontpage content listing view:
<a href="{{ path('view.frontpage') }}">{{ 'View all content'|t }}</a>

// Link to a specific node page:
<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">{{ 'Read more'|t }}</a>

// Link to a specific user profile page:
<a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a>

// Link to a view, and throw in some additional query string parameters:
<a href="{{ path('view.articles.page_1', {'page': 2}) }}">{{ 'Go to page 2'|t }}</a>

// Link to a view and pass in some arguments to the view:
<a href="{{ path('view.recent_articles_by_author.page_1', {'arg_0': user.field_display_name.value|drupal_escape }) }}">{{ 'See all the articles written by'|t }} {{ user.field_display_name.value }}</a>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值