From a5b7f70a2c1d9228f18efe47f602550775d96f7e Mon Sep 17 00:00:00 2001 From: Jurjen van Noord Date: Thu, 15 Apr 2021 22:42:13 +0200 Subject: [PATCH] Auto add fields from Content Type in method New $content->setFieldValue('dob', '')seemed out of place in method New. Instead I added code to loop through the fields from the given Content Type and have these fields added automatically. --- src/Controller/FrontendUsersProfileController.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Controller/FrontendUsersProfileController.php b/src/Controller/FrontendUsersProfileController.php index 8b89ff3..ab367a6 100644 --- a/src/Controller/FrontendUsersProfileController.php +++ b/src/Controller/FrontendUsersProfileController.php @@ -171,12 +171,17 @@ private function new(ContentType $contentType): Content $content->setContentType($contentTypeName); $content->setFieldValue('displayName', $user->getDisplayName()); // Hidden field for record title $content->setFieldValue('username', $user->getUsername()); // Hidden field with copy of username + $content->setFieldValue('slug', $user->getUsername()); // Make slugs unique to users // Initialise ALL extra fields as defined in the contenttype with empty strings. // This ensures they are displayed on the /profile/edit route without backend intervention - $content->setFieldValue('dob', ''); + foreach($contentType->get('fields') as $name => $field){ - $content->setFieldValue('slug', $user->getUsername()); // Make slugs unique to users + if(!in_array($name, ['displayName','username','slug'])) { + $content->setFieldValue($name, ''); + } + } + $this->contentFillListener->fillContent($content); // Persist in DB