提取网址来自Drupal 8中链接字段的标题? [英] Extract Url & Title from link field in Drupal 8?

查看:85
本文介绍了提取网址来自Drupal 8中链接字段的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索URL 和 Title 值rel = noreferrer> Drupal 8 中的链接字段。

I'm trying to retrieve the URL and the Title values of a Link field in Drupal 8.

在我的自定义控制器中,我使用以下命令检索节点: / p>

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 标题

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中链接字段的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