Last active
June 17, 2019 12:54
-
-
Save waako/d1ea5282b28b5f850a9d8d95cc9dad63 to your computer and use it in GitHub Desktop.
Drupal 8: Get Block's parent Node title and full URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\Core\Url; | |
/** | |
* 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