Skip to content

fix: give collection item macro arguments explicit defaults - #244

Merged
daphneslootmans merged 1 commit into
masterfrom
428-prep-twig-4
Sep 7, 2026
Merged

fix: give collection item macro arguments explicit defaults#244
daphneslootmans merged 1 commit into
masterfrom
428-prep-twig-4

Conversation

@daphneslootmans

@daphneslootmans daphneslootmans commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Twig 4.0 makes a macro argument required unless it declares a default value. The renderCollectionItem macro is called from project form themes that override collection_widget or collection_rows, so a call passing fewer than three arguments would start raising "Value for argument allow_delete is required for macro renderCollectionItem".

Declaring = false for both booleans reproduces what the is defined guards in the macro body did, so rendered output is unchanged for every existing call. With the defaults in the signature those guards are dead code and are removed.

The guard in collection_widget is left alone: it reads a form theme variable, not a macro argument, and can genuinely be undefined.

Document the macro signature in docs/forms.md, including that the import belongs inside the block that uses it, since form theme blocks run in the context of the template being rendered.

Summary by Sourcery

Allow collection item rendering macros to omit optional delete and drag-and-drop controls while documenting their use in custom form themes.

Bug Fixes:

  • Make collection item macro boolean arguments optional to preserve compatibility with calls that omit them under Twig 4.0.

Enhancements:

  • Simplify collection item rendering conditions while preserving existing output behavior.

Documentation:

  • Document the collection item macro signature and correct import usage for custom form themes.

Twig 4.0 makes a macro argument required unless it declares a default
value. The renderCollectionItem macro is called from project form themes
that override collection_widget or collection_rows, so a call passing
fewer than three arguments would start raising "Value for argument
allow_delete is required for macro renderCollectionItem".

Declaring `= false` for both booleans reproduces what the `is defined`
guards in the macro body did, so rendered output is unchanged for every
existing call. With the defaults in the signature those guards are dead
code and are removed.

The guard in collection_widget is left alone: it reads a form theme
variable, not a macro argument, and can genuinely be undefined.

Document the macro signature in docs/forms.md, including that the import
belongs inside the block that uses it, since form theme blocks run in the
context of the template being rendered.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Updates renderCollectionItem to accept omitted boolean options under Twig 4.0 without changing existing output, and documents the signature and correct usage for custom form theme overrides.

Sequence diagram for collection item macro rendering with optional controls

sequenceDiagram
    participant Theme as Custom form theme
    participant Macro as renderCollectionItem
    participant Twig as Twig 4.0
    participant Output as Rendered row

    Theme->>Macro: renderCollectionItem(item, allow_delete, allow_drag_and_drop)
    Twig->>Macro: Apply false defaults for omitted booleans
    Macro->>Macro: form_widget(item)
    Macro->>Macro: form_errors(item)
    opt allow_drag_and_drop
        Macro->>Output: Render drag handle
    end
    opt allow_delete
        Macro->>Output: Render delete button
    end
    Macro-->>Theme: Render collection row
Loading

File-Level Changes

Change Details Files
Make collection-item macro booleans optional while preserving existing rendering behavior.
  • Add false defaults for deletion and drag-and-drop controls.
  • Remove redundant defined checks now that the parameters are always initialized.
templates/Form/fields.html.twig
Document how custom form themes can reuse the collection-item macro.
  • Add an override example for collection_rows using the macro.
  • Document the signature, defaults, and block-local import requirement.
docs/forms.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@daphneslootmans
daphneslootmans merged commit beeb821 into master Sep 7, 2026
12 of 14 checks passed
@daphneslootmans
daphneslootmans deleted the 428-prep-twig-4 branch September 7, 2026 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants