Skip to content

Instantly share code, notes, and snippets.

@waako
Created June 19, 2016 16:48
Show Gist options
  • Save waako/6d4d5dc9cd4c40e9bff4a157e9bb5f9d to your computer and use it in GitHub Desktop.
Save waako/6d4d5dc9cd4c40e9bff4a157e9bb5f9d to your computer and use it in GitHub Desktop.
Drupal 8: get a block's parent node title and absolute url and pass it as variable
<?php
/**
* Implements hook_preprocess_HOOK() for block.html.twig.
*/
function themename_preprocess_block(&$variables) {
// Get Title of Block's parent Node.
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
$variables['node_title'] = $title;
// Get full URL of Block's parent Node.
$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
$current_url = \Drupal\Core\Url::fromUserInput($path_alias, array('absolute' => TRUE))->toString();
$variables['abs_url'] = $current_url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment