diff --git a/core/lib/Drupal/Core/Session/UserSession.php b/core/lib/Drupal/Core/Session/UserSession.php index fbdd2e5..e4a942a 100644 --- a/core/lib/Drupal/Core/Session/UserSession.php +++ b/core/lib/Drupal/Core/Session/UserSession.php @@ -6,6 +6,7 @@ */ namespace Drupal\Core\Session; +use Drupal\Core\Language\LanguageManagerInterface; /** * An implementation of the user account interface for the global user. @@ -101,15 +102,25 @@ class UserSession implements AccountInterface { protected $hostname = ''; /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected $languageManager; + + /** * Constructs a new user session. * * @param array $values * Array of initial values for the user session. + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * The language manager. */ - public function __construct(array $values = array()) { + public function __construct(array $values = array(), LanguageManagerInterface $language_manager) { foreach ($values as $key => $value) { $this->$key = $value; } + $this->languageManager = $language_manager; } /** @@ -183,7 +194,7 @@ public function isAnonymous() { * {@inheritdoc} */ function getPreferredLangcode($fallback_to_default = TRUE) { - $language_list = \Drupal::languageManager()->getLanguages(); + $language_list = $this->languageManager->getLanguages(); if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) { return $language_list[$this->preferred_langcode]->getId(); } diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module index de9315c..ffa5d89 100644 --- a/core/modules/content_translation/content_translation.module +++ b/core/modules/content_translation/content_translation.module @@ -322,6 +322,24 @@ function content_translation_enabled($entity_type, $bundle = NULL) { } /** + * Content translation controller factory. + * + * @param string $entity_type_id + * The type of the entity being translated. + * + * @return \Drupal\content_translation\ContentTranslationHandlerInterface + * An instance of the content translation controller interface. + * + * @todo Move to \Drupal\content_translation\ContentTranslationManager. + */ +function content_translation_controller($entity_type_id) { + $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); + // @todo Throw an exception if the key is missing. + $class = $entity_type->getHandlerClass('translation'); + return new $class($entity_type); +} + +/** * Checks whether a content translation is accessible. * * @param \Drupal\Core\Entity\EntityInterface $entity @@ -339,7 +357,7 @@ function content_translation_enabled($entity_type, $bundle = NULL) { * @todo Move to \Drupal\content_translation\ContentTranslationManager. */ function content_translation_access(EntityInterface $entity, $op) { - return \Drupal::entityManager()->getHandler($entity->getEntityTypeId(), 'translation')->getTranslationAccess($entity, $op); + return content_translation_controller($entity->getEntityTypeId())->getTranslationAccess($entity, $op) ; } /** @@ -353,7 +371,7 @@ function content_translation_form_alter(array &$form, FormStateInterface $form_s $entity = $form_object->getEntity(); if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && count($entity->getTranslationLanguages()) > 1) { - $controller = \Drupal::entityManager()->getHandler($entity->getEntityTypeId(), 'translation'); + $controller = content_translation_controller($entity->getEntityTypeId()); $controller->entityFormAlter($form, $form_state, $entity); // @todo Move the following lines to the code generating the property form diff --git a/core/modules/content_translation/src/ContentTranslationHandler.php b/core/modules/content_translation/src/ContentTranslationHandler.php index cb6e8bd..36ccacd 100644 --- a/core/modules/content_translation/src/ContentTranslationHandler.php +++ b/core/modules/content_translation/src/ContentTranslationHandler.php @@ -127,8 +127,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En // Adjust page title to specify the current language being edited, if we // have at least one translation. - $language_manager = $this->languageManager; - $languages = $language_manager->getLanguages(); + $languages = $this->languageManager->getLanguages(); if (isset($languages[$form_langcode]) && ($has_translations || $new_translation)) { $title = $this->entityFormTitle($entity); // When editing the original values display just the entity label. @@ -161,7 +160,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En '#submit' => array(array($this, 'entityFormSourceChange')), ), ); - foreach ($language_manager->getLanguages() as $language) { + foreach ($this->languageManager->getLanguages() as $language) { if (isset($translations[$language->getId()])) { $form['source_langcode']['source']['#options'][$language->getId()] = $language->getName(); } @@ -174,7 +173,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En $language_widget = isset($form['langcode']) && $form['langcode']['#type'] == 'language_select'; if ($language_widget && $has_translations) { $form['langcode']['#options'] = array(); - foreach ($language_manager->getLanguages() as $language) { + foreach ($this->languageManager->getLanguages() as $language) { if (empty($translations[$language->getId()]) || $language->getId() == $entity_langcode) { $form['langcode']['#options'][$language->getId()] = $language->getName(); } diff --git a/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php b/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php index d9f4ed6..e52036b 100644 --- a/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php +++ b/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php @@ -88,7 +88,7 @@ protected function setUp() { $this->setupUsers(); $this->setupTestFields(); - $this->controller = \Drupal::entityManager()->getHandler($this->entityTypeId, 'translation'); + $this->controller = content_translation_controller($this->entityTypeId); // Rebuild the container so that the new languages are picked up by services // that hold a list of languages. diff --git a/core/modules/node/src/Plugin/views/field/Node.php b/core/modules/node/src/Plugin/views/field/Node.php index a62e0f2..9ac0949 100644 --- a/core/modules/node/src/Plugin/views/field/Node.php +++ b/core/modules/node/src/Plugin/views/field/Node.php @@ -8,10 +8,12 @@ namespace Drupal\node\Plugin\views\field; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\views\ResultRow; use Drupal\views\ViewExecutable; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\Plugin\views\field\FieldPluginBase; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Field handler to provide simple renderer that allows linking to a node. @@ -25,6 +27,43 @@ class Node extends FieldPluginBase { /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected $languageManager; + + /** + * Constructs a PluginBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * The language manager. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->languageManager = $language_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('language_manager') + ); + } + + /** * Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::init(). */ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) { @@ -73,7 +112,7 @@ protected function renderLink($data, ResultRow $values) { $this->options['alter']['make_link'] = TRUE; $this->options['alter']['path'] = "node/" . $this->getValue($values, 'nid'); if (isset($this->aliases['langcode'])) { - $languages = \Drupal::languageManager()->getLanguages(); + $languages = $this->languageManager->getLanguages(); $langcode = $this->getValue($values, 'langcode'); if (isset($languages[$langcode])) { $this->options['alter']['language'] = $languages[$langcode];