Skip to content
Snippets Groups Projects
Commit 43dae46e authored by Roger Codina's avatar Roger Codina
Browse files

Merge branch '1615520-add-translatable-configsimpleexample' into '4.0.x'

Issue #1615520: Add translatable config_simple_example

See merge request !83
parents 3a5d4da7 bfa4220a
No related branches found
No related tags found
No related merge requests found
Pipeline #255095 passed with warnings
Showing
with 305 additions and 2 deletions
......@@ -28,8 +28,8 @@ include:
# OPT_IN_TEST_NEXT_MAJOR: '1'
# _CURL_TEMPLATES_REF: 'main'
variables:
SKIP_PHPUNIT: 1
_PHPUNIT_CONCURRENT: 1
SKIP_PHPUNIT: '0'
_PHPUNIT_CONCURRENT: '1'
_CSPELL_IGNORE_PATHS: 'modules/js_example/templates/accordion.html.twig, **/*.svg'
phpunit:
......
......@@ -122,6 +122,7 @@ function _examples_toolbar_routes() {
'block_example' => 'block_example.description',
'cache_example' => 'cache_example.description',
'config_entity_example' => 'entity.robot.list',
'config_simple_example' => 'config_simple_example.description',
'content_entity_example' => 'entity.content_entity_example_contact.collection',
'cron_example' => 'cron_example.description',
'dbtng_example' => 'dbtng_example.generate_entry_list',
......
message: 'Awesome settings'
config_simple_example.settings:
# A config_object is a configuration object which can store fields.
# You have to map all field types the object stores: string, boolean, etc.
# The "text" field type is special because it's translatable by default.
# Checkout how to define schema and all available data types in:
# https://www.drupal.org/docs/drupal-apis/configuration-api/configuration-schemametadata
# Direct link to cheat sheet:
# https://www.drupal.org/files/ConfigSchemaCheatSheet2.0.pdf
# Use Configuration Inspector to validate schema files:
# https://drupal.org/project/config_inspector
type: config_object
label: 'Example config'
mapping:
message:
type: text
label: 'Message'
config_simple_example.settings:
title: 'Config Simple Example Translatable config'
# Route for the translatable configuration form.
base_route_name: config_simple_example.settings
# The name of all yml files that have to be translatable.
names:
- config_simple_example.settings
name: 'Config Simple Example'
type: module
description: 'Demonstrates how to create a custom translatable config form.'
package: Example modules
core_version_requirement: ^10.3
dependencies:
- config_translation:config_translation
- examples:examples
config_simple_example.description:
title: 'Config Simple Example'
description: 'Custom translatable config form'
route_name: config_simple_example.description
expanded: TRUE
config_simple_example.settings:
title: 'Config Simple Example Settings'
route_name: config_simple_example.settings
parent: config_simple_example.description
description: ''
weight: -15
# This file is necessary so the configuration form translation tab is shown.
config_simple_example.settings:
# Route of the form.
route_name: config_simple_example.settings
title: 'Config Simple Example Settings'
# Route of the form.
base_route: config_simple_example.settings
weight: 0
<?php
/**
* @file
* Demonstrates how to create a translatable custom configuration form.
*/
/**
* @defgroup config_simple_example Example: Config Simple
* @ingroup examples
* @{
* Implement a Config Entity.
*
* This module demonstrates implementing a Simple Translatable Config Form.
*
* This is an example of a simple translatable configuration form, the kind you
* might create to store configuration data. This form can store different data
* for each site enabled language.
*/
/**
* @} End of "defgroup config_simple_example".
*/
config_simple_example.description:
path: '/examples/config_simple_example'
defaults:
_controller: '\Drupal\config_simple_example\Controller\ConfigSimpleController::description'
_title: 'Config Simple Example'
requirements:
_permission: 'access content'
config_simple_example.settings:
path: '/admin/config/form-api-example/config-simple-form'
defaults:
_form: '\Drupal\config_simple_example\Form\ConfigSimpleExampleSettingsForm'
_title: 'Config Simple Example'
requirements:
_permission: 'administer site configuration'
<?php
namespace Drupal\config_simple_example\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\examples\Utility\DescriptionTemplateTrait;
/**
* Controller routines for Config Simple Example routes.
*/
class ConfigSimpleController extends ControllerBase {
use DescriptionTemplateTrait;
/**
* {@inheritdoc}
*/
protected function getModuleName() {
return 'config_simple_example';
}
}
<?php
namespace Drupal\config_simple_example\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure example settings for this site.
*/
class ConfigSimpleExampleSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'config_simple_example_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'config_simple_example.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('config_simple_example.settings');
$form['message'] = [
'#type' => 'textarea',
'#title' => $this->t('Message'),
'#default_value' => $config->get('message'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Retrieve the configuration.
$this->config('config_simple_example.settings')
// Set the submitted configuration setting.
->set('message', $form_state->getValue('message'))
->save();
parent::submitForm($form, $form_state);
}
}
{#
Description text for the Config Simple Example.
#}
{% set config_simple_form = path('config_simple_example.settings') %}
{% trans %}
<p>The Config Simple Example module defines a simple translatable config
form. You need "Administer site configuration" permission to test it.
You will need at least two language enabled to test the translation
of the configuration.</p>
<p><a href={{ config_simple_form }}>Config Simple Form</a></p>
{% endtrans %}
<?php
namespace Drupal\Tests\form_api_example\Functional;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\examples\Functional\ExamplesBrowserTestBase;
/**
* Ensure that the form_api_example forms work properly.
*
* @group config_simple_example
* @group examples
*
* @ingroup form_api_example
*/
class ConfigExampleTest extends ExamplesBrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Our module dependencies.
*
* @var string[]
*/
protected static $modules = [
'language',
'locale',
'config_simple_example',
];
/**
* The installation profile to use with this test.
*
* @var string
*/
protected $profile = 'minimal';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// We need more language than default English.
$language = ConfigurableLanguage::createFromLangcode('es');
$language->save();
}
/**
* Aggregate all the tests.
*
* Since this is a functional test, and we don't expect to need isolation
* between these form tests, we'll aggregate them here for speed's sake. That
* way the testing system doesn't have to rebuild a new Drupal for us for each
* test.
*/
public function testFunctional() {
// Please fail this one first.
$this->doTestMainPage();
$this->doTestConfigSimpleForm();
}
/**
* Test the main page.
*/
public function doTestMainPage() {
$assert = $this->assertSession();
$example_page = Url::fromRoute('config_simple_example.description');
$this->drupalGet($example_page);
$assert->pageTextContains('The Config Simple Example module defines a simple translatable config form.');
$assert->linkExists('Config Simple Form');
}
/**
* Test the config simple form.
*/
public function doTestConfigSimpleForm() {
$assert = $this->assertSession();
$config_page = Url::fromRoute('config_simple_example.settings');
$this->drupalGet($config_page);
$assert->fieldEnabled('edit-message');
$assert->buttonExists('edit-submit');
// Ensure the 'Save configuration' action performs the save.
$this->drupalGet($config_page);
$edit = [
'message' => 'Message in EN',
];
$this->submitForm($edit, 'Save configuration');
$assert->pageTextContains('Message in EN');
// Go to Spanish version.
$es_lang_object = \Drupal::languageManager()->getLanguage('es');
$config_page_es = Url::fromRoute('config_simple_example.settings', [], ['language' => $es_lang_object]);
$this->drupalGet($config_page_es);
$assert->fieldEnabled('edit-message');
$assert->buttonExists('edit-submit');
// Ensure the 'Save configuration' action performs the save.
$this->drupalGet($config_page);
$edit = [
'message' => 'Message in ES',
];
$this->submitForm($edit, 'Save configuration');
$assert->pageTextContains('Message in ES');
// Go back to original English version.
$this->drupalGet($config_page);
$assert->pageTextContains('Message in EN');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment