Issue #2807743 by alexpott, catch, kiamlaluno, neclimdul, xjm: Switch from deprecation notice to warning for non-standard placeholders in FormattableMarkup::placeholderFormat()
parent
65e525854c
commit
e85462dda4
|
@ -149,6 +149,10 @@ function error_displayable($error = NULL) {
|
|||
function _drupal_log_error($error, $fatal = FALSE) {
|
||||
$is_installer = InstallerKernel::installationAttempted();
|
||||
|
||||
// Remove 'severity_level' as is not a valid replacement for t().
|
||||
$severity = $error['severity_level'];
|
||||
unset($error['severity_level']);
|
||||
|
||||
// Backtrace array is not a valid replacement value for t().
|
||||
$backtrace = $error['backtrace'];
|
||||
unset($error['backtrace']);
|
||||
|
@ -166,8 +170,10 @@ function _drupal_log_error($error, $fatal = FALSE) {
|
|||
// installer.
|
||||
if (\Drupal::hasService('logger.factory')) {
|
||||
try {
|
||||
// Provide the PHP backtrace to logger implementations.
|
||||
\Drupal::logger('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file) @backtrace_string.', $error + ['backtrace' => $backtrace]);
|
||||
// Provide the PHP backtrace to logger implementations. Add
|
||||
// 'severity_level' to the context to maintain BC and allow logging
|
||||
// implementations to use it.
|
||||
\Drupal::logger('php')->log($severity, '%type: @message in %function (line %line of %file) @backtrace_string.', $error + ['backtrace' => $backtrace, 'severity_level' => $severity]);
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
// We can't log, for example because the database connection is not
|
||||
|
|
|
@ -232,6 +232,7 @@ function update_do_one($module, $number, $dependency_map, &$context) {
|
|||
|
||||
$variables = Error::decodeException($e);
|
||||
unset($variables['backtrace']);
|
||||
unset($variables['severity_level']);
|
||||
$ret['#abort'] = ['success' => FALSE, 'query' => t('%type: @message in %function (line %line of %file).', $variables)];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -231,17 +231,12 @@ class FormattableMarkup implements MarkupInterface, \Countable {
|
|||
break;
|
||||
|
||||
default:
|
||||
// We do not trigger an error for placeholder that start with an
|
||||
// alphabetic character.
|
||||
// @todo https://www.drupal.org/node/2807743 Change to an exception
|
||||
// and always throw regardless of the first character.
|
||||
if (!ctype_alpha($key[0])) {
|
||||
// We trigger an error as we may want to introduce new placeholders
|
||||
// in the future without breaking backward compatibility.
|
||||
trigger_error('Invalid placeholder (' . $key . ') in string: ' . $string, E_USER_ERROR);
|
||||
// Deprecate support for random variables that won't be replaced.
|
||||
if (ctype_alpha($key[0]) && strpos($string, $key) === FALSE) {
|
||||
@trigger_error(sprintf('Support for keys without a placeholder prefix is deprecated in Drupal 9.1.0 and will be removed in Drupal 10.0.0. Invalid placeholder (%s) with string: "%s"', $key, $string), E_USER_DEPRECATED);
|
||||
}
|
||||
elseif (strpos($string, $key) !== FALSE) {
|
||||
trigger_error('Invalid placeholder (' . $key . ') in string: ' . $string, E_USER_DEPRECATED);
|
||||
else {
|
||||
trigger_error(sprintf('Invalid placeholder (%s) with string: "%s"', $key, $string), E_USER_WARNING);
|
||||
}
|
||||
// No replacement possible therefore we can discard the argument.
|
||||
unset($args[$key]);
|
||||
|
|
|
@ -94,6 +94,7 @@ class FinalExceptionSubscriber implements EventSubscriberInterface {
|
|||
$error = $this->simplifyFileInError($error);
|
||||
|
||||
unset($error['backtrace']);
|
||||
unset($error['severity_level']);
|
||||
|
||||
if (!$this->isErrorLevelVerbose()) {
|
||||
// Without verbose logging, use a simple message.
|
||||
|
|
|
@ -63,7 +63,9 @@ class ViewsIntegrationTest extends ViewsKernelTestBase {
|
|||
if (!isset($entry['variables'])) {
|
||||
continue;
|
||||
}
|
||||
$this->assertEqual($view->style_plugin->getField($index, 'message'), new FormattableMarkup($entry['message'], $entry['variables']));
|
||||
$message_vars = $entry['variables'];
|
||||
unset($message_vars['link']);
|
||||
$this->assertEqual($view->style_plugin->getField($index, 'message'), new FormattableMarkup($entry['message'], $message_vars));
|
||||
$link_field = $view->style_plugin->getField($index, 'link');
|
||||
// The 3rd entry contains some unsafe markup that needs to get filtered.
|
||||
if ($index == 2) {
|
||||
|
|
|
@ -91,12 +91,22 @@ class FormattableMarkupTest extends TestCase {
|
|||
*/
|
||||
public function providerTestUnexpectedPlaceholder() {
|
||||
return [
|
||||
['Non alpha starting character: ~placeholder', ['~placeholder' => 'replaced'], E_USER_ERROR, 'Invalid placeholder (~placeholder) in string: Non alpha starting character: ~placeholder'],
|
||||
['Alpha starting character: placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: Alpha starting character: placeholder'],
|
||||
// Ensure that where the placeholder is located in the string is
|
||||
['Non alpha starting character: ~placeholder', ['~placeholder' => 'replaced'], E_USER_WARNING, 'Invalid placeholder (~placeholder) with string: "Non alpha starting character: ~placeholder"'],
|
||||
['Alpha starting character: placeholder', ['placeholder' => 'replaced'], E_USER_WARNING, 'Invalid placeholder (placeholder) with string: "Alpha starting character: placeholder"'],
|
||||
// Ensure that where the placeholder is located in the the string is
|
||||
// irrelevant.
|
||||
['placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: placeholder'],
|
||||
['placeholder', ['placeholder' => 'replaced'], E_USER_WARNING, 'Invalid placeholder (placeholder) with string: "placeholder"'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedDeprecation Support for keys without a placeholder prefix is deprecated in Drupal 9.1.0 and will be removed in Drupal 10.0.0. Invalid placeholder (foo) with string: "No replacements"
|
||||
* @group legacy
|
||||
*/
|
||||
public function testNoReplacementUnsupportedVariable() {
|
||||
$markup = new FormattableMarkup('No replacements', ['foo' => 'bar']);
|
||||
// Cast it to a string which will generate the deprecation notice.
|
||||
$output = (string) $markup;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue