Needs work
Project:
Drupal core
Version:
main
Component:
language system
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
20 Sep 2015 at 18:05 UTC
Updated:
3 Mar 2025 at 01:12 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
mr.baileysInitial patch to get an idea of failures.
Comment #3
dawehnerIdeally we would put it into an eval string, just curious ...
Comment #4
dawehnerOn the other hand its basically for free here.
Comment #6
mr.baileysThis should fix a number of the failures.
Whether or not to use the eval string is being discussed in the parent issue #2570285: Make sure TranslatableMarkup accepts string values only, no agreement yet I think.
Comment #8
martin_qI am looking at this at DC Barcelona Sprint.
EDIT: ...and will continue to do so for a while, as I didn't get very far at the Sprint but I did understand what I need to do.
Comment #9
martin_qIssue renamed in view of the TranslatableString class having been renamed.
Building on what mr.baileys started, here's a patch which I think fixes all the places where empty strings may get fed to a TranslatableMarkup constructor, at least as far as the simpletests in core are concerned. Does there need to be a more thorough change to all the code and a test added whenever TranslatableMarkup objects are created, or was the aim just to make all the tests pass?
By the way, I don't know if this is the best way to fix the getTranslatableData method, but it seems to be a sound way to do it. That method seems to be designed to look for the valid translatable things and only translate them, so this change is just an extension of that behaviour.
Comment #10
dawehnerShould we define the property as '' instead?
Let's use !== so we don't cause a cast of objects to an empty string. You never know.
Comment #13
martin_qPatch rerolled to fix the fatal error caused by the use of the now obsolete TranslationWrapper instead of TranslatableMarkup, as well as to include @dawehner's second suggestion.
@dawehner
1. Sorry, I don't understand your suggestion. Instead of what? Do you mean there should be an
elsestatement?2. Sure. Somewhere else I saw that we are requiring strings (not just "no empty strings"), but I guess checking for that that is outside the scope of this issue.
Comment #14
martin_qComment #15
dawehnerYeah there could be an else to ensure we have the property set all the time. Not sure you how can actually end up in that situation but I think its not worth to cause the notice.
Comment #16
martin_q@dawehner Looking at the code again, there is already a check that
isset($definition[$property])so it would not make sense to have anelse {$definition[$property] = '';}statement connected to theifin the patch.Conceivably this
elsecould be added to theif (isset($definition[$property]))statement, but IMHO that is beyond the scope of this issue.Comment #19
plachThe assertion makes me think this is for 8.1.x now, but we could back-port a version without it to 8.0.x since then it would be a straight bug fix.
Also, this does not look major to me, feel free to restore the priority if I'm missing something.
Comment #20
catchHmm it might be easier to split the assertion into a new patch, then the bug fix can go into both versions at the same time without a backport.
Comment #21
dawehnerI'm a bit confused about this particular statement to be honest. The goal of that issue is to avoid putting empty strings into the TranslatableMarkup.
All the other fixes are just symptoms, the assertion is the actual bugfix.
Comment #22
xanoIf this condition is executed regardless of whether assertions are enabled, why don't we just throw an
\InvalidArgumentExceptioninstead?Comment #23
catch@dawehner
The assertion if committed by itself would break core, and potentially contrib/custom tests, we don't want to break people's tests in patch releases (unless we absolutely have to for some reason).
The other fixes are a pre-requisite for adding the assertion, but there's nothing about them which could break any other code, so they can go in a patch release easily.
I'm not sure what's confusing about that?
@Xano - if we throw an exception, a module that's not upgraded yet will throw exceptions on production, which will mean fatal errors for end users. We could do that in 9.x, not in a minor release. I'll admit I still have trouble between assertions and trigger_error() though particulary in this case where we're adding it after the fact - should we be using E_USER_DEPRECATED instead here?
Comment #24
xano@catch: Thanks for the clarification. Assertions are still new to me, but you helped me understand their use case here.
Comment #25
dawehnerI haven't been rational until jan 6, so yeah I agree, let's fix the issues in core.
Comment #41
smustgrave commentedCould the issue summary be updated for what's needed here? We looking to throw another exception if empty?
Comment #42
adaragao commentedI'm getting a InvalidArgumentException: $string ("") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 132 of /opt/drupal/web/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php).
And I was hoping to go around it with patch #13, but it doesn't work on Drupal 10.3.2.
Comment #43
dqd@smustgrave: Well,
Cannot access offsetcan be symptom-ish forempty string- however I wonder if this new issues on Drupal 11 are related:Cannot access offset of type Drupal\Core\StringTranslation\TranslatableMarkup in isset or empty in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 45 of core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.phpWhen users try to enter paths like
admin/structure/types/manage/article/fields/add-fieldComment #44
acbramley commentedRaised this in slack with @catch, posting response below:
Back to NW to get this into an MR.