diff --git a/core/lib/Drupal/Core/StringTranslation/Translator/CustomStrings.php b/core/lib/Drupal/Core/StringTranslation/Translator/CustomStrings.php index 9d9a3b326588e97aa4ed0f109edf697d728a8add..8e733d5488a0e7ca32fcf27d9ee77023a33cd0f1 100644 --- a/core/lib/Drupal/Core/StringTranslation/Translator/CustomStrings.php +++ b/core/lib/Drupal/Core/StringTranslation/Translator/CustomStrings.php @@ -3,6 +3,7 @@ namespace Drupal\Core\StringTranslation\Translator; use Drupal\Core\Site\Settings; +use Drupal\Core\DependencyInjection\DependencySerializationTrait; /** * String translator using overrides from variables. @@ -12,6 +13,8 @@ */ class CustomStrings extends StaticTranslation { + use DependencySerializationTrait; + /** * The settings read only object. * diff --git a/core/modules/datetime/tests/src/Kernel/DateTimeFormInjectionTest.php b/core/modules/datetime/tests/src/Kernel/DateTimeFormInjectionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a65d43928e0f60666257f34cfcb24b10c8014926 --- /dev/null +++ b/core/modules/datetime/tests/src/Kernel/DateTimeFormInjectionTest.php @@ -0,0 +1,113 @@ +<?php + +namespace Drupal\Tests\datetime\Kernel; + +use Drupal\Core\Datetime\DrupalDateTime; +use Drupal\Core\DependencyInjection\DependencySerializationTrait; +use Drupal\Core\Form\FormInterface; +use Drupal\Core\Form\FormState; +use Drupal\Core\Form\FormStateInterface; +use Drupal\KernelTests\KernelTestBase; + +/** + * Tests serializing a form with an injected datetime instance. + * + * @group datetime + */ +class DateTimeFormInjectionTest extends KernelTestBase implements FormInterface { + + use DependencySerializationTrait; + + /** + * A Dblog logger instance. + * + * @var \Psr\Log\LoggerInterface + */ + protected $logger; + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = ['system', 'datetime']; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + $this->installSchema('system', ['key_value_expire', 'sequences']); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'datetime_test_injection_form'; + } + + /** + * Process callback. + * + * @param array $element + * Form element. + * + * @return array + * Processed element. + */ + public function process($element) { + return $element; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form['datelist_element'] = [ + '#title' => 'datelist test', + '#type' => 'datelist', + '#default_value' => new DrupalDateTime('2000-01-01 00:00:00'), + '#date_part_order' => [ + 'month', + 'day', + 'year', + 'hour', + 'minute', 'ampm', + ], + '#date_text_parts' => ['year'], + '#date_year_range' => '2010:2020', + '#date_increment' => 15, + ]; + $form['#process'][] = [$this, 'process']; + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) {} + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->assertTrue(TRUE); + $form_state->setRebuild(); + } + + /** + * Tests custom string injection serialization. + */ + public function testDatetimeSerialization() { + $form_state = new FormState(); + $form_state->setRequestMethod('POST'); + $form_state->setCached(); + $form_builder = $this->container->get('form_builder'); + $form_id = $form_builder->getFormId($this, $form_state); + $form = $form_builder->retrieveForm($form_id, $form_state); + $form_builder->prepareForm($form_id, $form, $form_state); + $form_builder->processForm($form_id, $form, $form_state); + } + +}