diff -u b/core/modules/node/node.module b/core/modules/node/node.module --- b/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -548,7 +548,7 @@ foreach ($variables['content'] as $type) { $variables['types'][$type->id()] = [ 'type' => $type->id(), - 'add_link' => Link::fromTextAndUrl($type->label(), new Url('node.add', ['node_type' => $type->id()], ['language' => $content_language])), + 'add_link' => Link::fromTextAndUrl($type->label(), Url::fromRoute('node.add', ['node_type' => $type->id()], ['language' => $content_language]))->toString(), 'description' => [ '#markup' => $type->getDescription(), ], diff -u b/core/modules/responsive_image/src/ResponsiveImageStyleForm.php b/core/modules/responsive_image/src/ResponsiveImageStyleForm.php --- b/core/modules/responsive_image/src/ResponsiveImageStyleForm.php +++ b/core/modules/responsive_image/src/ResponsiveImageStyleForm.php @@ -281,13 +281,8 @@ // Redirect to edit form after creating a new responsive image style or // after selecting another breakpoint group. - if (!$responsive_image_style->hasImageStyleMappings()) { - $link = 'edit-form'; - } - else { - $link = 'collection'; - } - $form_state->setRedirectUrl($responsive_image_style->toUrl($link)); + $link_template = $responsive_image_style->hasImageStyleMappings() ? 'collection' : 'edit-form'; + $form_state->setRedirectUrl($responsive_image_style->toUrl($link_template)); } } diff -u b/core/modules/user/src/Controller/UserController.php b/core/modules/user/src/Controller/UserController.php --- b/core/modules/user/src/Controller/UserController.php +++ b/core/modules/user/src/Controller/UserController.php @@ -6,9 +6,7 @@ use Drupal\Component\Utility\Xss; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Datetime\DateFormatterInterface; -use Drupal\Core\Language\LanguageInterface; -use Drupal\Core\Language\LanguageManagerInterface; -use Drupal\user\Entity\User; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Url; use Drupal\user\Form\UserPasswordResetForm; @@ -62,4 +60,11 @@ /** + * The entity repository. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + + /** * Constructs a UserController object. * @@ -73,8 +78,10 @@ * A logger instance. * @param \Drupal\Core\Flood\FloodInterface $flood * The flood service. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. */ - public function __construct(DateFormatterInterface $date_formatter, UserStorageInterface $user_storage, UserDataInterface $user_data, LoggerInterface $logger, FloodInterface $flood = NULL, LanguageManagerInterface $language_manager) { + public function __construct(DateFormatterInterface $date_formatter, UserStorageInterface $user_storage, UserDataInterface $user_data, LoggerInterface $logger, FloodInterface $flood = NULL, EntityRepositoryInterface $entity_repository = NULL) { $this->dateFormatter = $date_formatter; $this->userStorage = $user_storage; $this->userData = $user_data; @@ -84,7 +91,11 @@ $flood = \Drupal::service('flood'); } $this->flood = $flood; - $this->languageManager = $language_manager; + if (!$entity_repository) { + @trigger_error('Calling ' . __METHOD__ . ' without the $entity_repository parameter is deprecated in drupal:9.1.0 and is required in drupal:10.0.0. See https://www.drupal.org/project/drupal/issues/2940992', E_USER_DEPRECATED); + $entity_repository = \Drupal::service('entity.repository'); + } + $this->entityRepository = $entity_repository; } /** @@ -97,7 +108,7 @@ $container->get('user.data'), $container->get('logger.factory')->get('user'), $container->get('flood'), - $container->get('language_manager') + $container->get('entity.repository') ); } @@ -258,7 +269,13 @@ $_SESSION['pass_reset_' . $user->id()] = $token; // Clear any flood events for this user. $this->flood->clear('user.password_request_user', $uid); - $user = \Drupal::service('entity.repository')->getTranslationFromContext($user); + + // Retrieve the entity in a preferable translation in order to consider it + // when generating the URL. + $preferred_langcode = $user->getPreferredLangcode(FALSE); + $preferred_langcode = empty($preferred_langcode) ? NULL : $preferred_langcode; + $user = $this->entityRepository->getTranslationFromContext($user, $preferred_langcode); + $url = $user->toUrl('edit-form', [ 'query' => ['pass-reset-token' => $token], 'absolute' => TRUE, @@ -282,10 +299,16 @@ */ public function userPage() { $user = $this->currentUser()->id(); - $user = User::load($user); - $user = \Drupal::service('entity.repository')->getTranslationFromContext($user); + /** @var \Drupal\user\UserInterface $user */ + $user = $this->userStorage->load($user); + + // Retrieve the entity in a preferable translation in order to consider it + // when generating the URL. + $preferred_langcode = $user->getPreferredLangcode(FALSE); + $preferred_langcode = empty($preferred_langcode) ? NULL : $preferred_langcode; + $user = $this->entityRepository->getTranslationFromContext($user, $preferred_langcode); - return $this->redirectToUrl($user->toUrl('canonical', ['language' => $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)])); + return $this->redirectToUrl($user->toUrl()); } /** diff -u b/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php b/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php --- b/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php +++ b/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php @@ -3,11 +3,12 @@ namespace Drupal\user\EventSubscriber; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Url; -use Drupal\user\Entity\User; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; @@ -32,13 +33,33 @@ protected $account; /** + * The entity repository. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + + /** + * The user storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $storage; + + /** * Constructs a new redirect subscriber. * * @param \Drupal\Core\Session\AccountInterface $account * The current user. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. */ - public function __construct(AccountInterface $account) { + public function __construct(AccountInterface $account, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) { $this->account = $account; + $this->storage = $entity_type_manager->getStorage('user'); + $this->entityRepository = $entity_repository; } /** @@ -51,24 +72,29 @@ $exception = $event->getException(); if ($exception instanceof AccessDeniedHttpException) { $route_name = RouteMatch::createFromRequest($event->getRequest())->getRouteName(); - $language = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT); $redirect_url = NULL; if ($this->account->isAuthenticated()) { $user = $this->account; if (!$this->account instanceof EntityInterface) { - $user = User::load($this->account->id()); - $user = \Drupal::service('entity.repository')->getTranslationFromContext($user); + /** @var \Drupal\user\UserInterface $user */ + $user = $this->storage->load($this->account->id()); } + // Retrieve the entity in a preferable translation in order to consider it + // when generating the URL. + $preferred_langcode = $user->getPreferredLangcode(FALSE); + $preferred_langcode = empty($preferred_langcode) ? NULL : $preferred_langcode; + $user = $this->entityRepository->getTranslationFromContext($user, $preferred_langcode); + switch ($route_name) { case 'user.login'; // Redirect an authenticated user to the profile page. - $redirect_url = $user->toUrl('canonical', ['language' => $language]); + $redirect_url = $user->toUrl(); break; case 'user.register'; // Redirect an authenticated user to the profile form. - $redirect_url = $user->toUrl('edit-form', ['language' => $language]); + $redirect_url = $user->toUrl('edit-form'); break; } } diff -u b/core/modules/user/src/Form/UserLoginForm.php b/core/modules/user/src/Form/UserLoginForm.php --- b/core/modules/user/src/Form/UserLoginForm.php +++ b/core/modules/user/src/Form/UserLoginForm.php @@ -2,6 +2,7 @@ namespace Drupal\user\Form; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; @@ -50,4 +51,11 @@ /** + * The entity repository. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + + /** * Constructs a new UserLoginForm. * @@ -59,12 +67,15 @@ * The user authentication object. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. */ - public function __construct(FloodInterface $flood, UserStorageInterface $user_storage, UserAuthInterface $user_auth, RendererInterface $renderer) { + public function __construct(FloodInterface $flood, UserStorageInterface $user_storage, UserAuthInterface $user_auth, RendererInterface $renderer, EntityRepositoryInterface $entity_repository) { $this->flood = $flood; $this->userStorage = $user_storage; $this->userAuth = $user_auth; $this->renderer = $renderer; + $this->entityRepository = $entity_repository; } /** @@ -75,7 +86,8 @@ $container->get('flood'), $container->get('entity_type.manager')->getStorage('user'), $container->get('user.auth'), - $container->get('renderer') + $container->get('renderer'), + $container->get('entity.repository') ); } @@ -132,12 +144,18 @@ * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { + /** @var \Drupal\user\UserInterface $account */ $account = $this->userStorage->load($form_state->get('uid')); - $account = \Drupal::service('entity.repository')->getTranslationFromContext($account); + + // Retrieve the entity in a preferable translation in order to consider it + // when generating the URL. + $preferred_langcode = $account->getPreferredLangcode(FALSE); + $preferred_langcode = empty($preferred_langcode) ? NULL : $preferred_langcode; + $account = $this->entityRepository->getTranslationFromContext($account, $preferred_langcode); // A destination was set, probably on an exception controller, if (!$this->getRequest()->request->has('destination')) { - $form_state->setRedirectUrl($account->toUrl('canonical', ['language' => \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)])); + $form_state->setRedirectUrl($account->toUrl('canonical')); } else { $this->getRequest()->query->set('destination', $this->getRequest()->request->get('destination')); diff -u b/core/modules/user/tests/src/Kernel/Controller/UserControllerTest.php b/core/modules/user/tests/src/Kernel/Controller/UserControllerTest.php --- b/core/modules/user/tests/src/Kernel/Controller/UserControllerTest.php +++ b/core/modules/user/tests/src/Kernel/Controller/UserControllerTest.php @@ -3,8 +3,6 @@ namespace Drupal\Tests\user\Kernel\Controller; use Drupal\Core\Datetime\DateFormatterInterface; -use Drupal\Core\Flood\FloodInterface; -use Drupal\Core\Language\LanguageManagerInterface; use Drupal\KernelTests\KernelTestBase; use Drupal\user\Controller\UserController; use Drupal\user\UserDataInterface; @@ -20,22 +18,18 @@ /** * @group legacy * @expectedDeprecation Calling Drupal\user\Controller\UserController::__construct without the $flood parameter is deprecated in drupal:8.8.0 and is required in drupal:9.0.0. See https://www.drupal.org/node/1681832 + * @expectedDeprecation Calling Drupal\user\Controller\UserController::__construct without the $entity_repository parameter is deprecated in drupal:9.1.0 and is required in drupal:10.0.0. See https://www.drupal.org/project/drupal/issues/2940992 */ public function testConstructorDeprecations() { $date_formatter = $this->prophesize(DateFormatterInterface::class); $user_storage = $this->prophesize(UserStorageInterface::class); $user_data = $this->prophesize(UserDataInterface::class); $logger = $this->prophesize(LoggerInterface::class); - $flood = $this->prophesize(FloodInterface::class); - $language_manager = $this->prophesize(LanguageManagerInterface::class); - $controller = new UserController( $date_formatter->reveal(), $user_storage->reveal(), $user_data->reveal(), - $logger->reveal(), - NULL, - $language_manager->reveal() + $logger->reveal() ); $this->assertNotNull($controller); } diff -u b/core/modules/user/user.module b/core/modules/user/user.module --- b/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -532,8 +532,15 @@ ->toString(); } else { + /** @var \Drupal\user\UserInterface $user */ $user = $account instanceof EntityInterface ? $account : User::load($account->id()); - $user = \Drupal::service('entity.repository')->getTranslationFromContext($user); + + // Retrieve the entity in a preferable translation in order to consider it + // when generating the URL. + $preferred_langcode = $user->getPreferredLangcode(FALSE); + $preferred_langcode = empty($preferred_langcode) ? NULL : $preferred_langcode; + $user = \Drupal::service('entity.repository')->getTranslationFromContext($user, $preferred_langcode); + $variables['attributes']['href'] = $user->toUrl()->toString(); } } diff -u b/core/modules/user/user.services.yml b/core/modules/user/user.services.yml --- b/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -32,7 +32,7 @@ - { name: event_subscriber } user_access_denied_subscriber: class: Drupal\user\EventSubscriber\AccessDeniedSubscriber - arguments: ['@current_user'] + arguments: ['@current_user', '@entity_type.manager', '@entity.repository'] tags: - { name: event_subscriber } user_last_access_subscriber: diff -u b/core/themes/seven/seven.theme b/core/themes/seven/seven.theme --- b/core/themes/seven/seven.theme +++ b/core/themes/seven/seven.theme @@ -5,9 +5,9 @@ * Functions to support theming in the Seven theme. */ +use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Url; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Language\LanguageInterface; use Drupal\media\MediaForm; use Drupal\views\Form\ViewsForm; use Drupal\views\ViewExecutable;