php 从 Drupal 8 的链接字段中提取网址和标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34499554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:03:27  来源:igfitidea点击:

Extract Url & Title from link field in Drupal 8?

phpdrupaldrupal-8

提问by Kevin Wenger

I'm trying to retrieve the URLand the Titlevalues of a Link fieldin Drupal 8.

我试图检索URL标题一个值链接字段的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);

When I loop throught all my nodes, to preprocess vars I'll give to my view, I would like to retrieve the URLand the Title.

当我遍历所有节点时,为了预处理我将提供给我的视图的变量,我想检索URLTitle

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
    );
}

Unfortunately, I don't found how to retrieve the URLand the Titleof field_link.

不幸的是,我没有找到如何检索URL标题FIELD_LINK

Thanks for your help.

谢谢你的帮助。

回答by Mark Conroy

At the node level, inside your Twig template you can use:

在节点级别,您可以在 Twig 模板中使用:

{{ content.field_link.0['#url'] }}& {{ content.field_link.0['#title'] }}

{{ content.field_link.0['#url'] }}& {{ content.field_link.0['#title'] }}

For example:

例如:

<a href="{{ content.field_link.0['#url'] }}">{{ content.field_link.0['#title'] }}</a>

<a href="{{ content.field_link.0['#url'] }}">{{ content.field_link.0['#title'] }}</a>

field_linkbeing the name of the link field in question.

field_link是相关链接字段的名称。

回答by Kevin Wenger

I just found the solution ...

我刚刚找到了解决方案...

$partner->field_lien->uri // The url
$partner->field_lien->title // The title

My bad, hope it could help someone.

我的不好,希望它可以帮助某人。

回答by Greg

Just to piggyback on the above, if you have an external link,

只是为了搭载上述内容,如果您有外部链接,

$node->field_name->uri

Will give you the URL, but if it's an internal you may need to tweak a bit more:

会给你 URL,但如果它是内部的,你可能需要稍微调整一下:

use Drupal\Core\Url;

$mylink = Url::fromUri($node->field_name[0]->uri);
$mylink->toString();

回答by Benjen

You can render either the uri or text of a link field directly in the twig template. In the case of a node you can use either of the following within the twig template file (assumes the machine name of your link field is field_link):

您可以直接在树枝模板中呈现链接字段的 uri 或文本。对于节点,您可以在 twig 模板文件中使用以下任一项(假设您的链接字段的机器名称是field_link):

{{ node.field_link.uri }}

{{ node.field_link.title }} 

回答by zanvidmar

This one works for me in twig:

这个在树枝上对我有用:

content.field_link_name.0['#title']        // title
content.field_link_name.0['#url_title']    // url value

*you should use: "Separate link text and URL" widget in display

*您应该使用:显示中的“单独的链接文本和 URL”小部件

回答by Vishal Rao

Updated for Drupal 8

为 Drupal 8 更新

To get the url all you need to do is:

要获取 url,您需要做的就是:

{{ content.field_link_name[0]['#url'] }}

To get the link text:

要获取链接文本:

{{ content.field_link_name[0]['#title'] }}

回答by mel-miller

If you want to do this in a field template instead of a node template do this:

如果要在字段模板而不是节点模板中执行此操作,请执行以下操作:

{% for item in items %}
  {{ item.content['#url'] }}
  {{ item.content['#title'] }}
{% endfor %}

Alternately, if this is not a multi-value field you can just do this instead:

或者,如果这不是多值字段,您可以改为执行以下操作:

{{ items|first.content['#url'] }}
{{ items|first.content['#title'] }}

回答by Kiwad

If you're doing it in a preprocess, I'd suggest to base your code on LinkSeparateFormatter.php. It shows the idea on how to get the title from the link field. Here's a way to do it.

如果您在预处理中执行此操作,我建议将您的代码基于 LinkSeparateFormatter.php。它展示了如何从链接字段获取标题的想法。这是一种方法。

use Drupal\Component\Utility\Unicode;

//...

$field_link = $entity->field_link->first();
$link_url = $field_link->getUrl();
$link_data = $field_link->toArray();
// If you want to truncate the url
$link_title = Unicode::truncate($link_url->toString(), 40, FALSE, TRUE);
if(!empty($link_data['title'])){
  // You could truncate here as well
  $link_title = $link_data['title'];
}
$variables['content'] = [
  '#type' => 'link',
  '#url' => $link_url,
  '#title' => $link_title,
];

回答by Arti Prasad

I am doing this link separation for ECK fields and this solution really helped me. I have updated the code for ECK fields for apply inline style in twig file like this:

我正在为 ECK 字段做这个链接分离,这个解决方案真的帮助了我。我已经更新了 ECK 字段的代码,以便在 twig 文件中应用内联样式,如下所示:

<a style="color: {{ entity.field_link_color[0] }};" href="{{ entity.field_link[0]['#url'] }}"> {{ entity.field_link[0]['#title'] }} </a>

<a style="color: {{ entity.field_link_color[0] }};" href="{{ entity.field_link[0]['#url'] }}"> {{ entity.field_link[0]['#title'] }} </a>

To get url:
{{ entity.field_link[0]['#url'] }}

获取网址:
{{ entity.field_link[0]['#url'] }}

To get link title:
{{ entity.field_link[0]['#title'] }}

获取链接标题:
{{ entity.field_link[0]['#title'] }}

回答by Debasish

After rendering a block if you want to access the link field used in it then you can use like this $render['field_target_url']['#items']->uri inside node preprocess hook.

渲染块后,如果您想访问其中使用的链接字段,则可以像这样使用 $render['field_target_url']['#items']->uri 内部节点预处理钩子。