After some modifications in my block settings and my custom menu which are all migrated from a drupal 6 site i got the following error on blocks layout page:

InvalidArgumentException: $string ("Array") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 140 of core\lib\Drupal\Core\StringTranslation\TranslatableMarkup.php).

Perhaps the issue is not related to the block modification but after uninstall blocks module the error disappeared.

This is a translated one lingual Hungarian site running on Drupal 8.1.0.

Please help! I am really stuck :(

CommentFileSizeAuthor
#66 2719553-66.patch8.51 KBLendude
#66 interdiff-2719553-64-66.txt1.01 KBLendude
#64 2719553-64.patch7.73 KBLendude
#64 interdiff-2719553-61-64.txt5.51 KBLendude
#61 2719553-61.patch3.6 KBLendude
#61 interdiff-2719553-57-61.txt435 bytesLendude
#57 2719553-57.patch3.01 KBLendude
#56 2719553-56-TEST_ONLY.patch2.25 KBLendude
#55 2719553-55.patch1.64 KBlolmanKD
#51 2719553-50.patch1.64 KBpmkanse
#42 interdiff_35-42.txt1.48 KBrahulkhandelwal1990
#42 2719553-42.patch1.64 KBrahulkhandelwal1990
#35 2719553-35.patch879 bytesccarrascal
#30 String-Translation.patch1.41 KBmanoj124
#26 2719553-26-null_for_string.patch887 bytesemclaughlin
#13 2719553-11.patch918 bytesRajab Natshah
#9 drupal_core_translatablemarkup-2719553-9.patch873 bytesbarbun
#8 drupal_core_translatablemarkup-2719553-8.patch873 bytesbarbun
#7 drupal_core_translatablemarkup-2719553-7.patch900 bytesbarbun
#6 drupal_core_translatablemarkup-2719553-6.patch882 bytesthekevinday
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

juhaszg created an issue. See original summary.

juhaszg’s picture

If you need more information about the issue don't hesitate to ask or give a feedback. Workarounds also welcome! :)

juhaszg’s picture

Also if i try to run database update the previously mentioned error occured :(

juhaszg’s picture

Component: block.module » language system
jatinkumar1989’s picture

I am also getting the same error

any help, please let me know

@juhaszg, have you able to resolve this issue ?

Many Thanks
-Jatin

thekevinday’s picture

The problem seems to be that the __construct() function call in the TranslatableMarkup.php class, called TranslatableMarkup, is not properly handling a case where translation is disabled and NULL is (automatically?) passed to the translate function.

It might be a good idea to handle the case of NULL as a string.

I suggest adding an if is_null() and manually convert NULL to an empty string.
There should be no reason to fail on NULL, in my opinion.

I have provided a patch as a work around that converts NULL to a string.
The real solution should be to determine why NULL is making to this __construct() and then determine if that should or should not be allowed.

FYI: I came across this on drupal 8.3.7.
I have not (yet) checked to see if this is resolved in later versions.

barbun’s picture

Also got this error after upgrading from Lightning 2.1.7 to 2.1.8.

Thanks for the patch @thekevinday! Agreed that the patch is probably fixing the consequence of a problem rather than its root.
Meanwhile, I had to adjust the patch #6 because I was getting:

InvalidArgumentException: $string ("0") must be a string.

I will try and do some research as to why this is happening in the first place.

barbun’s picture

Just realised that empty() function would catch NULL as well. Updated patch attached.

barbun’s picture

Ugh, I probably should test my patches before submitting. Apologies.

mayurjadhav’s picture

Issue tags: +DrupalMumbaiCodeSprint
blakemorgan’s picture

RTBC

Rajab Natshah’s picture

Version: 8.1.0 » 8.4.4
Status: Active » Needs work
Rajab Natshah’s picture

Rajab Natshah’s picture

Status: Needs work » Needs review
Rajab Natshah’s picture

Still testing ... Seems that the fix is not working.
not sure if the patch will fix the issue in most cases.

Rajab Natshah’s picture

Status: Needs review » Needs work
Rajab Natshah’s picture

Title: TranslatableMarkup - $string ("Array") must be a string. » When we have plugin_id: text_custom Then we will have this notice in TranslatableMarkup for Invalid Argument Exception: $string (Array) must be a string.
Rajab Natshah’s picture

Title: When we have plugin_id: text_custom Then we will have this notice in TranslatableMarkup for Invalid Argument Exception: $string (Array) must be a string. » When we have plugin_id: text_custom in config Then we will have this notice in TranslatableMarkup for Invalid Argument Exception: $string (Array) must be a string.
Rajab Natshah’s picture

After some troubleshooting:
Seems the issue with plugin_id: text_custom in config
if plugin_id: text we will not have this issue.

Rajab Natshah’s picture

Title: When we have plugin_id: text_custom in config Then we will have this notice in TranslatableMarkup for Invalid Argument Exception: $string (Array) must be a string. » TranslatableMarkup for Invalid Argument Exception: $string (Array) must be a string.
afoster’s picture

In case anyone else end up on this ticket when they visit the "admin > Appearance" from your own custom theme.

This issue I had which caused this bug is leaving a blank description in the custom-theme.info.yml file. Adding in a description resolved the issue.

Rajab Natshah’s picture

Version: 8.4.4 » 4.6.x-dev
Status: Needs work » Needs review
SchnWalter’s picture

Version: 4.6.x-dev » 8.4.x-dev

Reverting the previous issue version change.

emclaughlin’s picture

The issue I'm running into is that NULL is being passed to the function as the string. Not sure how to fix it, or where the NULL is coming from, but hopefully this helps someone.

kmani’s picture

i am also getting something like same issue,

ResponseText: The website encountered an unexpected error. Please try again later.InvalidArgumentException: $string ("1") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 132 of core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php). Drupal\locale\LocaleConfigManager->getTranslatableData(Object) (Line: 167)

can you help anyone,

Thanks,
--Mani.

emclaughlin’s picture

Added a patch that worked for me, but not sure if it's the appropriate fix. From what I could tell, NULL was being passed in explicitly, so setting the default value of $string to '' wasn't working.

Kris77’s picture

Thank you so much @emclaughlin.

I explain my problem: I create a sub-theme, I install it and everything is ok.
But when I go to visit the site it returns the error as above.

I have apply patch in #26 and its ok now.

I use Drupal 8.7.2 and hope they can update the drupal core with this patch.

Mykola Dolynskyi’s picture

Hi, everyone
I also had same problem. I was building theme settings translation (seems like nobody done this since 8.0 release?)

socks.schema.yml

socks.settings:
  type: theme_settings
  label: 'Socks theme settings'
  mapping:
    socks_phones:
      type: text
      label: 'Phones'
      translatable: true

socks.settings.yml

socks_phones:
  value: "## phones"
  format: full_html
  langcode: en

socks.config_translation.yml

socks.settings:
  title: 'Socks theme settings'
  base_route_name: system.theme_settings
  names:
    - socks.settings

And when I hit "save" on admin/appearance/settings/translate/uk/edit - I get error like in subject

So what I had to do (and it worked fine, translation was saved and renders per each language) is

@core\lib\Drupal\Core\StringTranslation\TranslatableMarkup.php

  public function __construct($string, array $arguments = [], array $options = [], TranslationInterface $string_translation = NULL) {
    if(is_array($string)) {
      $options = $string;
      $string = $string['value'];
    }
....

Version: 8.4.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

manoj124’s picture

This is patch is used to check if it is a string or not and was working when I performed site install.

Status: Needs review » Needs work

The last submitted patch, 30: String-Translation.patch, failed testing. View results

mayurjadhav’s picture

Assigned: Unassigned » mayurjadhav
mayurjadhav’s picture

Assigned: mayurjadhav » Unassigned

Apologies members, due to some project commitments not able to work hence unassigned without any inputs.

pbhuktar7’s picture

I have drupal 8.9.2, getting error InvalidArgumentException: $string ("") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 132 of /var/www/html/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php).

ccarrascal’s picture

I am getting this error on Drupal 8.9.1 also:
InvalidArgumentException: $string ("0") must be a string.

I am uploading another patch, similar to #26, but using an empty() function instead of the is_null().

Anyway, I still don't know the root cause of the problem.

joseph.olstad’s picture

***EDIT***
Editing my own comment to avoid confusion, I am not using this patch so I will make no comment on it.
for my particular issue the string translation I was looking to fix did not need fixing so I didn't need a twig workaround , just had to use the configuration translation and dig down a bit to find and translate it.
***END EDIT***

pmate’s picture

Just my 2 cents here as I also stumbled into this.

The actual issue here is that Drupal\locale\LocaleConfigManager::getTranslatableData passes data directly to TranslatableMarkup constructor without checking what it is or converting it into the string.

In my case schema defined float type value and I was getting:

InvalidArgumentException: $string ("1") must be a string

I think none of the patches here solves the issue.

suldan’s picture

InvalidArgumentException: $string ("Array") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct()

when a views block was rendered. Caused by
{{ label|t }} in my block.html.twig

Solution: created block--views.html.twig and rendered the label without translation {{ label }}

t_stallmann’s picture

Also getting this error, on Drupal 9.1.4. Haven't tried all the patches, but #26 does not work for me.

reszli’s picture

I had a similar error:

InvalidArgumentException: $string ("100") must be a string

which I figured to be an issue with a config module's schema

it's in easy_breacrumbs schema:

    truncator_length:
      type: text
      label: 'Set the limit for truncation.'

vs in config:
truncator_length: 100

hope this helps someone else with a similar error

and BTW, I agree with @pmate that this should probably be fixed in Drupal\locale\LocaleConfigManager::getTranslatableData

vadim.jin’s picture

I have the same issue with NULL value in $string and the error text with one difference word "array" changed to nothing "".
TranslatableMarkup for Invalid Argument Exception: $string ("") must be a string.
And I don't imaging where from this NULL string are coming.

rahulkhandelwal1990’s picture

Status: Needs work » Needs review
FileSize
1.64 KB
1.48 KB

Added a check in Drupal\locale\LocaleConfigManager::getTranslatableData as suggested in #37 in this patch, please verify.

pbbhoge’s picture

Status: Needs review » Reviewed & tested by the community

Above patch works fine

joseph.olstad’s picture

Version: 8.9.x-dev » 9.3.x-dev
joseph.olstad’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Tests will make this issue easier to understand.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

charly71’s picture

The patch #42 doesn't work form me (Drupal 9.3.6). My error is:

Si è verificato un errore inatteso. Riprova più tardi, grazie.

InvalidArgumentException: $string ("Modulistica") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 133 of core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php).

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ericdsd’s picture

