Created
August 23, 2017 10:11
-
-
Save gwagroves/148d8dad5a9881849803ff9131a0b2ba to your computer and use it in GitHub Desktop.
Get a Drupal 8 View title and URL from a path.
This file contains 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\views\Views; | |
// Get the View title and URL from a path. | |
$path = '/my-path'; | |
$url = \Drupal::service('path.validator')->getUrlIfValid($path); | |
// Check URL is invalid and has a route. | |
if ($url && $url->isRouted()) { | |
$parts = explode('.', $url->getRouteName()); | |
// Is it a view? | |
if ($parts[0] === 'view') { | |
// Get ViewExectuable. | |
if ($view = Views::getView($parts[1])) { | |
$view->setDisplay($parts[2]); | |
$title = $view->getTitle(); | |
$url = $url->toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment