Commit b1d33121 authored by larowlan's avatar larowlan

Issue #3001309 by alexpott, Krzysztof Domański: Replace usages of...

Issue #3001309 by alexpott, Krzysztof Domański: Replace usages of UrlGeneratorTrait in non-api classes
parent 1f032717
......@@ -2,8 +2,8 @@
namespace Drupal\Core\StreamWrapper;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
/**
* Drupal private (private://) stream wrapper class.
......@@ -13,8 +13,6 @@
*/
class PrivateStream extends LocalStream {
use UrlGeneratorTrait;
/**
* {@inheritdoc}
*/
......@@ -48,7 +46,7 @@ public function getDirectoryPath() {
*/
public function getExternalUrl() {
$path = str_replace('\\', '/', $this->getTarget());
return $this->url('system.private_file_download', ['filepath' => $path], ['absolute' => TRUE, 'path_processing' => FALSE]);
return Url::fromRoute('system.private_file_download', ['filepath' => $path], ['absolute' => TRUE, 'path_processing' => FALSE])->toString();
}
/**
......
......@@ -14,7 +14,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -31,7 +31,6 @@
class DefaultProcessor extends AggregatorPluginSettingsBase implements ProcessorInterface, ContainerFactoryPluginInterface {
use ConfigFormBaseTrait;
use UrlGeneratorTrait;
/**
* Contains the configuration object factory.
......@@ -150,7 +149,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'#title' => t('Discard items older than'),
'#default_value' => $config->get('items.expire'),
'#options' => $period,
'#description' => t('Requires a correctly configured <a href=":cron">cron maintenance task</a>.', [':cron' => $this->url('system.status')]),
'#description' => t('Requires a correctly configured <a href=":cron">cron maintenance task</a>.', [':cron' => Url::fromRoute('system.status')->toString()]),
];
$lengths = [0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000];
......
......@@ -4,13 +4,12 @@
use Drupal\Core\Menu\LocalActionDefault;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Url;
/**
* Modifies the 'Add custom block' local action.
*/
class BlockContentAddLocalAction extends LocalActionDefault {
use UrlGeneratorTrait;
/**
* {@inheritdoc}
......@@ -23,7 +22,7 @@ public function getOptions(RouteMatchInterface $route_match) {
}
// Adds a destination on custom block listing.
if ($route_match->getRouteName() == 'entity.block_content.collection') {
$options['query']['destination'] = $this->url('<current>');
$options['query']['destination'] = Url::fromRoute('<current>')->toString();
}
return $options;
}
......
......@@ -7,7 +7,7 @@ services:
comment.manager:
class: Drupal\comment\CommentManager
arguments: ['@entity.manager', '@config.factory', '@string_translation', '@url_generator', '@module_handler', '@current_user']
arguments: ['@entity.manager', '@config.factory', '@string_translation', '@module_handler', '@current_user']
comment.statistics:
class: Drupal\comment\CommentStatistics
......
......@@ -8,11 +8,10 @@
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\user\RoleInterface;
......@@ -22,7 +21,6 @@
*/
class CommentManager implements CommentManagerInterface {
use StringTranslationTrait;
use UrlGeneratorTrait;
/**
* The entity manager service.
......@@ -68,18 +66,15 @@ class CommentManager implements CommentManagerInterface {
* The config factory.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The url generator service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
$this->entityManager = $entity_manager;
$this->userConfig = $config_factory->get('user.settings');
$this->stringTranslation = $string_translation;
$this->urlGenerator = $url_generator;
$this->moduleHandler = $module_handler;
$this->currentUser = $current_user;
}
......@@ -151,7 +146,7 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
'entity' => $entity->id(),
'field_name' => $field_name,
];
$destination = ['destination' => $this->url('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])];
$destination = ['destination' => Url::fromRoute('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])->toString()];
}
else {
$destination = ['destination' => $entity->url('canonical', ['fragment' => 'comment-form'])];
......@@ -160,14 +155,14 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
// Users can register themselves.
return $this->t('<a href=":login">Log in</a> or <a href=":register">register</a> to post comments', [
':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]),
':register' => $this->urlGenerator->generateFromRoute('user.register', [], ['query' => $destination]),
':login' => Url::fromRoute('user.login', [], ['query' => $destination])->toString(),
':register' => Url::fromRoute('user.register', [], ['query' => $destination])->toString(),
]);
}
else {
// Only admins can add new users, no public registration.
return $this->t('<a href=":login">Log in</a> to post comments', [
':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]),
':login' => Url::fromRoute('user.login', [], ['query' => $destination])->toString(),
]);
}
}
......
......@@ -7,10 +7,10 @@
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Url;
/**
* Plugin implementation of the 'comment' field type.
......@@ -26,7 +26,6 @@
* )
*/
class CommentItem extends FieldItemBase implements CommentItemInterface {
use UrlGeneratorTrait;
/**
* {@inheritdoc}
......@@ -188,7 +187,7 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state
'#title' => t('Comment type'),
'#options' => $options,
'#required' => TRUE,
'#description' => $this->t('Select the Comment type to use for this comment field. Manage the comment types from the <a href=":url">administration overview page</a>.', [':url' => $this->url('entity.comment_type.collection')]),
'#description' => $this->t('Select the Comment type to use for this comment field. Manage the comment types from the <a href=":url">administration overview page</a>.', [':url' => Url::fromRoute('entity.comment_type.collection')->toString()]),
'#default_value' => $this->getSetting('comment_type'),
'#disabled' => $has_data,
];
......
......@@ -48,7 +48,6 @@ public function testGetFields() {
$entity_manager,
$this->getMock('Drupal\Core\Config\ConfigFactoryInterface'),
$this->getMock('Drupal\Core\StringTranslation\TranslationInterface'),
$this->getMock('Drupal\Core\Routing\UrlGeneratorInterface'),
$this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'),
$this->getMock('Drupal\Core\Session\AccountInterface')
);
......
......@@ -4,7 +4,6 @@
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Url;
use Drupal\search\Plugin\ConfigurableSearchPluginBase;
......@@ -18,8 +17,6 @@
*/
class SearchExtraTypeSearch extends ConfigurableSearchPluginBase {
use UrlGeneratorTrait;
/**
* {@inheritdoc}
*/
......
......@@ -4,9 +4,9 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
......@@ -21,8 +21,6 @@
*/
class AccessDeniedSubscriber implements EventSubscriberInterface {
use UrlGeneratorTrait;
/**
* The current user.
*
......@@ -35,12 +33,9 @@ class AccessDeniedSubscriber implements EventSubscriberInterface {
*
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The URL generator.
*/
public function __construct(AccountInterface $account, UrlGeneratorInterface $url_generator) {
public function __construct(AccountInterface $account) {
$this->account = $account;
$this->setUrlGenerator($url_generator);
}
/**
......@@ -53,21 +48,26 @@ public function onException(GetResponseForExceptionEvent $event) {
$exception = $event->getException();
if ($exception instanceof AccessDeniedHttpException) {
$route_name = RouteMatch::createFromRequest($event->getRequest())->getRouteName();
$redirect_url = NULL;
if ($this->account->isAuthenticated()) {
switch ($route_name) {
case 'user.login';
// Redirect an authenticated user to the profile page.
$event->setResponse($this->redirect('entity.user.canonical', ['user' => $this->account->id()]));
$redirect_url = Url::fromRoute('entity.user.canonical', ['user' => $this->account->id()], ['absolute' => TRUE]);
break;
case 'user.register';
// Redirect an authenticated user to the profile form.
$event->setResponse($this->redirect('entity.user.edit_form', ['user' => $this->account->id()]));
$redirect_url = Url::fromRoute('entity.user.edit_form', ['user' => $this->account->id()], ['absolute' => TRUE]);
break;
}
}
elseif ($route_name === 'user.page') {
$event->setResponse($this->redirect('user.login'));
$redirect_url = Url::fromRoute('user.login', [], ['absolute' => TRUE]);
}
if ($redirect_url) {
$event->setResponse(new RedirectResponse($redirect_url->toString()));
}
}
}
......
......@@ -3,10 +3,11 @@
namespace Drupal\user\EventSubscriber;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\MaintenanceModeInterface;
use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
......@@ -15,8 +16,6 @@
*/
class MaintenanceModeSubscriber implements EventSubscriberInterface {
use UrlGeneratorTrait;
/**
* The maintenance mode.
*
......@@ -58,7 +57,9 @@ public function onKernelRequestMaintenance(GetResponseEvent $event) {
if ($this->account->isAuthenticated() && !$this->maintenanceMode->exempt($this->account)) {
user_logout();
// Redirect to homepage.
$event->setResponse($this->redirect($this->url('<front>')));
$event->setResponse(
new RedirectResponse(Url::fromRoute('<front>')->toString())
);
}
}
}
......
......@@ -32,7 +32,7 @@ services:
- { name: event_subscriber }
user_access_denied_subscriber:
class: Drupal\user\EventSubscriber\AccessDeniedSubscriber
arguments: ['@current_user', '@url_generator']
arguments: ['@current_user']
tags:
- { name: event_subscriber }
user_last_access_subscriber:
......
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