Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Exports of flexible templates throw PHP 8 error when System language is not English. #5853

Closed
djzwerg opened this issue Nov 3, 2022 · 22 comments · Fixed by backdrop/backdrop#4251

Comments

@djzwerg
Copy link

djzwerg commented Nov 3, 2022

Description of the bug

When I'm going to export my layout the following error occurs:
natsort(): Argument #1 ($array) must be of type array, null given

Steps to reproduce:

  1. Enable the language and Locale modules, add a language
  2. Translate the string "Flexible layout templates" on /admin/config/regional/translate/translate
  3. enable error display
  4. create own flexible template on /admin/structure/layouts/settings/flexible-template/add
  5. make sure you switch to the other language path
  6. try to export, following the dropbutton menu link on /admin/structure/layouts/settings

Backdrop error logs:

TypeError: natsort(): Argument #1 ($array) must be of type array, null given in natsort() (line 313 of /mypath/core/modules/config/config.admin.inc).

Warning: Undefined array key "Flexible layout templates" in config_export_single_form() (line 312 of /mypath/core/modules/config/config.admin.inc).

Edit:

Warning: Trying to access array offset on value of type null in field_config_label_instance() (line 436 of /mypath/core/modules/field/field.module).
(3 times)

This one belongs to the comment module. It has been gone when the module is enabled.

