commit d320b411243928333343b8d780572a1e3335f4c5 Author: Lee Rowlands Date: Fri Oct 10 07:12:01 2014 +1000 Adds route tokens diff --git a/core/modules/system/src/Tests/Token/RouteTokensTest.php b/core/modules/system/src/Tests/Token/RouteTokensTest.php new file mode 100644 index 0000000..134daa1 --- /dev/null +++ b/core/modules/system/src/Tests/Token/RouteTokensTest.php @@ -0,0 +1,51 @@ +installSchema('system', array('router', 'sequences')); + $this->installConfig(array('core')); + \Drupal::service('router.builder')->rebuild(); + + } + + /** + * Tests that default route tokens work. + */ + public function testRouteTokens() { + $text = 'This should Link to admin'; + $replaced = \Drupal::token()->replace($text); + $this->assertTrue(strpos($replaced, 'replace($text); + $this->assertTrue(strpos($replaced, '[route:system.nonexistant]') !== FALSE); + } + +} diff --git a/core/modules/system/system.tokens.inc b/core/modules/system/system.tokens.inc index ff78335..5c11d63 100644 --- a/core/modules/system/system.tokens.inc +++ b/core/modules/system/system.tokens.inc @@ -9,6 +9,7 @@ use Drupal\Component\Utility\String; use Drupal\Component\Utility\Xss; +use Drupal\Core\Url; /** * Implements hook_token_info(). @@ -22,7 +23,10 @@ function system_token_info() { 'name' => t("Dates"), 'description' => t("Tokens related to times and dates."), ); - + $types['route'] = array( + 'name' => t("Route information"), + 'description' => t("Tokens for route names."), + ); // Site-wide global tokens. $site['name'] = array( 'name' => t("Name"), @@ -74,12 +78,30 @@ function system_token_info() { 'name' => t("Raw timestamp"), 'description' => t("A date in UNIX timestamp format (%date)", array('%date' => REQUEST_TIME)), ); + $routes['route'] = array( + 'name' => t('Route URLs'), + 'description' => t('A route URL, enter your token in route:route_name format eg route:entity.canonical.node. Route parameters are not supported.'), + ); + + $routes = array(); + /* @var \Symfony\Component\Routing\Route $route */ + foreach (\Drupal::service('router.route_provider')->getAllRoutes() as $route_name => $route) { + if (strpos($route->getPath(), '}') !== FALSE) { + // Route tokens don't support route parameters. + continue; + } + $routes[$route_name] = array( + 'name' => t('URL to !route_name', ['!route_name' => $route_name]), + 'description' => t('The URL to the !route_name route.', ['!route_name' => $route_name]), + ); + } return array( 'types' => $types, 'tokens' => array( 'site' => $site, 'date' => $date, + 'route' => $routes, ), ); } @@ -116,7 +138,8 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a break; case 'mail': - $replacements[$original] = \Drupal::config('system.site')->get('mail'); + $replacements[$original] = \Drupal::config('system.site') + ->get('mail'); break; case 'url': @@ -124,7 +147,10 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a break; case 'url-brief': - $replacements[$original] = preg_replace(array('!^https?://!', '!/$!'), '', \Drupal::url('', array(), $url_options)); + $replacements[$original] = preg_replace(array( + '!^https?://!', + '!/$!' + ), '', \Drupal::url('', array(), $url_options)); break; case 'login-url': @@ -173,5 +199,20 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a } } + elseif ($type == 'route') { + foreach ($tokens as $route_name => $original) { + try { + $url = Url::fromRoute($route_name)->toString(); + } + catch (\Exception $e) { + // Invalid route, do nothing. + $url = FALSE; + } + if ($url) { + $replacements[$original] = $url; + } + } + } + return $replacements; }