Issue #2527846 by dawehner, Wim Leers: Try to get rid of Url::fromRoute() in system_js_settings_alter()

8.0.x
Nathaniel Catchpole 2015-07-24 15:08:23 +01:00
parent 04564f7d1f
commit ca004120e6
4 changed files with 44 additions and 10 deletions

View File

@ -46,6 +46,8 @@ class InstallerServiceProvider implements ServiceProviderInterface, ServiceModif
$container
->register('url_generator', 'Drupal\Core\Routing\NullGenerator')
->addArgument(new Reference('request_stack'));
$container
->register('path_processor_manager', 'Drupal\Core\PathProcessor\NullPathProcessorManager');
$container
->register('router.dumper', 'Drupal\Core\Routing\NullMatcherDumper');

View File

@ -0,0 +1,34 @@
<?php
/**
* @file
* Contains \Drupal\Core\PathProcessor\NullPathProcessorManager.
*/
namespace Drupal\Core\PathProcessor;
use Drupal\Core\Render\BubbleableMetadata;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a null implementation of the path processor manager.
*
* This can be used for example in really early installer phases.
*/
class NullPathProcessorManager implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
/**
* {@inheritdoc}
*/
public function processInbound($path, Request $request) {
return $path;
}
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
return $path;
}
}

View File

@ -215,7 +215,6 @@ class AttachedAssetsTest extends KernelTestBase {
// Test whether the settings for core/drupalSettings are available.
$this->assertTrue(isset($parsed_settings['path']['baseUrl']), 'drupalSettings.path.baseUrl is present.');
$this->assertTrue(isset($parsed_settings['path']['scriptPath']), 'drupalSettings.path.scriptPath is present.');
$this->assertIdentical($parsed_settings['path']['pathPrefix'], 'yarhar', 'drupalSettings.path.pathPrefix is present and has the correct (overridden) value.');
$this->assertIdentical($parsed_settings['path']['currentPath'], '', 'drupalSettings.path.currentPath is present and has the correct value.');
$this->assertIdentical($parsed_settings['path']['currentPathIsAdmin'], FALSE, 'drupalSettings.path.currentPathIsAdmin is present and has the correct value.');

View File

@ -648,21 +648,20 @@ function system_js_settings_build(&$settings, AttachedAssetsInterface $assets) {
* as well as theme_token ajax state.
*/
function system_js_settings_alter(&$settings, AttachedAssetsInterface $assets) {
// url() generates the script and prefix using hook_url_outbound_alter().
// Instead of running the hook_url_outbound_alter() again here, extract
// them from url().
// @todo Make this less hacky: https://www.drupal.org/node/1547376.
$request = \Drupal::request();
$scriptPath = $request->getScriptName();
$pathPrefix = '';
$current_query = $request->query->all();
Url::fromRoute('<front>', [], array('script' => &$scriptPath, 'prefix' => &$pathPrefix))->toString(TRUE);
// Let output path processors set a prefix.
/** @var \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $path_processor */
$path_processor = \Drupal::service('path_processor_manager');
$options = ['prefix' => ''];
$path_processor->processOutbound('/', $options);
$pathPrefix = $options['prefix'];
$current_path = \Drupal::routeMatch()->getRouteName() ? Url::fromRouteMatch(\Drupal::routeMatch())->getInternalPath() : '';
$current_path_is_admin = \Drupal::service('router.admin_context')->isAdminRoute();
$path_settings = [
'baseUrl' => $request->getBaseUrl() . '/',
'scriptPath' => $scriptPath,
'pathPrefix' => $pathPrefix,
'currentPath' => $current_path,
'currentPathIsAdmin' => $current_path_is_admin,