Skip to content

Instantly share code, notes, and snippets.

@gwagroves
Created August 23, 2017 10:11
Show Gist options
  • Save gwagroves/148d8dad5a9881849803ff9131a0b2ba to your computer and use it in GitHub Desktop.
Save gwagroves/148d8dad5a9881849803ff9131a0b2ba to your computer and use it in GitHub Desktop.
Get a Drupal 8 View title and URL from a path.
<?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