tractorcow-farm / silverstripe-fluent

Multi-language translate module for Silverstripe, without having to manage separate site trees.
BSD 3-Clause "New" or "Revised" License
93 stars 111 forks source link

Double calls of updateFluentActions lead to RuntimeException #888

Open moritz-sauer-13 opened 2 months ago

moritz-sauer-13 commented 2 months ago

Module version(s) affected

^7

Description

Description: When editing a page in a grid field, the problem occurs that the updateFluentActions() method is called twice. This happens both from the FluentSiteTreeExtension and from the FluentGridFieldExtension.

This double call results in the clearFluent field and other fields from the corresponding trait being present more than once. This causes a RuntimeException to occur when saving.

Steps for reproduction:

  1. Edit a page in a grid field in a Model Admin
  2. Saving/Publishing the page.

Expected behavior: updateFluentActions() should only be called once to avoid duplicate field definitions and the resulting exception.

Actual behavior: updateFluentActions() is called twice, resulting in duplicate definition of fields and a RuntimeException.

Error Message:

[Emergency] Uncaught RuntimeException: SilverStripe\Forms\{closure}() I noticed that a field called 'action_clearFluent' appears twice in your 'SilverStripe\Forms\Form' form called 'ItemEditForm'
POST /admin/offers/OfferPage/EditForm/field/OfferPage/item/55/ItemEditForm

Line 165 in /var/www/ss_project_web/vendor/silverstripe/framework/src/Forms/FieldList.php

How to reproduce

Model Admin:

private static $managed_models = [
      OfferPage::class,
];

FluentAdminTrait - updateFluentActions():

$moreOptions->push(
    FormAction::create(
        'clearFluent',
        _t(
            __TRAIT__ . '.Label_clearFluent',
            "Clear from all except '{title}'",
            [
                'title' => $locale->getTitle()
            ]
        )
    )->addExtraClass('btn-secondary')
);

Possible Solution

No response

Additional Context

No response

Validations

tractorcow commented 2 months ago

When editing a page in a grid field, the problem occurs that the updateFluentActions() method is called twice. This happens both from the FluentSiteTreeExtension and from the FluentGridFieldExtension.

I think I've had this issue before. I ended up solving it by just using a basic model, not a sitetree, which isn't really a reasonable workaround.

Suggested fix is to make the updateFluentAction() idempotent so that it safely can be called multiple times on the same action set.

moritz-sauer-13 commented 2 months ago

I have now temporarily written a patch to call the function in the gridfield extension only if the form action is not one of the corresponding model admin. Not a nice solution either, but it works for now.