Issue #2570285 by mr.baileys, cpj, stefan.r, alexpott, nlisgo, joelpittet, Aki Tendo, borisson_, chx, dawehner, s_leu, Berdir: Make sure TranslatableMarkup accepts string values only
parent
18a5c79333
commit
1672445a15
|
@ -72,9 +72,16 @@ class TranslatableMarkup extends FormattableMarkup {
|
||||||
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
|
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
|
||||||
* (optional) The string translation service.
|
* (optional) The string translation service.
|
||||||
*
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
* Exception thrown when $string is not a string.
|
||||||
|
*
|
||||||
* @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
|
* @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
|
||||||
*/
|
*/
|
||||||
public function __construct($string, array $arguments = array(), array $options = array(), TranslationInterface $string_translation = NULL) {
|
public function __construct($string, array $arguments = array(), array $options = array(), TranslationInterface $string_translation = NULL) {
|
||||||
|
if (!is_string($string)) {
|
||||||
|
$message = $string instanceof TranslatableMarkup ? '$string ("' . $string->getUntranslatedString() . '") must be a string.' : '$string ("' . (string) $string . '") must be a string.';
|
||||||
|
throw new \InvalidArgumentException($message);
|
||||||
|
}
|
||||||
$this->string = $string;
|
$this->string = $string;
|
||||||
$this->arguments = $arguments;
|
$this->arguments = $arguments;
|
||||||
$this->options = $options;
|
$this->options = $options;
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
namespace Drupal\Core\Validation;
|
namespace Drupal\Core\Validation;
|
||||||
|
|
||||||
use Drupal\Component\Render\MarkupInterface;
|
use Drupal\Component\Render\MarkupInterface;
|
||||||
|
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Translates strings using Drupal's translation system.
|
* Translates strings using Drupal's translation system.
|
||||||
|
@ -27,8 +28,9 @@ class DrupalTranslator implements TranslatorInterface {
|
||||||
* Implements \Symfony\Component\Translation\TranslatorInterface::trans().
|
* Implements \Symfony\Component\Translation\TranslatorInterface::trans().
|
||||||
*/
|
*/
|
||||||
public function trans($id, array $parameters = array(), $domain = NULL, $locale = NULL) {
|
public function trans($id, array $parameters = array(), $domain = NULL, $locale = NULL) {
|
||||||
|
// If a TranslatableMarkup object is passed in as $id, return it since the
|
||||||
return t($id, $this->processParameters($parameters), $this->getOptions($domain, $locale));
|
// message has already been translated.
|
||||||
|
return $id instanceof TranslatableMarkup ? $id : t($id, $this->processParameters($parameters), $this->getOptions($domain, $locale));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -23,9 +23,11 @@ class FormTest extends FieldTestBase {
|
||||||
/**
|
/**
|
||||||
* Modules to enable.
|
* Modules to enable.
|
||||||
*
|
*
|
||||||
|
* Locale is installed so that TranslatableMarkup actually does something.
|
||||||
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public static $modules = array('node', 'field_test', 'options', 'entity_test');
|
public static $modules = array('node', 'field_test', 'options', 'entity_test', 'locale');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An array of values defining a field single.
|
* An array of values defining a field single.
|
||||||
|
|
|
@ -47,7 +47,7 @@ function module_test_system_info_alter(&$info, Extension $file, $type) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($file->getName() == 'seven' && $type == 'theme') {
|
if ($file->getName() == 'seven' && $type == 'theme') {
|
||||||
$info['regions']['test_region'] = t('Test region');
|
$info['regions']['test_region'] = 'Test region';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
namespace Drupal\Tests\Core\StringTranslation;
|
namespace Drupal\Tests\Core\StringTranslation;
|
||||||
|
|
||||||
|
use Drupal\Component\Render\FormattableMarkup;
|
||||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||||
use Drupal\Tests\UnitTestCase;
|
use Drupal\Tests\UnitTestCase;
|
||||||
|
@ -85,4 +86,27 @@ class TranslatableMarkupTest extends UnitTestCase {
|
||||||
$this->assertRegExp('/Exception thrown while calling __toString on a .*Mock_TranslatableMarkup_.* object in .*TranslatableMarkupTest.php on line [0-9]+: Yes you may./', $this->lastErrorMessage);
|
$this->assertRegExp('/Exception thrown while calling __toString on a .*Mock_TranslatableMarkup_.* object in .*TranslatableMarkupTest.php on line [0-9]+: Yes you may./', $this->lastErrorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage $string ("foo") must be a string.
|
||||||
|
*
|
||||||
|
* @covers ::__construct
|
||||||
|
*/
|
||||||
|
public function testIsStringAssertion() {
|
||||||
|
$translation = $this->getStringTranslationStub();
|
||||||
|
new TranslatableMarkup(new TranslatableMarkup('foo', [], [], $translation));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage $string ("foo") must be a string.
|
||||||
|
*
|
||||||
|
* @covers ::__construct
|
||||||
|
*/
|
||||||
|
public function testIsStringAssertionWithFormattableMarkup() {
|
||||||
|
$translation = $this->getStringTranslationStub();
|
||||||
|
$formattable_string = new FormattableMarkup('@bar', ['@bar' => 'foo']);
|
||||||
|
new TranslatableMarkup($formattable_string);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue