php warnings thrown by ClassyLayout when definition_default is NULL for default_section

Created on 3 April 2023, over 1 year ago
Updated 9 November 2023, about 1 year ago

Problem/Motivation

As a sitebuilder,
When I add a section,
A php warning is thrown

Dev Notes

This came to our attention when updating Drupal Test Traits to 2.x.
When updating to Drupal Test Traits 2.x, it was noticed that ClassyLayout is throwing php warnings when configuring a section in LayoutBuilder. The warning being thrown is below:

Warning: foreach() argument must be of type array|object, null given in Drupal\Core\Render\Element\Checkboxes::valueCallback() (line 113 of /data/web/core/lib/Drupal/Core/Render/Element/Checkboxes.php) #0 /data/web/core/includes/bootstrap.inc(347): _drupal_error_handler_real()

This is because line 69 of ClassyLayout.php assigns NULL to $definition_default if none is specified, but instead of NULL this should probably be empty or an empty array instead.

Steps to reproduce

  1. Enable the layout_section_classes module
  2. Create a page with layout builder.
  3. Add a section.
  4. Choose any layout for the section.
  5. Check the Drupal log.
  6. When the configuration form for the section appears, the warning is thrown.

Proposed resolution

  • The previously mentioned line of ClassyLayout.php should be changed so the #default_value is an empty value or empty array, instead of NULL.
  • A test to this effect should be added.
πŸ› Bug report
Status

Fixed

Version

1.3

Component

Code

Created by

πŸ‡¦πŸ‡ΊAustralia T.Barker

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.

  • πŸ‡¦πŸ‡ΊAustralia amjad1233 Brisbane

    @T.Barker the patch as per #2 seems to be missing tests.

  • πŸ‡¦πŸ‡ΊAustralia amjad1233 Brisbane
  • πŸ‡¦πŸ‡ΊAustralia T.Barker

    Uploading a new version of the patch which contains the tests, as per feedback from @amjad1233 .

  • Status changed to Needs review over 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Setting to needs review so the tests run

  • Status changed to Needs work over 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
    +++ b/src/ClassyLayout.php
    @@ -66,7 +66,7 @@ class ClassyLayout extends LayoutDefault implements PluginFormInterface {
    -      $definition_default = $class_definition['default'] ?? NULL;
    +      $definition_default = $class_definition['default'] ?? [];
    

    I think we need to $class_definition['multiple'] here, as an array is appropriate if that's TRUE, but NULL is appropriate if not.

    I also note you can probably put `default: {}` in your definition and it should resolve the error

  • πŸ‡¦πŸ‡ΊAustralia amjad1233 Brisbane

    Hi @larowlan,

    Thanks for your feedback.

    Would you mind elaborating?

    I think we need to $class_definition['multiple'] here, as an array is appropriate if that's TRUE, but NULL is appropriate if not.

    In regards to `default: {}` I thought it was mandatory but makes sense.

    Regards,
    Amjad

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    So the default value should be NULL if the class doesn't support multiple values.
    If it does, then an empty array makes sense.

    And in your definition of the class in the yaml, you can add `default: {}` and it should fix this error too (but I agree fixing it here is the best approach)

  • πŸ‡¦πŸ‡ΊAustralia amjad1233 Brisbane

    Ah, that makes sense. Cool no worries we will give it another look. Thanks for that.

    I agree too fixing seems to be a good approach since Test Traits 2 are bit more sensetive.

  • @tbarker opened merge request.
  • πŸ‡¦πŸ‡ΊAustralia T.Barker

    Thanks @larowlan. @amjad1233 hasn't been able to work on this with me but I've attached a new patch file with changes from your feedback, as I've interpreted it. Is this along the lines of what you were meaning? I'm sure there is probably a better way to express the logic but I'm still learning - sorry!

  • @tbarker opened merge request.
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
    +++ b/src/ClassyLayout.php
    @@ -66,7 +66,11 @@ class ClassyLayout extends LayoutDefault implements PluginFormInterface {
    +        $definition_default = NULL;
    

    We still need to honour the default in non multiple scenarios, so we basically need the original code in the else clause

  • πŸ‡¦πŸ‡ΊAustralia gordon Melbourne

    I found another issue and fixed out.

  • Status changed to Fixed about 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Thanks
    Tagging 8.x-1.4

  • Status changed to Active about 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia acbramley

    This has caused a regression/behaviour change where required classes now have an empty option and default to NULL instead of the first option in the list of classes.

    1.3:

    1.4:

  • Status changed to Needs work about 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Ah, we should be able to fix that and get a new release out

  • Status changed to Fixed about 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Added followup πŸ› Default value not set for required options Active

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024