Skip to content

Commit

Permalink
Issue #2430797 by Berdir: Fixed request attribute _system_path was re…
Browse files Browse the repository at this point in the history
…moved.
  • Loading branch information
berdir authored and davereid committed Mar 4, 2015
1 parent ab7d014 commit 8cf99e5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
2 changes: 0 additions & 2 deletions src/Tests/TokenCurrentPageTest.php
Expand Up @@ -35,7 +35,6 @@ function testCurrentPageTokens() {
'[current-page:page-number]' => 1,
'[current-page:query:foo]' => NULL,
'[current-page:query:bar]' => NULL,
'[current-page:query:q]' => 'user/login',
// Deprecated tokens
'[current-page:arg:0]' => 'user',
'[current-page:arg:1]' => 'login',
Expand All @@ -60,7 +59,6 @@ function testCurrentPageTokens() {
'[current-page:page-number]' => 1,
'[current-page:query:foo]' => 'bar',
'[current-page:query:bar]' => NULL,
'[current-page:query:q]' => 'node/1',
// Deprecated tokens
'[current-page:arg:0]' => 'node',
'[current-page:arg:1]' => 1,
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/TokenMenuTest.php
Expand Up @@ -34,7 +34,7 @@ function testMenuTokens() {
// Add a root link.
/** @var \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent $root_link */
$root_link = entity_create('menu_link_content', array(
'link' => ['uri' => 'user-path:/admin'],
'link' => ['uri' => 'internal:/admin'],
'title' => 'Administration',
'menu_name' => 'main-menu',
));
Expand All @@ -43,7 +43,7 @@ function testMenuTokens() {
// Add another link with the root link as the parent.
/** @var \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent $parent_link */
$parent_link = entity_create('menu_link_content', array(
'link' => ['uri' => 'user-path:/admin/config'],
'link' => ['uri' => 'internal:/admin/config'],
'title' => 'Configuration',
'menu_name' => 'main-menu',
'parent' => $root_link->getPluginId(),
Expand Down
4 changes: 2 additions & 2 deletions token.pages.inc
Expand Up @@ -111,10 +111,10 @@ function theme_token_tree($variables) {
}

$element = array(
'#cache' => array(
/*'#cache' => array(
'cid' => 'tree-rendered:' . hash('sha256', serialize(array('token_types' => $token_types, 'global_types' => NULL) + $variables)),
'tags' => array(Token::TOKEN_INFO_CACHE_TAG),
),
),*/
);

// @todo Find a way to use the render cache for this.
Expand Down
40 changes: 17 additions & 23 deletions token.tokens.inc
Expand Up @@ -381,12 +381,12 @@ function token_tokens($type, array $tokens, array $data = array(), array $option
$replacements = array();

$url_options = array('absolute' => TRUE);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
if (isset($options['langcode'])) {
$url_options['language'] = language_load($options['langcode']);
$langcode = $options['langcode'];
}
else {
$language_code = NULL;
$langcode = NULL;
}

$sanitize = !empty($options['sanitize']);
Expand All @@ -399,7 +399,7 @@ function token_tokens($type, array $tokens, array $data = array(), array $option
$date_format_types = \Drupal::entityManager()->getStorage('date_format')->loadMultiple();
foreach ($tokens as $name => $original) {
if (isset($date_format_types[$name]) && _token_module('date', $name) == 'token') {
$replacements[$original] = format_date($date, $name, '', NULL, $language_code);
$replacements[$original] = format_date($date, $name, '', NULL, $langcode);
}
}
}
Expand Down Expand Up @@ -697,9 +697,7 @@ function token_tokens($type, array $tokens, array $data = array(), array $option
$replacements[$original] = $sanitize ? $title : String::decodeEntities($title);
break;
case 'url':
// Strip the query string from the result of Request::getUri().
list($uri) = explode('?', \Drupal::request()->getUri());
$replacements[$original] = Url::fromUri($uri, $url_options)->toString();
$replacements[$original] = Url::fromRoute('<current>', [], $url_options)->toString();
break;
case 'page-number':
if ($page = \Drupal::request()->query->get('page')) {
Expand All @@ -715,7 +713,7 @@ function token_tokens($type, array $tokens, array $data = array(), array $option
// @deprecated
// [current-page:arg] dynamic tokens.
if ($arg_tokens = \Drupal::token()->findWithPrefix($tokens, 'arg')) {
$path = \Drupal::request()->attributes->get('_system_path');
$path = ltrim(\Drupal::service('path.current')->getPath(), '/');
// Make sure its a system path.
$path = \Drupal::service('path.alias_manager')->getPathByAlias($path);
foreach ($arg_tokens as $name => $original) {
Expand All @@ -734,16 +732,12 @@ function token_tokens($type, array $tokens, array $data = array(), array $option
$value = \Drupal::request()->query->get($name);
$replacements[$original] = $sanitize ? String::checkPlain($value) : $value;
}
elseif ($name == 'q') {
$replacements[$original] = \Drupal::request()->attributes->get('_system_path');
}
}
}

// Chained token relationships.
if ($url_tokens = \Drupal::token()->findWithPrefix($tokens, 'url')) {
$request = \Drupal::request();
$url = new Url($request->attributes->get(RouteObjectInterface::ROUTE_OBJECT), $request->attributes->get('_raw_variables')->all());
$url = Url::fromRoute('<current>');
$replacements += \Drupal::token()->generate('url', $url_tokens, array('url' => $url), $options);
}
}
Expand All @@ -759,12 +753,12 @@ function token_tokens($type, array $tokens, array $data = array(), array $option
foreach ($tokens as $name => $original) {
switch ($name) {
case 'path':
$value = !($url->getOption('alias')) ? \Drupal::service('path.alias_manager')->getAliasByPath($path, $language_code) : $path;
$value = !($url->getOption('alias')) ? \Drupal::service('path.alias_manager')->getAliasByPath($path, $langcode) : $path;
$replacements[$original] = $sanitize ? String::checkPlain($value) : $value;
break;
case 'alias':
// @deprecated
$alias = \Drupal::service('path.alias_manager')->getAliasByPath($path, $language_code);
$alias = \Drupal::service('path.alias_manager')->getAliasByPath($path, $langcode);
$replacements[$original] = $sanitize ? String::checkPlain($alias) : $alias;
break;
case 'absolute':
Expand All @@ -781,7 +775,7 @@ function token_tokens($type, array $tokens, array $data = array(), array $option
$replacements[$original] = $unaliased->setAbsolute()->setOption('alias', TRUE)->toString();
break;
case 'args':
$value = !($url->getOption('alias')) ? \Drupal::service('path.alias_manager')->getAliasByPath($path, $language_code) : $path;
$value = !($url->getOption('alias')) ? \Drupal::service('path.alias_manager')->getAliasByPath($path, $langcode) : $path;
$replacements[$original] = token_render_array(explode('/', $value), $options);
break;

Expand All @@ -790,7 +784,7 @@ function token_tokens($type, array $tokens, array $data = array(), array $option

// [url:args:*] chained tokens.
if ($arg_tokens = \Drupal::token()->findWithPrefix($tokens, 'args')) {
$value = !($url->getOption('alias')) ? \Drupal::service('path.alias_manager')->getAliasByPath($path, $language_code) : $path;
$value = !($url->getOption('alias')) ? \Drupal::service('path.alias_manager')->getAliasByPath($path, $langcode) : $path;
$replacements += \Drupal::token()->generate('array', $arg_tokens, array('array' => explode('/', $value)), $options);
}

Expand Down Expand Up @@ -1052,12 +1046,12 @@ function menu_ui_tokens($type, $tokens, array $data = array(), array $options =
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');

$url_options = array('absolute' => TRUE);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->getId();
if (isset($options['langcode'])) {
$url_options['language'] = language_load($options['langcode']);
$langcode = $options['langcode'];
}
else {
$language_code = NULL;
$langcode = NULL;
}

$sanitize = !empty($options['sanitize']);
Expand Down Expand Up @@ -1248,7 +1242,7 @@ function _token_field_label($entity_type, $field_name) {
function field_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
$langcode = isset($options['language']) ? $options['language']->language : NULL;
$langcode = isset($options['langcode']) ? $options['langcode'] : NULL;

// Entity tokens.
if ($type == 'entity' && !empty($data['entity_type']) && !empty($data['entity']) && !empty($data['token_type'])) {
Expand Down

0 comments on commit 8cf99e5

Please sign in to comment.