Skip to content

Commit

Permalink
Auto add fields from Content Type in method New
Browse files Browse the repository at this point in the history
$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.
  • Loading branch information
jurjenvn authored Apr 15, 2021
1 parent 85ea949 commit a5b7f70
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Controller/FrontendUsersProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a5b7f70

Please sign in to comment.