【发布时间】:2020-09-30 14:21:11
【问题描述】:
我试图找到一种方法来通过节点中的 url 别名的一部分来获取所有节点。我知道我通过完整的 url 别名获得了一个特定的节点。例如:
$path = \Drupal::service('path.alias_manager')->getPathByAlias('/this-is-the-alias');
if(preg_match('/node\/(\d+)/', $path, $matches)) {
$node = \Drupal\node\Entity\Node::load($matches[1]);
}
而且我知道我可以通过实体查询找到多个节点:
$nids = \Drupal::entityQuery('node')
->condition('type','page')
->condition('status',1)
->sort('created', 'DESC')
->range(0, 20)
->execute();
但我想知道如何实现一个附加条件,以通过 URL 别名的一部分过滤节点,例如“/news/*”。我需要所有带有这个 url 片段的节点。
【问题讨论】:
-
服务名称是
path_alias.manager。所以你的问题代码的第一行应该是:$path = \Drupal::service('path_alias.manager')->getPathByAlias('/this-is-the-alias');。谢谢,我在你的问题中找到了答案