Extract Url amp; Title from link field in Drupal 8?(提取网址 amp;Drupal 8 中链接字段的标题?)
问题描述
我正在尝试检索 URL 和 Title 值Drupal 8 中的 rel="noreferrer">链接字段.
I'm trying to retrieve the URL and the Title values of a Link field in Drupal 8.
在我的自定义控制器中,我使用以下命令检索节点:
In my custom controller, I retrieve the nodes with:
$storage = Drupal::entityManager()->getStorage('node');
$nids = $storage->getQuery()
->condition('type', 'partners')
->condition('status', 1)
->execute();
$partners = $storage->loadMultiple($nids);
当我遍历所有节点时,为了预处理我将提供给我的视图的变量,我想检索 URL 和 Title.
When I loop throught all my nodes, to preprocess vars I'll give to my view, I would like to retrieve the URL and the Title.
foreach ($partners as $key => $partner) {
$variables['partners'][] = array(
'image' => $partner->field_logo->entity->url(),
'url' => $partner->field_link->value, // Can't retrieve values of link field
);
}
很遗憾,我没有找到如何检索 URL 和 field_link 的Title.
Unfortunately, I don't found how to retrieve the URL and the Title of field_link.
感谢您的帮助.
推荐答案
在节点级别,您可以在 Twig 模板中使用:
At the node level, inside your Twig template you can use:
{{ content.field_link.0['#url'] }}
&{{ content.field_link.0['#title'] }}
例如:
<a href="{{ content.field_link.0['#url'] }}">{{ content.field_link.0['#title'] }}</a>
field_link
是相关链接字段的名称.
field_link
being the name of the link field in question.
这篇关于提取网址 &Drupal 8 中链接字段的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:提取网址 &Drupal 8 中链接字段的标题?


- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01