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

Created on 10 November 2022, over 1 year ago
Updated 26 June 2023, 12 months 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 12 months ago
  • ๐Ÿ‡ธ๐Ÿ‡ฐSlovakia trafo
Production build 0.69.0 2024