summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2017-02-14 09:23:39 +0000
committerAlex Pott2017-02-14 09:23:39 +0000
commitb67ccdc14004baa3446cedead6caa1615e08f481 (patch)
tree418423fc96705ccc88567b9272c16e5f82ff9c9d
parentfee5c769222707792e4e463e53ee4234b9de9b5e (diff)
Issue #2827784 by maxocub, faline, cebasqueira, juancasantito, tstoeckler, Wim Leers, alexpott: LanguageSelectWidget should have options for which language types to show
-rw-r--r--core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php27
-rw-r--r--core/modules/language/config/schema/language.schema.yml8
-rw-r--r--core/modules/language/language.post_update.php37
-rw-r--r--core/modules/language/src/Tests/Update/LanguageSelectWidgetUpdateTest.php40
-rw-r--r--core/modules/language/tests/src/Kernel/LanguageSelectWidgetTest.php77
5 files changed, 188 insertions, 1 deletions
diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php
index cff646d..697c603 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php
@@ -27,10 +27,35 @@ class LanguageSelectWidget extends WidgetBase {
$element['value'] = $element + array(
'#type' => 'language_select',
'#default_value' => $items[$delta]->value,
- '#languages' => LanguageInterface::STATE_ALL,
+ '#languages' => $this->getSetting('include_locked') ? LanguageInterface::STATE_ALL : LanguageInterface::STATE_CONFIGURABLE,
);
return $element;
}
+ /**
+ * {@inheritdoc}
+ */
+ public static function defaultSettings() {
+ $settings = parent::defaultSettings();
+ $settings['include_locked'] = TRUE;
+
+ return $settings;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsForm(array $form, FormStateInterface $form_state) {
+ $element = parent::settingsForm($form, $form_state);
+
+ $element['include_locked'] = [
+ '#type' => 'checkbox',
+ '#title' => $this->t('Include locked languages such as <em>Not specified</em> and <em>Not applicable</em>'),
+ '#default_value' => $this->getSetting('include_locked'),
+ ];
+
+ return $element;
+ }
+
}
diff --git a/core/modules/language/config/schema/language.schema.yml b/core/modules/language/config/schema/language.schema.yml
index 5e3f55b..91ce2d1 100644
--- a/core/modules/language/config/schema/language.schema.yml
+++ b/core/modules/language/config/schema/language.schema.yml
@@ -131,3 +131,11 @@ condition.plugin.language:
type: sequence
sequence:
type: string
+
+field.widget.settings.language_select:
+ type: mapping
+ label: 'Language format settings'
+ mapping:
+ include_locked:
+ type: boolean
+ label: 'Include locked languages'
diff --git a/core/modules/language/language.post_update.php b/core/modules/language/language.post_update.php
new file mode 100644
index 0000000..d1138d0
--- /dev/null
+++ b/core/modules/language/language.post_update.php
@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * @file
+ * Post update functions for Language module.
+ */
+
+use Drupal\Core\Entity\Entity\EntityFormDisplay;
+
+/**
+ * @addtogroup updates-8.4.0
+ * @{
+ */
+
+/**
+ * Add the 'include_locked' settings to the 'language_select' widget.
+ */
+function language_post_update_language_select_widget() {
+ foreach (EntityFormDisplay::loadMultiple() as $display_form) {
+ $content = $display_form->get('content');
+ $changed = FALSE;
+ foreach (array_keys($content) as $element) {
+ if (isset($content[$element]['type']) && $content[$element]['type'] == 'language_select') {
+ $content[$element]['settings']['include_locked'] = TRUE;
+ $changed = TRUE;
+ }
+ }
+ if ($changed) {
+ $display_form->set('content', $content);
+ $display_form->save();
+ }
+ }
+}
+
+/**
+ * @} End of "addtogroup updates-8.4.0".
+ */
diff --git a/core/modules/language/src/Tests/Update/LanguageSelectWidgetUpdateTest.php b/core/modules/language/src/Tests/Update/LanguageSelectWidgetUpdateTest.php
new file mode 100644
index 0000000..b055983
--- /dev/null
+++ b/core/modules/language/src/Tests/Update/LanguageSelectWidgetUpdateTest.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Drupal\language\Tests\Update;
+
+use Drupal\system\Tests\Update\UpdatePathTestBase;
+use Drupal\Core\Entity\Entity\EntityFormDisplay;
+
+/**
+ * Tests the update path for the language_select widget.
+ *
+ * @group Update
+ */
+class LanguageSelectWidgetUpdateTest extends UpdatePathTestBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setDatabaseDumpFiles() {
+ $this->databaseDumpFiles = [
+ __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.filled.standard.php.gz',
+ ];
+ }
+
+ /**
+ * Tests language_post_update_language_select_widget().
+ */
+ public function testLanguagePostUpdateLanguageSelectWidget() {
+ // Tests before the update.
+ $content_before = EntityFormDisplay::load('node.page.default')->get('content');
+ $this->assertEqual([], $content_before['langcode']['settings']);
+
+ // Run the update.
+ $this->runUpdates();
+
+ // Tests after the update.
+ $content_after = EntityFormDisplay::load('node.page.default')->get('content');
+ $this->assertEqual(['include_locked' => TRUE], $content_after['langcode']['settings']);
+ }
+
+}
diff --git a/core/modules/language/tests/src/Kernel/LanguageSelectWidgetTest.php b/core/modules/language/tests/src/Kernel/LanguageSelectWidgetTest.php
new file mode 100644
index 0000000..966275c
--- /dev/null
+++ b/core/modules/language/tests/src/Kernel/LanguageSelectWidgetTest.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Drupal\Tests\language\Kernel;
+
+use Drupal\entity_test\Entity\EntityTest;
+use Drupal\KernelTests\KernelTestBase;
+
+/**
+ * Tests the language select widget.
+ *
+ * @group language
+ */
+class LanguageSelectWidgetTest extends KernelTestBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $modules = [
+ 'entity_test',
+ 'language',
+ 'user',
+ 'system',
+ ];
+
+ /**
+ * The entity form display.
+ *
+ * @var \Drupal\Core\Entity\Entity\EntityFormDisplay
+ */
+ protected $entityFormDisplay;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setUp() {
+ parent::setUp();
+
+ $this->installEntitySchema('entity_test');
+ $this->installEntitySchema('user');
+
+ $storage = $this->container->get('entity_type.manager')->getStorage('entity_form_display');
+ $this->entityFormDisplay = $storage->create([
+ 'targetEntityType' => 'entity_test',
+ 'bundle' => 'entity_test',
+ 'mode' => 'default',
+ 'status' => TRUE,
+ ]);
+ }
+
+ /**
+ * Tests the widget with the locked languages.
+ */
+ public function testWithIncludedLockedLanguage() {
+ $this->entityFormDisplay->setComponent('langcode', [
+ 'type' => 'language_select',
+ ])->save();
+ $entity = EntityTest::create(['name' => $this->randomString()]);
+ $form = $this->container->get('entity.form_builder')->getForm($entity);
+ $options = array_keys($form['langcode']['widget'][0]['value']['#options']);
+ $this->assertSame(['en', 'und', 'zxx'], $options);
+ }
+
+ /**
+ * Test the widget without the locked languages.
+ */
+ public function testWithoutIncludedLockedLanguage() {
+ $this->entityFormDisplay->setComponent('langcode', [
+ 'type' => 'language_select',
+ 'settings' => ['include_locked' => FALSE],
+ ])->save();
+ $entity = EntityTest::create(['name' => $this->randomString()]);
+ $form = $this->container->get('entity.form_builder')->getForm($entity);
+ $options = array_keys($form['langcode']['widget'][0]['value']['#options']);
+ $this->assertSame(['en'], $options);
+ }
+
+}