Commit 8bd63769 authored by webchick's avatar webchick

Issue #2344487 by tim.plunkett, dawehner: Provide special route names for <current> and <none>.

parent 17aac917
......@@ -819,6 +819,14 @@ services:
- { name: path_processor_inbound, priority: 200 }
- { name: path_processor_outbound, priority: 200 }
arguments: ['@config.factory']
path_processor_none:
class: Drupal\Core\PathProcessor\PathProcessorNone
tags:
- { name: path_processor_outbound, priority: 200 }
path_processor_current:
class: Drupal\Core\PathProcessor\PathProcessorCurrent
tags:
- { name: path_processor_outbound, priority: 200 }
path_processor_alias:
class: Drupal\Core\PathProcessor\PathProcessorAlias
tags:
......
......@@ -187,6 +187,16 @@ public static function request() {
return static::$container->get('request_stack')->getCurrentRequest();
}
/**
* Retrives the request stack.
*
* @return \Symfony\Component\HttpFoundation\RequestStack
* The request stack
*/
public static function requestStack() {
return static::$container->get('request_stack');
}
/**
* Retrieves the currently active route match object.
*
......
<?php
/**
* @file
* Contains \Drupal\Core\PathProcessor\PathProcessorCurrent.
*/
namespace Drupal\Core\PathProcessor;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a path processor to replace <current>.
*/
class PathProcessorCurrent implements OutboundPathProcessorInterface {
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = array(), Request $request = NULL) {
if ($path == '%3Ccurrent%3E' && $request) {
$request_uri = $request->getRequestUri();
$current_base_path = $request->getBasePath() . '/';
return substr($request_uri, strlen($current_base_path));
}
return $path;
}
}
<?php
/**
* @file
* Contains \Drupal\Core\PathProcessor\PathProcessorNone.
*/
namespace Drupal\Core\PathProcessor;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a path processor to replace <none>.
*/
class PathProcessorNone implements OutboundPathProcessorInterface {
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = array(), Request $request = NULL) {
if ($path == '%3Cnone%3E') {
return '';
}
return $path;
}
}
......@@ -97,7 +97,7 @@ function contact_mail($key, &$message, $params) {
'!site-name' => \Drupal::config('system.site')->get('name'),
'!subject' => $contact_message->getSubject(),
'!form' => !empty($params['contact_form']) ? $params['contact_form']->label() : NULL,
'!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
'!form-url' => \Drupal::url('<current>', [], ['absolute' => TRUE, 'language' => $language]),
'!sender-name' => user_format_name($sender),
);
if ($sender->isAuthenticated()) {
......
......@@ -49,7 +49,7 @@ function locale_test_locale_translation_projects_alter(&$projects) {
// Instead of the default ftp.drupal.org we use the file system of the test
// instance to simulate a remote file location.
$url = url(NULL, array('absolute' => TRUE));
$url = \Drupal::url('<none>', [], ['absolute' => TRUE]);
$remote_url = $url . PublicStream::basePath() . '/remote/';
// Completely replace the project data with a set of test projects.
......
<?php
/**
* @file
* Contains \Drupal\system\Tests\PathProcessor\PathProcessorIntegrationTest.
*/
namespace Drupal\system\Tests\PathProcessor;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
/**
* @see \Drupal\Core\PathProcessor\PathProcessorCurrent
* @group path_processor
*/
class PathProcessorCurrentIntegrationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['router']);
\Drupal::service('router.builder')->rebuild();
}
/**
* Tests the output process.
*/
public function testProcessOutbound() {
$request_stack = \Drupal::requestStack();
/** @var \Symfony\Component\Routing\RequestContext $request_context */
$request_context = \Drupal::service('router.request_context');
// Test request with subdir on homepage.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/subdir/', \Drupal::url('<current>'));
// Test request with subdir on other page.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir/node/add', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/subdir/node/add', \Drupal::url('<current>'));
// Test request without subdir on the homepage.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/', \Drupal::url('<current>'));
// Test request without subdir on other page.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/node/add', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/node/add', \Drupal::url('<current>'));
}
}
<?php
/**
* @file
* Contains \Drupal\system\Tests\PathProcessor\PathProcessorNoneIntegrationTest.
*/
namespace Drupal\system\Tests\PathProcessor;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
/**
* @see \Drupal\Core\PathProcessor\PathProcessorNone
* @group path_processor
*/
class PathProcessorNoneIntegrationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['router']);
\Drupal::service('router.builder')->rebuild();
}
/**
* Tests the output process.
*/
public function testProcessOutbound() {
$request_stack = \Drupal::requestStack();
/** @var \Symfony\Component\Routing\RequestContext $request_context */
$request_context = \Drupal::service('router.request_context');
// Test request with subdir on homepage.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/subdir/', \Drupal::url('<none>'));
$this->assertEqual('/subdir/#test-fragment', \Drupal::url('<none>', [], ['fragment' => 'test-fragment']));
// Test request with subdir on other page.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir/node/add', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/subdir/', \Drupal::url('<none>'));
$this->assertEqual('/subdir/#test-fragment', \Drupal::url('<none>', [], ['fragment' => 'test-fragment']));
// Test request without subdir on the homepage.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/', \Drupal::url('<none>'));
$this->assertEqual('/#test-fragment', \Drupal::url('<none>', [], ['fragment' => 'test-fragment']));
// Test request without subdir on other page.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/node/add', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/', \Drupal::url('<none>'));
$this->assertEqual('/#test-fragment', \Drupal::url('<none>', [], ['fragment' => 'test-fragment']));
}
}
......@@ -352,6 +352,12 @@ system.theme_settings_theme:
requirements:
_access: 'TRUE'
'<none>':
path: '<none>'
'<current>':
path: '<current>'
system.modules_uninstall:
path: '/admin/modules/uninstall'
defaults:
......
......@@ -8,6 +8,7 @@
namespace Drupal\Tests\Core;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Tests the Drupal class.
......@@ -131,6 +132,18 @@ public function testQueue() {
$this->assertNotNull(\Drupal::queue('test_queue', TRUE));
}
/**
* Tests the testRequestStack() method.
*
* @covers ::requestStack
*/
public function testRequestStack() {
$request_stack = new RequestStack();
$this->setMockContainerService('request_stack', $request_stack);
$this->assertSame($request_stack, \Drupal::requestStack());
}
/**
* Tests the keyValue() method.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment