1

Task: to retrieve a list of nodes in JSON format including their aliases generated by Pathauto module. The configuration of my view is this:

enter image description here

Problem: path fields come empty - they won't include original Drupal path to it nor an alias generated by Pathauto:

Path is an empty array

The current solution I've got is to programmatically amend view's result in the following way:

function my_custom_views_pre_render(\Drupal\views\ViewExecutable $view) {
foreach($view->result as $value) {
    $url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $value->_entity->get('nid')->value], ['absolute' => false]);
    $url = $url->toString();
    $value->_entity->set('path', $url);
}

}

And that populates the path fields:

Path is populated

I'd like to know if there is a way to avoid the hook and make views return node paths without it, or is it normal Drupal Views behaviour?

P.s.: If I chose "Fields" instead of "Entity" in the Format section in the Views UI, it does return me the correct paths. However I want to get the full entity due to extensive data it consists for later consumption. Thanks!

  • did you try my solution? – Yusef Mar 28 '17 at 20:05
0

Put Show on field, then remove "title" field and add Path field

enter image description here

and if you want result be Absolute Url on Rewrite secion Check Use absolute link (begins with "http://")

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.