megclaypool
1/29/2020 - 3:49 AM

Get Drupal node url and path in php and twig

[Get Drupal node url and path in php and twig]

/**
 * Implements hook_preprocess_node().
 */
function radicati_d8_preprocess_node(array &$variables) {
 
  if ($variables['node'] instanceof \Drupal\node\NodeInterface) {
    // Make full url availabile to templates
    $variables['node']->full_url = url('node/' . $node->nid);
    
    // Make relative path availabile to templates
    $variables['node']->relative_path = path('node/' . $node->nid);
  }
}
{# node id #}
{{ node.id }}

{# relative path #}
{{ path('entity.node.canonical', {'node': node.id}) }}

{# absolute url #}
{{ url('entity.node.canonical', {'node': node.id}) }}