diff --git a/core/core.services.yml b/core/core.services.yml index b814db2..d9e02cc 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -108,7 +108,7 @@ services: arguments: [default] form_builder: class: Drupal\Core\Form\FormBuilder - arguments: ['@module_handler', '@keyvalue.expirable', '@event_dispatcher', '@url_generator', '@?csrf_token', '@?http_kernel'] + arguments: ['@module_handler', '@keyvalue.expirable', '@event_dispatcher', '@url_generator', '@string_translation', '@?csrf_token', '@?http_kernel'] calls: - [setRequest, ['@?request']] keyvalue: diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 1ca05e3..6395881 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -461,6 +461,7 @@ function install_begin_request(&$install_state) { ->addArgument(new Reference('keyvalue.expirable')) ->addArgument(new Reference('event_dispatcher')) ->addArgument(new Reference('url_generator')) + ->addArgument(new Reference('string_translation')) ->addArgument(new Reference('csrf_token', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)) ->addArgument(new Reference('http_kernel', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)) ->addMethodCall('setRequest', array(new Reference('request'))); diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index d9c8375..a55f70a 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -13,6 +13,7 @@ use Drupal\Core\HttpKernel; use Drupal\Core\KeyValueStore\KeyValueExpirableFactory; use Drupal\Core\Routing\UrlGeneratorInterface; +use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -55,6 +56,13 @@ class FormBuilder implements FormBuilderInterface { protected $urlGenerator; /** + * The translation manager service. + * + * @var \Drupal\Core\StringTranslation\TranslationInterface + */ + protected $translationManager; + + /** * The current request. * * @var \Symfony\Component\HttpFoundation\Request @@ -121,16 +129,19 @@ class FormBuilder implements FormBuilderInterface { * The event dispatcher. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * The URL generator. + * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * The translation manager. * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token * The CSRF token generator. * @param \Drupal\Core\HttpKernel $http_kernel * The HTTP kernel. */ - public function __construct(ModuleHandlerInterface $module_handler, KeyValueExpirableFactory $key_value_expirable_factory, EventDispatcherInterface $event_dispatcher, UrlGeneratorInterface $url_generator, CsrfTokenGenerator $csrf_token = NULL, HttpKernel $http_kernel = NULL) { + public function __construct(ModuleHandlerInterface $module_handler, KeyValueExpirableFactory $key_value_expirable_factory, EventDispatcherInterface $event_dispatcher, UrlGeneratorInterface $url_generator, TranslationInterface $translation_manager, CsrfTokenGenerator $csrf_token = NULL, HttpKernel $http_kernel = NULL) { $this->moduleHandler = $module_handler; $this->keyValueExpirableFactory = $key_value_expirable_factory; $this->eventDispatcher = $event_dispatcher; $this->urlGenerator = $url_generator; + $this->translationManager = $translation_manager; $this->csrfToken = $csrf_token; $this->httpKernel = $http_kernel; } @@ -596,7 +607,7 @@ public function processForm($form_id, &$form, &$form_state) { // possibly ending execution. We make sure we do not react to the batch // that is already being processed (if a batch operation performs a // self::submitForm). - if ($batch =& $this->batchGet() && !isset($batch['current_set'])) { + if ($batch =& batch_get() && !isset($batch['current_set'])) { // Store $form_state information in the batch definition. // We need the full $form_state when either: // - Some submit handlers were saved to be called during batch @@ -982,7 +993,7 @@ public function validateForm($form_id, &$form, &$form_state) { $url = $this->urlGenerator->generateFromPath($path, array('query' => $query)); // Setting this error will cause the form to fail validation. - $this->setErrorByName('form_token', t('The form has become outdated. Copy any unsaved work in the form below and then reload this page.', array('@link' => $url))); + $this->setErrorByName('form_token', $this->t('The form has become outdated. Copy any unsaved work in the form below and then reload this page.', array('@link' => $url))); } } @@ -1067,7 +1078,7 @@ protected function doValidateForm(&$elements, &$form_state, $form_id = NULL) { if (isset($elements['#needs_validation'])) { // Verify that the value is not longer than #maxlength. if (isset($elements['#maxlength']) && Utility\Unicode::strlen($elements['#value']) > $elements['#maxlength']) { - $this->setError($elements, t('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'], '%max' => $elements['#maxlength'], '%length' => Utility\Unicode::strlen($elements['#value'])))); + $this->setError($elements, $this->t('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'], '%max' => $elements['#maxlength'], '%length' => Utility\Unicode::strlen($elements['#value'])))); } if (isset($elements['#options']) && isset($elements['#value'])) { @@ -1081,7 +1092,7 @@ protected function doValidateForm(&$elements, &$form_state, $form_id = NULL) { $value = in_array($elements['#type'], array('checkboxes', 'tableselect')) ? array_keys($elements['#value']) : $elements['#value']; foreach ($value as $v) { if (!isset($options[$v])) { - $this->setError($elements, t('An illegal choice has been detected. Please contact the site administrator.')); + $this->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.')); $this->watchdog('form', 'Illegal choice %choice in !name element.', array('%choice' => $v, '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR); } } @@ -1100,7 +1111,7 @@ protected function doValidateForm(&$elements, &$form_state, $form_id = NULL) { $this->setValue($elements, NULL, $form_state); } elseif (!isset($options[$elements['#value']])) { - $this->setError($elements, t('An illegal choice has been detected. Please contact the site administrator.')); + $this->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.')); $this->watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR); } } @@ -1183,7 +1194,7 @@ protected function doValidateForm(&$elements, &$form_state, $form_id = NULL) { // constructors are encouraged to set #title anyway, and then set // #title_display to 'invisible'. This improves accessibility. elseif (isset($elements['#title'])) { - $this->setError($elements, t('!name field is required.', array('!name' => $elements['#title']))); + $this->setError($elements, $this->t('!name field is required.', array('!name' => $elements['#title']))); } else { $this->setError($elements); @@ -1253,7 +1264,7 @@ public function executeHandlers($type, &$form, &$form_state) { // Check if a previous _submit handler has set a batch, but make sure we // do not react to a batch that is already being processed (for instance // if a batch operation performs a self::submitForm()). - if ($type == 'submit' && ($batch =& $this->batchGet()) && !isset($batch['id'])) { + if ($type == 'submit' && ($batch =& batch_get()) && !isset($batch['id'])) { // Some previous submit handler has set a batch. To ensure correct // execution order, store the call in a special 'control' batch set. // See _batch_next_set(). @@ -1652,15 +1663,6 @@ protected function drupalInstallationAttempted() { } /** - * Wraps batch_get(). - * - * @return array() - */ - protected function &batchGet() { - return batch_get(); - } - - /** * Wraps menu_get_item(). * * @return array|bool @@ -1738,6 +1740,15 @@ protected function currentUser() { } /** + * Translates a string to the current language or to a given language. + * + * See the t() documentation for details. + */ + protected function t($string, array $args = array(), array $options = array()) { + return $this->translationManager->translate($string, $args, $options); + } + + /** * Sets the request object to use. * * @param \Symfony\Component\HttpFoundation\Request $request diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index 6e13dfe..a80ece9 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -62,8 +62,12 @@ public function setUp() { $event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface'); + $translation_manager = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface'); + $translation_manager->expects($this->any()) + ->method('translate') + ->will($this->returnArgument(0)); - $this->formBuilder = new TestFormBuilder($this->moduleHandler, $key_value_expirable_factory, $event_dispatcher, $this->urlGenerator); + $this->formBuilder = new TestFormBuilder($this->moduleHandler, $key_value_expirable_factory, $event_dispatcher, $this->urlGenerator, $translation_manager); $this->formBuilder->setRequest(new Request()); $account = $this->getMock('Drupal\Core\Session\AccountInterface'); @@ -429,13 +433,6 @@ protected function getElementInfo($type) { /** * {@inheritdoc} */ - protected function batchGet() { - return array(); - } - - /** - * {@inheritdoc} - */ protected function drupalInstallationAttempted() { return FALSE; } @@ -528,4 +525,9 @@ function test_form_id() { if (!defined('WATCHDOG_ERROR')) { define('WATCHDOG_ERROR', 3); } + if (!function_exists('batch_get')) { + function batch_get() { + return array(); + } + } }