Commit b67ccdc1 authored by alexpott's avatar alexpott

Issue #2827784 by maxocub, faline, cebasqueira, juancasantito, tstoeckler, Wim...

Issue #2827784 by maxocub, faline, cebasqueira, juancasantito, tstoeckler, Wim Leers, alexpott: LanguageSelectWidget should have options for which language types to show
parent fee5c769
......@@ -27,10 +27,35 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$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;
}
}
......@@ -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'
<?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".
*/
<?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']);
}
}
<?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);
}
}
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