diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 5a920ec920..ee50711255 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -16,6 +16,7 @@ use Drupal\Core\Database\StatementInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; @@ -531,11 +532,11 @@ function node_is_page(NodeInterface $node) { function template_preprocess_node_add_list(&$variables) { $variables['types'] = []; if (!empty($variables['content'])) { - $default_language = \Drupal::languageManager()->getDefaultLanguage(); + $content_language = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT); foreach ($variables['content'] as $type) { $variables['types'][$type->id()] = [ 'type' => $type->id(), - 'add_link' => \Drupal::l($type->label(), new Url('node.add', ['node_type' => $type->id()], ['language' => $default_language])), + 'add_link' => \Drupal::l($type->label(), new Url('node.add', ['node_type' => $type->id()], ['language' => $content_language])), 'description' => [ '#markup' => $type->getDescription(), ], diff --git a/core/modules/node/src/Controller/NodeController.php b/core/modules/node/src/Controller/NodeController.php index c493ccdf3b..a5b199f6b2 100644 --- a/core/modules/node/src/Controller/NodeController.php +++ b/core/modules/node/src/Controller/NodeController.php @@ -6,6 +6,7 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Url; use Drupal\node\NodeStorageInterface; @@ -88,7 +89,7 @@ public function addPage() { // Bypass the node/add listing if only one content type is available. if (count($content) == 1) { $type = array_shift($content); - return $this->redirect('node.add', ['node_type' => $type->id()], ['language' => $this->languageManager()->getDefaultLanguage()]); + return $this->redirect('node.add', ['node_type' => $type->id()], ['language' => $this->languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)]); } $build['#content'] = $content; diff --git a/core/modules/user/src/Controller/UserController.php b/core/modules/user/src/Controller/UserController.php index e6d248b647..0c2bf8246f 100644 --- a/core/modules/user/src/Controller/UserController.php +++ b/core/modules/user/src/Controller/UserController.php @@ -230,9 +230,8 @@ public function resetPassLogin($uid, $timestamp, $hash) { // check. $token = Crypt::randomBytesBase64(55); $_SESSION['pass_reset_' . $user->id()] = $token; - $interface_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); - $user = $user->hasTranslation($interface_langcode) ? $user->getTranslation($interface_langcode) : $user; + $user = \Drupal::service('entity.repository')->getTranslationFromContext($user); $url = $user->toUrl('edit-form', [ 'query' => ['pass-reset-token' => $token], 'absolute' => TRUE, @@ -257,8 +256,7 @@ public function resetPassLogin($uid, $timestamp, $hash) { public function userPage() { $user = $this->currentUser()->id(); $user = User::load($user); - $interface_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); - $user = $user->hasTranslation($interface_langcode) ? $user->getTranslation($interface_langcode) : $user; + $user = \Drupal::service('entity.repository')->getTranslationFromContext($user); return $this->redirectToUrl($user->toUrl()); } diff --git a/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php b/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php index 21b1947f78..47ede3398c 100644 --- a/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php +++ b/core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php @@ -59,8 +59,7 @@ public function onException(GetResponseForExceptionEvent $event) { $user = $this->account; if (!$this->account instanceof EntityInterface) { $user = User::load($this->account->id()); - $interface_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); - $user = $user->hasTranslation($interface_langcode) ? $user->getTranslation($interface_langcode) : $user; + $user = \Drupal::service('entity.repository')->getTranslationFromContext($user); } switch ($route_name) { diff --git a/core/modules/user/src/Form/UserLoginForm.php b/core/modules/user/src/Form/UserLoginForm.php index fb8a546f13..036f862896 100644 --- a/core/modules/user/src/Form/UserLoginForm.php +++ b/core/modules/user/src/Form/UserLoginForm.php @@ -130,8 +130,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { $account = $this->userStorage->load($form_state->get('uid')); - $interface_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); - $account = $account->hasTranslation($interface_langcode) ? $account->getTranslation($interface_langcode) : $account; + $account = \Drupal::service('entity.repository')->getTranslationFromContext($account); // A destination was set, probably on an exception controller, if (!$this->getRequest()->request->has('destination')) { diff --git a/core/modules/user/src/ToolbarLinkBuilder.php b/core/modules/user/src/ToolbarLinkBuilder.php index 19adb6b54c..1671fb5df1 100644 --- a/core/modules/user/src/ToolbarLinkBuilder.php +++ b/core/modules/user/src/ToolbarLinkBuilder.php @@ -3,6 +3,7 @@ namespace Drupal\user; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Session\AccountProxyInterface; @@ -30,17 +31,16 @@ class ToolbarLinkBuilder { * The current user. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. - * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager - * The language manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. */ - public function __construct(AccountProxyInterface $account, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) { + public function __construct(AccountProxyInterface $account, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) { $this->currentUser = $account->getAccount(); if (!$this->currentUser instanceof ContentEntityInterface) { $this->currentUser = $entity_type_manager->getStorage('user') ->load($this->currentUser->id()); } - $interface_langcode = $language_manager->getCurrentLanguage()->getId(); - $this->currentUser = $this->currentUser->hasTranslation($interface_langcode) ? $this->currentUser->getTranslation($interface_langcode) : $this->currentUser; + $this->currentUser = $entity_repository->getTranslationFromContext($this->currentUser); } /** diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 002488a74d..382677028b 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -10,6 +10,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Asset\AttachedAssetsInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; @@ -527,8 +528,7 @@ function template_preprocess_username(&$variables) { } else { $user = $account instanceof EntityInterface ? $account : User::load($account->id()); - $interface_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); - $user = $user->hasTranslation($interface_langcode) ? $user->getTranslation($interface_langcode) : $user; + $user = \Drupal::service('entity.repository')->getTranslationFromContext($user); $variables['attributes']['href'] = $user->toUrl()->toString(); } } diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index f10447979e..87a6af1f5b 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -70,4 +70,4 @@ services: - { name: 'context_provider' } user.toolbar_link_builder: class: Drupal\user\ToolbarLinkBuilder - arguments: ['@current_user', '@entity_type.manager', '@language_manager'] + arguments: ['@current_user', '@entity_type.manager', '@entity.repository'] diff --git a/core/themes/seven/seven.theme b/core/themes/seven/seven.theme index 4808484dab..bf602e635c 100644 --- a/core/themes/seven/seven.theme +++ b/core/themes/seven/seven.theme @@ -6,6 +6,7 @@ */ use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Language\LanguageInterface; /** * Implements hook_preprocess_HOOK() for HTML document templates. @@ -58,11 +59,11 @@ function seven_preprocess_menu_local_task(&$variables) { */ function seven_preprocess_node_add_list(&$variables) { if (!empty($variables['content'])) { - $default_language = \Drupal::languageManager()->getDefaultLanguage(); + $content_language = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT); /** @var \Drupal\node\NodeTypeInterface $type */ foreach ($variables['content'] as $type) { $variables['types'][$type->id()]['label'] = $type->label(); - $variables['types'][$type->id()]['url'] = \Drupal::url('node.add', ['node_type' => $type->id()], ['language' => $default_language]); + $variables['types'][$type->id()]['url'] = \Drupal::url('node.add', ['node_type' => $type->id()], ['language' => $content_language]); } } }