Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal-3443205
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
drupal-3443205
Commits
2af2fb45
Commit
2af2fb45
authored
10 years ago
by
Alex Pott
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2420239
by andypost, pjonckiere: Default language setting form needs validation
parent
71a987d0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/modules/language/src/LanguageListBuilder.php
+33
-17
33 additions, 17 deletions
core/modules/language/src/LanguageListBuilder.php
core/modules/language/src/Tests/LanguageListTest.php
+7
-0
7 additions, 0 deletions
core/modules/language/src/Tests/LanguageListTest.php
with
40 additions
and
17 deletions
core/modules/language/src/LanguageListBuilder.php
+
33
−
17
View file @
2af2fb45
...
...
@@ -7,6 +7,7 @@
namespace
Drupal\language
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Config\Entity\DraggableListBuilder
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Entity\EntityStorageInterface
;
...
...
@@ -35,6 +36,13 @@ class LanguageListBuilder extends DraggableListBuilder {
*/
protected
$languageManager
;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected
$configFactory
;
/**
* {@inheritdoc}
*/
...
...
@@ -42,7 +50,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
return
new
static
(
$entity_type
,
$container
->
get
(
'entity.manager'
)
->
getStorage
(
$entity_type
->
id
()),
$container
->
get
(
'language_manager'
)
$container
->
get
(
'language_manager'
),
$container
->
get
(
'config.factory'
)
);
}
...
...
@@ -55,10 +64,13 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
* The entity storage controller class.
* @param \Drupal\Core\Language\LanguageManagerInterface
* The language manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
*/
public
function
__construct
(
EntityTypeInterface
$entity_type
,
EntityStorageInterface
$storage
,
LanguageManagerInterface
$language_manager
)
{
public
function
__construct
(
EntityTypeInterface
$entity_type
,
EntityStorageInterface
$storage
,
LanguageManagerInterface
$language_manager
,
ConfigFactoryInterface
$config_factory
)
{
parent
::
__construct
(
$entity_type
,
$storage
);
$this
->
languageManager
=
$language_manager
;
$this
->
configFactory
=
$config_factory
;
}
/**
...
...
@@ -104,6 +116,10 @@ public function buildRow(EntityInterface $entity) {
'#return_value'
=>
$entity
->
id
(),
'#id'
=>
'edit-site-default-language-'
.
$entity
->
id
(),
);
// Mark the right language as default in the form.
if
(
$entity
->
id
()
==
$this
->
languageManager
->
getDefaultLanguage
()
->
getId
())
{
$row
[
'default'
][
'#default_value'
]
=
$entity
->
id
();
}
return
$row
+
parent
::
buildRow
(
$entity
);
}
...
...
@@ -113,33 +129,33 @@ public function buildRow(EntityInterface $entity) {
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$form
=
parent
::
buildForm
(
$form
,
$form_state
);
// Mark the right language as default in the form.
$default
=
\Drupal
::
languageManager
()
->
getDefaultLanguage
();
foreach
(
Element
::
children
(
$form
[
$this
->
entitiesKey
])
as
$key
)
{
if
(
$key
==
$default
->
getId
())
{
$form
[
$this
->
entitiesKey
][
$key
][
'default'
][
'#default_value'
]
=
$default
->
getId
();
}
}
$form
[
$this
->
entitiesKey
][
'#languages'
]
=
$this
->
entities
;
$form
[
'actions'
][
'submit'
][
'#value'
]
=
t
(
'Save configuration'
);
return
$form
;
}
/**
* {@inheritdoc}
*/
public
function
validateForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
if
(
!
isset
(
$this
->
entities
[
$form_state
->
getValue
(
'site_default_language'
)]))
{
$form_state
->
setErrorByName
(
'site_default_language'
,
$this
->
t
(
'Selected default language no longer exists.'
));
}
}
/**
* {@inheritdoc}
*/
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
parent
::
submitForm
(
$form
,
$form_state
);
// Save the default language.
foreach
(
$form_state
->
getValue
(
$this
->
entitiesKey
)
as
$id
=>
$value
)
{
if
(
isset
(
$this
->
entities
[
$id
])
&&
(
$
id
=
=
$
form_state
->
getValue
(
'site_d
efault
_l
anguage
'
)
))
{
\Drupal
::
configFactory
()
->
getEditable
(
'system.site'
)
->
set
(
'langcode'
,
$
form_state
->
getValue
(
'site_default_language'
)
)
->
save
();
}
// Save the default language
if changed
.
$new_id
=
$form_state
->
getValue
(
'site_default_language'
);
if
(
$new_
id
!
=
$
this
->
languageManager
->
getD
efault
L
anguage
()
->
getId
(
))
{
$this
->
configFactory
->
getEditable
(
'system.site'
)
->
set
(
'langcode'
,
$
new_id
)
->
save
();
$this
->
languageManager
->
reset
();
}
$this
->
languageManager
->
reset
();
if
(
$this
->
languageManager
instanceof
ConfigurableLanguageManagerInterface
)
{
$this
->
languageManager
->
updateLockedLanguageWeights
();
}
...
...
@@ -147,7 +163,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
drupal_set_message
(
t
(
'Configuration saved.'
));
// Force the redirection to the page with the language we have just
// selected as default.
$form_state
->
setRedirect
(
'entity.configurable_language.
collection'
,
array
(),
array
(
'language'
=>
$this
->
entities
[
$
form_state
->
getValue
(
'site_default_language'
)]
));
$form_state
->
setRedirect
Url
(
$this
->
entities
[
$new_id
]
->
urlInfo
(
'
collection'
,
array
(
'language'
=>
$this
->
entities
[
$
new_id
])
));
}
}
This diff is collapsed.
Click to expand it.
core/modules/language/src/Tests/LanguageListTest.php
+
7
−
0
View file @
2af2fb45
...
...
@@ -179,6 +179,13 @@ function testLanguageList() {
// Ensure we can't delete a locked language.
$this
->
drupalGet
(
'admin/config/regional/language/delete/und'
);
$this
->
assertResponse
(
403
,
'Can not delete locked language'
);
// Ensure that NL cannot be set default when it's not available.
$this
->
drupalGet
(
'admin/config/regional/language'
);
$extra_values
=
'&site_default_language=nl'
;
$this
->
drupalPostForm
(
NULL
,
array
(),
t
(
'Save configuration'
),
array
(),
array
(),
NULL
,
$extra_values
);
$this
->
assertText
(
t
(
'Selected default language no longer exists.'
));
$this
->
assertNoFieldChecked
(
'edit-site-default-language-xx'
,
'The previous default language got deselected.'
);
}
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment