我尝试找到一种方法,通过节点的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');
。谢谢,我已经在您的问题中找到了答案。 - Reza Bayat