I experience the same issue on core 9.4.5 on both php 7.4 and 8.1, it occurs on drush locale:update

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pmkanse’s picture

josep_kiwop’s picture

We had the same problem but the pmkanse fix works in latest version: 9.5.7

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Rajab Natshah’s picture

Facing this issue in Drupal 10.1.0-beta1

lolmanKD’s picture

We are facing this issue on Drupal 9.2.20, I didn't saw the version support for it so created a new for it.

Lendude’s picture

Component: language system » locale.module
Status: Needs work » Needs review
Issue tags: -blocks layout, -menu, -language, -translation, -Needs tests +ddd2023
FileSize
2.25 KB

Came here from #2730807: WSOD on admin/modules if description is set but is NULL in module.info.yml, where this was raised as a possible fix, but this seems unrelated.

Here is a test for this.

This problem arises when a config item is marked as translatable or uses a translatable type such as 'label', but contains something other than a string. #28 is a good example of this where schema says its a sting and the config is an array.

  1. +++ b/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php
    @@ -127,6 +127,7 @@ class TranslatableMarkup extends FormattableMarkup {
       public function __construct($string, array $arguments = [], array $options = [], TranslationInterface $string_translation = NULL) {
    +    $string = !empty($string) ? $string : '';
    

    We should not be changing TranslatableMarkup

  2. +++ b/core/modules/locale/src/LocaleConfigManager.php
    @@ -175,7 +175,7 @@ protected function getTranslatableData(TypedDataInterface $element) {
    -      if (!empty($definition['translatable']) && $value !== '' && $value !== NULL) {
    +      if (!empty($definition['translatable']) && (($value !== '' && $value !== NULL) && is_string($value))) {
    

    Do we want to be fault tolerant here and just skip this? Or should we throw something to indicate something it trying to translate something untranslatable? The currently suggested fix would leave users without any indication that anything is wrong. But it doesn't necessarily feel like locales responsibility to inform about faulty config

Lendude’s picture

This implements the previous proposed fix of skipping the faulty config, but somewhat refactored.

The last submitted patch, 56: 2719553-56-TEST_ONLY.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 57: 2719553-57.patch, failed testing. View results

penyaskito’s picture

+++ b/core/modules/locale/src/LocaleConfigManager.php
@@ -175,7 +175,7 @@ protected function getTranslatableData(TypedDataInterface $element) {
+      if (!empty($definition['translatable']) && $value !== '' && is_string($value)) {

As there are some discussions (#2275865: Per language settings (vs translated settings) are not directly supported) around how to localize configuration (even when they are not strings) and this is subject to change, I would use !is_array instead of asserting for is_string().

Even if we never promised to support that you can translate non-labels, I'm pretty sure people is doing that in custom or even contrib modules. This could break their assumptions, which might be ok-ish but we should avoid if we can.

Lendude’s picture

Status: Needs work » Needs review
FileSize
435 bytes
3.6 KB

Fixing the failing test.

@penyaskito thanks for linking that, hadn't seen that issue yet. In this case though, the current way getTranslatableData works will always send the value to TranslatableMarkup and since that does this:

    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);
    }

anything that can't get passed is_string() will cause an exception. So I think keeping the check in getTranslatableData the same as in TranslatableMarkup would be the best way to prevent the exception. Should we change in the future what we allow to be translated, we probably need new logic here anyway.

But again, I'm not 100% convinced that preventing the exception is the right thing to do here. It's just that the current exception makes it very unclear what is going wrong. I'd be totally on board with catching the current exception and re-throwing it with a clearer message that a specified config setting can't be translated or something along those lines.

Lendude’s picture

@penyaskito So yeah, maybe we shouldn't use is_string, but catch the exception? That way the logic of what can be translated stays within TranslatableMarkup and any changes would work in getTranslatableData too....

Sill leaves the question about what we want to do if we catch that exception, silently move along or log something or rethrow it?

smustgrave’s picture

Status: Needs review » Needs work

@Lendude think something should be logged and move along.

Lot of my sites I routinely check the logs for issues and know that's when I would fix that.

Lendude’s picture

Status: Needs work » Needs review
FileSize
5.51 KB
7.73 KB

Ok lets log something!

This requires the logger factory to be added, so added a deprecation and change record for that.
Added test coverage for the error.

Not sure about the wording of the error, best I could come up with.

Status: Needs review » Needs work

The last submitted patch, 64: 2719553-64.patch, failed testing. View results

Lendude’s picture

Status: Needs work » Needs review
FileSize
1.01 KB
8.51 KB

Fix the last test

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Logged addition looks good.

penyaskito’s picture

Title: TranslatableMarkup for Invalid Argument Exception: $string (Array) must be a string. » Log error when a TranslatableMarkup is created with a non-string input

I think this is the best we can do for having _some_ error handling while still not breaking what people might be doing as in #60. Thanks!

Might be good having a summary update, or at least a title update. I tried to do that, but suggestions welcome.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 66: 2719553-66.patch, failed testing. View results