使用实体查询获取节点的url路径

时间:2017-07-21 08:46:00

标签: drupal

我试图找出如何通过实体查询获取节点的路径。到目前为止,我已经设法获取节点的标题,但我似乎找到了如何获取节点路径/ url的解决方案。这是我的代码示例

$facultyPostings = $query->get('node')
                           ->condition('status', 1, '=')
                           ->condition('type', 'careers')
                           ->condition('field_career_directory', 'Faculty Postings', '=')
                           ->sort('created')
                           ->execute();

foreach ($facultyPostings as $key => $faculty_postings_careers) {


     $careersNode = _nodeLoad($faculty_postings_careers);



    $variables['faculty_postings'][$key]['title'] = $careersNode->get('title')->value;
    $variables['faculty_postings'][$key]['path'] = $careersNode->get('path')->value;
}

1 个答案:

答案 0 :(得分:2)

您需要使用Drupal 8 core.services.yml文件中提供的服务。所以在循环中你可以使用。

//认为这样会得到nid

$ nid = $ careersNode-> get('id') - > value;

//根据文档

,这个是正确的

$ alias = \ Drupal :: service('path.alias_manager') - > getAliasByPath('/ node /'.$ nid);

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Path%21AliasManager.php/class/AliasManager/8.3.x