Twig condition always true on PHP7, but not anymore on PHP8

Created on 10 November 2022, about 2 years ago
Updated 26 June 2023, over 1 year ago

Problem/Motivation

Forms with States API started to behave strange after updating to PHP8.
I was able to track it back to the following code:

{%
  set classes = [
  (has_parent and ('form-actions' in attributes.class|keys or 'form-actions' in attributes.class)) ? 'js-form-wrapper',
  (has_parent and ('form-actions' in attributes.class|keys or 'form-actions' in attributes.class)) ? 'form-wrapper',
]
%}

On PHP7 the conditionals always evaluate to TRUE, even if there is no `form-actions` class or key, so the needed classes are added.
On PHP8 this is not the case anymore, so the classes are missing.
Thus, states API gets confused and instead of hiding the correct container, it was hiding the first parent element with `js-form-wrapper` class.

Proposed resolution

If/since this always evaluates to TRUE, and no issue was reported about it until now, it can be safely reverted to the way it is in core container.html.twig:

{%
  set classes = [
    has_parent ? 'js-form-wrapper',
    has_parent ? 'form-wrapper',
  ]
%}

Remaining tasks

Check if there is a reason for these conditionals and if so, implement it properly on PHP8. Otherwise revert to default.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Needs review

Version

3.7

Component

Code

Created by

🇷🇴Romania reszli Târgu Mureș

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • @trafo opened merge request.
  • 🇸🇰Slovakia trafo

    Expression 'form-actions' in attributes.class|keys is translate into this PHP code in_array('form-actions', [ 0 ], FALSE).

    In PHP 7.4 in_array would return TRUE since it uses loose comparison and 'form-actions' == 0 is TRUE.
    Since PHP 8.0.0 this is fixed as note in PHP doc states it: https://www.php.net/manual/en/function.in-array.php

    Proposed solution is to remove keys twig filter, since it doesn't make sense.

  • Status changed to Needs review over 1 year ago
Production build 0.71.5 2024