From 491329044b38314f217e750d810ec1237451c660 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Fri, 18 May 2018 13:41:02 +1200 Subject: [PATCH 1/2] FIX Add extension to remap polymorphic relationship classes for Parent and Form fields --- _config/extensions.yml | 6 ++ .../Extension/UpgradePolymorphicExtension.php | 96 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 _config/extensions.yml create mode 100644 code/Extension/UpgradePolymorphicExtension.php diff --git a/_config/extensions.yml b/_config/extensions.yml new file mode 100644 index 0000000..3e243dc --- /dev/null +++ b/_config/extensions.yml @@ -0,0 +1,6 @@ +--- +Name: userformsextensions +--- +SilverStripe\UserForms\Model\UserDefinedForm: + extensions: + UpgradePolymorphicExtension: SilverStripe\UserForms\Extension\UpgradePolymorphicExtension diff --git a/code/Extension/UpgradePolymorphicExtension.php b/code/Extension/UpgradePolymorphicExtension.php new file mode 100644 index 0000000..6bf79d4 --- /dev/null +++ b/code/Extension/UpgradePolymorphicExtension.php @@ -0,0 +1,96 @@ + ['ParentClass'], + EmailRecipient::class => ['FormClass'], + SubmittedForm::class => ['ParentClass'], + ]; + + /** + * The default class name that will be used to replace values with + * + * @var string + */ + protected $defaultReplacement = UserDefinedForm::class; + + public function requireDefaultRecords() + { + if (!UserDefinedForm::config()->get('upgrade_on_build')) { + return; + } + + $updated = 0; + foreach ($this->targets as $className => $fieldNames) { + foreach ($fieldNames as $fieldName) { + /** @var DataList $list */ + $list = $className::get(); + + foreach ($list as $entry) { + /** @var DataObject $relationshipObject */ + $relationshipObject = Injector::inst()->get($entry->$fieldName); + if (!$relationshipObject) { + continue; + } + + // If the defined data class doesn't have the UserForm trait applied, it's probably wrong. Re-map + // it to a default value that does + $classTraits = class_uses($relationshipObject); + if (in_array(UserForm::class, $classTraits)) { + continue; + } + + $entry->$fieldName = $this->defaultReplacement; + try { + $entry->write(); + $updated++; + } catch (ValidationException $ex) { + // no-op, allow the rest of dev/build to continue. There may be an error indicating that the + // object's class doesn't exist, which can be fixed by {@link DatabaseAdmin::doBuild} and this + // logic will work the next time dev/build is run. + } + } + } + } + + if ($updated) { + $message = "Corrected {$updated} default polymorphic class names to {$this->defaultReplacement}"; + if (Director::is_cli()) { + echo sprintf(" * %s\n", $message); + } else { + echo sprintf("
  • %s
  • \n", $message); + } + } + } +} From 8454a3ef06ded5f4c5e9707003f325e888727afd Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Wed, 30 May 2018 10:24:29 +1200 Subject: [PATCH 2/2] FIX Remove invalid duplicate of core translation --- lang/en.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/lang/en.yml b/lang/en.yml index a91fbad..c83a77d 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -21,8 +21,6 @@ en: SilverStripe\CMS\Model\SiteTree: PLURALNAME: Pages SINGULARNAME: Page - SilverStripe\Forms\Form: - FIELDISREQUIRED: '{field} is required' SilverStripe\Forms\GridField\GridField: Filter: Filter ResetFilter: Reset