Commit 338e9669 authored by alexpott's avatar alexpott

Issue #2328293 by keopx, JeroenT, ianthomas_uk, ashutoshsngh, rpayanm, hudo,...

Issue #2328293 by keopx, JeroenT, ianthomas_uk, ashutoshsngh, rpayanm, hudo, a_thakur: Remove usage of language_list()
parent f98c96a0
......@@ -25,6 +25,7 @@
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -121,6 +122,13 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
*/
protected $cacheBackend;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* {@inheritdoc}
*/
......@@ -129,7 +137,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
$entity_type,
$container->get('database'),
$container->get('entity.manager'),
$container->get('cache.entity')
$container->get('cache.entity'),
$container->get('language_manager')
);
}
......@@ -155,12 +164,15 @@ public function getFieldStorageDefinitions() {
* The entity manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend to be used.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache) {
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) {
parent::__construct($entity_type);
$this->database = $database;
$this->entityManager = $entity_manager;
$this->cacheBackend = $cache;
$this->languageManager = $language_manager;
$this->initTableLayout();
}
......@@ -1251,7 +1263,7 @@ protected function loadFieldItems(array $entities) {
}
// Load field data.
$langcodes = array_keys(language_list(LanguageInterface::STATE_ALL));
$langcodes = array_keys($this->languageManager->getLanguages(LanguageInterface::STATE_ALL));
foreach ($storage_definitions as $field_name => $storage_definition) {
$table = $load_current ? $table_mapping->getDedicatedDataTableName($storage_definition) : $table_mapping->getDedicatedRevisionTableName($storage_definition);
......
......@@ -183,7 +183,7 @@ public function isAnonymous() {
* {@inheritdoc}
*/
function getPreferredLangcode($fallback_to_default = TRUE) {
$language_list = language_list();
$language_list = \Drupal::languageManager()->getLanguages();
if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) {
return $language_list[$this->preferred_langcode]->getId();
}
......@@ -196,7 +196,7 @@ function getPreferredLangcode($fallback_to_default = TRUE) {
* {@inheritdoc}
*/
function getPreferredAdminLangcode($fallback_to_default = TRUE) {
$language_list = language_list();
$language_list = \Drupal::languageManager()->getLanguages();
if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) {
return $language_list[$this->preferred_admin_langcode]->getId();
}
......
......@@ -15,6 +15,7 @@
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -45,9 +46,11 @@ class CommentStorage extends SqlContentEntityStorage implements CommentStorageIn
* Cache backend instance to use.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache) {
parent::__construct($entity_info, $database, $entity_manager, $cache);
public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) {
parent::__construct($entity_info, $database, $entity_manager, $cache, $language_manager);
$this->currentUser = $current_user;
}
......@@ -60,7 +63,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
$container->get('database'),
$container->get('entity.manager'),
$container->get('current_user'),
$container->get('cache.entity')
$container->get('cache.entity'),
$container->get('language_manager')
);
}
......
......@@ -242,10 +242,7 @@ function content_translation_translate_access(EntityInterface $entity) {
* @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);
return \Drupal::entityManager()->getHandler($entity_type_id, 'translation');
}
/**
......
......@@ -8,18 +8,21 @@
namespace Drupal\content_translation;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\Element;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for content translation handlers.
*
* @ingroup entity_api
*/
class ContentTranslationHandler implements ContentTranslationHandlerInterface {
class ContentTranslationHandler implements ContentTranslationHandlerInterface, EntityHandlerInterface {
/**
* The type of the entity being translated.
......@@ -35,15 +38,35 @@ class ContentTranslationHandler implements ContentTranslationHandlerInterface {
*/
protected $entityType;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('language_manager')
);
}
/**
* Initializes an instance of the content translation controller.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The info array of the given entity type.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityTypeInterface $entity_type) {
public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager) {
$this->entityTypeId = $entity_type->id();
$this->entityType = $entity_type;
$this->languageManager = $language_manager;
}
/**
......@@ -104,7 +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.
$languages = language_list();
$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.
......@@ -137,7 +160,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
'#submit' => array(array($this, 'entityFormSourceChange')),
),
);
foreach (language_list(LanguageInterface::STATE_CONFIGURABLE) as $language) {
foreach ($this->languageManager->getLanguages() as $language) {
if (isset($translations[$language->getId()])) {
$form['source_langcode']['source']['#options'][$language->getId()] = $language->getName();
}
......@@ -158,7 +181,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
$language_select = &$language_widget['widget'][0]['value'];
if ($language_select['#type'] == 'language_select') {
$options = array();
foreach (\Drupal::languageManager()->getLanguages() as $language) {
foreach ($this->languageManager->getLanguages() as $language) {
// Show the current language, and the languages for which no
// translation already exists.
if (empty($translations[$language->getId()]) || $language->getId() == $entity_langcode) {
......@@ -461,7 +484,7 @@ public function entityFormSourceChange($form, FormStateInterface $form_state) {
'source' => $source,
'target' => $form_object->getFormLangcode($form_state),
));
$languages = language_list();
$languages = $this->languageManager->getLanguages();
drupal_set_message(t('Source language set to: %language', array('%language' => $languages[$source]->getName())));
}
......
......@@ -11,6 +11,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
......@@ -28,14 +29,24 @@ class LanguageDeleteForm extends EntityConfirmFormBase {
*/
protected $urlGenerator;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new LanguageDeleteForm object.
*
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The url generator service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(UrlGeneratorInterface $url_generator) {
public function __construct(UrlGeneratorInterface $url_generator, LanguageManagerInterface $language_manager) {
$this->urlGenerator = $url_generator;
$this->languageManager = $language_manager;
}
/**
......@@ -43,7 +54,8 @@ public function __construct(UrlGeneratorInterface $url_generator) {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('url_generator')
$container->get('url_generator'),
$container->get('language_manager')
);
}
......@@ -96,7 +108,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
}
// Throw a 404 when attempting to delete a non-existing language.
$languages = language_list();
$languages = $this->languageManager->getLanguages();
if (!isset($languages[$langcode])) {
throw new NotFoundHttpException();
}
......
......@@ -60,7 +60,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$form = array();
// Initialize a language list to the ones available, including English.
$languages = language_list();
$languages = $this->languageManager->getLanguages();
$existing_languages = array();
foreach ($languages as $langcode => $language) {
......
......@@ -9,6 +9,9 @@
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
/**
......@@ -16,6 +19,36 @@
*/
class NegotiationUrlForm extends ConfigFormBase {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new LanguageDeleteForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
parent::__construct($config_factory);
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('language_manager')
);
}
/**
* {@inheritdoc}
*/
......@@ -69,7 +102,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
),
);
$languages = language_list();
$languages = $this->languageManager->getLanguages();
$prefixes = language_negotiation_url_prefixes();
$domains = language_negotiation_url_domains();
foreach ($languages as $langcode => $language) {
......@@ -98,7 +131,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* Implements \Drupal\Core\Form\FormInterface::validateForm().
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$languages = language_list();
$languages = $this->languageManager->getLanguages();
// Count repeated values for uniqueness check.
$count = array_count_values($form_state->getValue('prefix'));
......
......@@ -10,6 +10,9 @@
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a 'Language' condition.
......@@ -23,15 +26,54 @@
* )
*
*/
class Language extends ConditionPluginBase {
class Language extends ConditionPluginBase implements ContainerFactoryPluginInterface {
/**
* The Language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Creates a new Language instance.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key 'context' may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*/
public function __construct(LanguageManagerInterface $language_manager, array $configuration, $plugin_id, $plugin_definition) {
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(
$container->get('language_manager'),
$configuration,
$plugin_id,
$plugin_definition
);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
if (\Drupal::languageManager()->isMultilingual()) {
if ($this->languageManager->isMultilingual()) {
// Fetch languages.
$languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
$languages = $this->languageManager->getLanguages();
$langcodes_options = array();
foreach ($languages as $language) {
$langcodes_options[$language->getId()] = $language->getName();
......@@ -65,7 +107,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
* {@inheritdoc}
*/
public function summary() {
$language_list = language_list(LanguageInterface::STATE_ALL);
$language_list = $this->languageManager->getLanguages(LanguageInterface::STATE_ALL);
$selected = $this->configuration['langcodes'];
// Reduce the language list to an array of language names.
$language_names = array_reduce($language_list, function(&$result, $item) use ($selected) {
......
......@@ -10,8 +10,8 @@
use Drupal\simpletest\WebTestBase;
/**
* Tests enabling Language if a module exists that calls language_list during
* installation.
* Tests enabling Language if a module exists that calls
* LanguageManager::getLanguages() during installation.
*
* @group language
*/
......@@ -28,8 +28,8 @@ class LanguageListModuleInstallTest extends WebTestBase {
* Tests enabling Language.
*/
function testModuleInstallLanguageList() {
// Since language_list uses static caches we need to do this by enabling
// the module using the UI.
// Since LanguageManager::getLanguages() uses static caches we need to do
// this by enabling the module using the UI.
$admin_user = $this->drupalCreateUser(array('access administration pages', 'administer modules'));
$this->drupalLogin($admin_user);
$edit = array();
......
......@@ -32,7 +32,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$langcode = $filter_values['langcode'];
$this->languageManager->reset();
$languages = language_list();
$languages = $this->languageManager->getLanguages();
$langname = isset($langcode) ? $languages[$langcode]->getName() : "- None -";
......
......@@ -161,7 +161,7 @@ protected function translateFilters() {
// Get all languages, except English.
$this->languageManager->reset();
$languages = language_list();
$languages = $this->languageManager->getLanguages();
$language_options = array();
foreach ($languages as $langcode => $language) {
if ($langcode != 'en' || locale_translate_english()) {
......
......@@ -73,7 +73,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 = language_list();
$languages = \Drupal::languageManager()->getLanguages();
$langcode = $this->getValue($values, 'langcode');
if (isset($languages[$langcode])) {
$this->options['alter']['language'] = $languages[$langcode];
......
......@@ -372,7 +372,7 @@ public function getTimeZone() {
* {@inheritdoc}
*/
function getPreferredLangcode($fallback_to_default = TRUE) {
$language_list = language_list();
$language_list = \Drupal::languageManager()->getLanguages();
$preferred_langcode = $this->get('preferred_langcode')->value;
if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
return $language_list[$preferred_langcode]->getId();
......@@ -386,7 +386,7 @@ function getPreferredLangcode($fallback_to_default = TRUE) {
* {@inheritdoc}
*/
function getPreferredAdminLangcode($fallback_to_default = TRUE) {
$language_list = language_list();
$language_list = \Drupal::languageManager()->getLanguages();
$preferred_langcode = $this->get('preferred_admin_langcode')->value;
if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
return $language_list[$preferred_langcode]->getId();
......
......@@ -33,7 +33,7 @@ protected function renderLink($data, ResultRow $values) {
$lang = language_default();
}
else {
$lang = language_list();
$lang = \Drupal::languageManager()->getLanguages();
$lang = $lang[$data];
}
......
......@@ -15,6 +15,7 @@
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Password\PasswordInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -45,9 +46,11 @@ class UserStorage extends SqlContentEntityStorage implements UserStorageInterfac
* Cache backend instance to use.
* @param \Drupal\Core\Password\PasswordInterface $password
* The password hashing service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, PasswordInterface $password) {
parent::__construct($entity_type, $database, $entity_manager, $cache);
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, PasswordInterface $password, LanguageManagerInterface $language_manager) {
parent::__construct($entity_type, $database, $entity_manager, $cache, $language_manager);
$this->password = $password;
}
......@@ -61,7 +64,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
$container->get('database'),
$container->get('entity.manager'),
$container->get('cache.entity'),
$container->get('password')
$container->get('password'),
$container->get('language_manager')
);
}
......
......@@ -14,6 +14,7 @@
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
......@@ -79,6 +80,13 @@ class SqlContentEntityStorageTest extends UnitTestCase {
*/
protected $cache;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $languageManager;
/**
* The database connection to use.
*
......@@ -101,6 +109,10 @@ protected function setUp() {
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$this->languageManager->expects($this->any())
->method('getDefaultLanguage')
->will($this->returnValue(new Language(array('langcode' => 'en'))));
$this->connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
->disableOriginalConstructor()
->getMock();
......@@ -334,7 +346,7 @@ public function testOnEntityTypeCreate() {
->will($this->returnValue($schema_handler));
$storage = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorage')
->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->cache))
->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->cache, $this->languageManager))
->setMethods(array('getStorageSchema'))
->getMock();
......@@ -1045,7 +1057,7 @@ protected function setUpEntityStorage() {
->method('getBaseFieldDefinitions')
->will($this->returnValue($this->fieldDefinitions));
$this->entityStorage = new SqlContentEntityStorage($this->entityType, $this->connection, $this->entityManager, $this->cache);
$this->entityStorage = new SqlContentEntityStorage($this->entityType, $this->connection, $this->entityManager, $this->cache, $this->languageManager);
}
/**
......@@ -1122,7 +1134,7 @@ public function testLoadMultipleNoPersistentCache() {
->method('set');
$entity_storage = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorage')
->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->cache))
->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->cache, $this->languageManager))
->setMethods(array('getFromStorage'))
->getMock();
$entity_storage->expects($this->once())
......@@ -1172,7 +1184,7 @@ public function testLoadMultiplePersistentCacheMiss() {
->with($key, $entity, CacheBackendInterface::CACHE_PERMANENT, array($this->entityTypeId . '_values', 'entity_field_info'));
$entity_storage = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorage')
->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->cache))
->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->cache, $this->languageManager))
->setMethods(array('getFromStorage'))
->getMock();
$entity_storage->expects($this->once())
......@@ -1227,7 +1239,7 @@ public function testHasData() {
->method('getBaseFieldDefinitions')
->will($this->returnValue($this->fieldDefinitions));
$this->entityStorage = new SqlContentEntityStorage($this->entityType, $database, $this->entityManager, $this->cache);
$this->entityStorage = new SqlContentEntityStorage($this->entityType, $database, $this->entityManager, $this->cache, $this->languageManager);
$result = $this->entityStorage->hasData();
......
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