(previously posted on https://github.com/backdrop-contrib/flexible_layout/issues/8)

Actual behavior

You'll get the error message natsort(): Argument #1 ($array) must be of type array, null given.

Expected behavior

The export would work.

Additional information

Backdrop core version: 1.23.0
Database version: MySQL 8.0.30
PHP version: 8.0.25

For now I think this is an issue with the PHP version. Using PHP 7.4 the export feature works.

@indigoxela
Copy link
Member

@djzwerg I can't reproduce the problem without the flexible_template module. But I also can't reproduce it with that module enabled.

I'm also not sure what that contrib module is for. Flexible layouts are in core - why a module?

I tried your steps with PHP 8.1, but see no warnings in dblog.

@djzwerg
Copy link
Author

djzwerg commented Nov 3, 2022

@indigoxela I can reproduce the problem without the contrib module, too:

  1. fresh Backdrop installation (without changes)
  2. set language to German
  3. add flexible template 'test'
  4. try to export the flexible layout

The error still occurs.

Additional information

Backdrop core version: 1.23.0
Database version: MySQL 8.0.30
PHP version: 8.0.25

@djzwerg
Copy link
Author

djzwerg commented Nov 3, 2022

As I see the issue won't be there if you use the standard English installation. But if you change the language to German it happens.

@djzwerg djzwerg changed the title Flexible template: PHP 8 issue on export Flexible template: PHP 8 issue on export (System language not English) Nov 3, 2022
@indigoxela
Copy link
Member

indigoxela commented Nov 3, 2022

I can reproduce the problem without the contrib module, too

Interesting, I still can't. Language and Locale module are enabled. Switched to German (/de), the export still works just fine and nothing shows up in dblog. So it doesn't seem to caused by the language.

It might have to do with your template... 🤔 Wait... do you have the comment module enabled or uninstalled? The third PHP warning is caused by that (see #5469).

Now for the two remaining:

Warning: Undefined array key "Flexible layout templates" in config_export_single_form()

This seems very odd. You DO have the "Flexible layout templates" option in your dropdown list, right? Otherwise you couldn't export it. The export per se works? Is some json displayed in the export area?

@djzwerg
Copy link
Author

djzwerg commented Nov 3, 2022

Within the fresh installation comment module is enabled. The former report based on a site with disabled comment module.

There is no json available:
grafik

Themes are Basis (frontend) and Seven (backend).

@djzwerg
Copy link
Author

djzwerg commented Nov 3, 2022

I can confirm that the third error log has been gone with comment module enabled.

@djzwerg
Copy link
Author

djzwerg commented Nov 3, 2022

Devel shows nothing.

@indigoxela
Copy link
Member

Did you already try a different path? Instead of starting on the layout overview, go to /admin/config/development/configuration/single/export

And check for the items in the dropdown list for a single export - is "Flexible layout templates" available there or not?

@indigoxela
Copy link
Member

Wait... a suspicion: I just overwrote the url and that way I can reproduce the problem. But only if I set the URL to something non-existent.

Can you post the exact URL, when getting the error?

@indigoxela
Copy link
Member

And to (eventually) confirm: go to /admin/config/regional/translate/translate and search for a translated string "Flexible layout templates". And if you delete that translation - does the export work again?

@djzwerg
Copy link
Author

djzwerg commented Nov 3, 2022

@indigoxela yes that solves that issue 🪄

@indigoxela
Copy link
Member

Many thanks for verifying!

So the bug here is, that the dropdown menu item does not consider that this is a translatable string and blindly applies it as url param and on the other side the form blindly relies on an existing option with that key. Oh dear! 🙄

@indigoxela
Copy link
Member

A PR is available for testing and review.

Additionally to translating the string "on both sides" it also tries to catch errors, if the url param is not found in the select list for whatever reasons. A value that comes in with $_GET is never fully reliable.

@argiepiano
Copy link

Since there was so much back and forth, could @djzwerg update the steps to reproduce this problem? That will help test.

@indigoxela
Copy link
Member

indigoxela commented Nov 3, 2022

@argiepiano I updated the steps, hopefully they're complete now to reproduce. Note that this only causes an error on php 7.4, but is fatal on php 8.+

@djzwerg
Copy link
Author

djzwerg commented Nov 3, 2022

@indigoxela I can reproduce the problem without the contrib module, too:

  1. fresh Backdrop installation (without changes)
  2. set language to German
  3. add flexible template 'test'
  4. try to export the flexible layout

The error still occurs.

Additional information

Backdrop core version: 1.23.0
Database version: MySQL 8.0.30
PHP version: 8.0.25

@argiepiano this is the way to reproduce. I don't know whether it makes a difference to install Backdrop with the German language pack. I can check the PR later.

@djzwerg
Copy link
Author

djzwerg commented Nov 3, 2022

@indigoxela the PR solves this issue. Thanks a lot!

@argiepiano
Copy link

argiepiano commented Nov 3, 2022

I've tried both sets of direction and am still unable to reproduce the problem. I'm working with Spanish, where the phrase "Flexible layout template" is not translated in the translation file. I can't translate it manually through the UI until Backdrop actually encounters that phrase in the UI, so the only way to do this is to create the flexible template in English first, go to the Export screen and then manually translate it to Spanish after the phrase appears in the translation form. Then visit the Spanish page to export the layout... no issues whatsoever.

I also tried manually adding the phrase to the .po Spanish file and reinstalling the site by selecting Spanish in the installation interface, then follow these directions. Still no joy.

I'll just let someone with better luck (and someone who reads German, where perhaps the problem exists) try testing this.

@indigoxela
Copy link
Member

indigoxela commented Nov 4, 2022

Then visit the Spanish page to export the layout

Maybe that's the culprit? You should try to export the template, not the layout, so starting from /es/admin/structure/layouts/settings and use the "export" link from the dropdown button of your template.

You should end up with a url like /es/admin/config/development/configuration/single/export?group=Flexible layout templates&name=layout.flexible.WHATEVER. Note the group= param.
And also: on PHP versions before 8 this won't be a fatal error, so make sure that the error display is on.
Alternative: override the group= GET param manually.

@argiepiano
Copy link

PHP versions before 8 this won't be a fatal error

Doh!!!! 🤦🏽 Stupid mistake - I was trying with 7.4. I'll try again later.

@argiepiano
Copy link

OK, tested in php 8. WFM!

@quicksketch
Copy link
Member

Thanks @indigoxela, @djzwerg, and @argiepiano! I merged backdrop/backdrop#4251 into 1.x and 1.23.x. This will be in 1.23.1.

@jenlampton jenlampton changed the title Flexible template: PHP 8 issue on export (System language not English) Fixed: Exports of flexible templates throw PHP 8 error when System language is not English. Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants