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); + } + } + } +} diff --git a/lang/en.yml b/lang/en.yml index 1fc9461..e375dcf 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -3,8 +3,6 @@ en: SELECTUPLOADFOLDER: 'Select upload folder' SilverStripe\CMS\Model\SiteTree: TABMAIN: Main - SilverStripe\Forms\Form: - FIELDISREQUIRED: '{name} is required.' SilverStripe\UserForms: ADDEMAILRECIPIENT: 'Add Email Recipient' CLEARBUTTON: Clear