Skip to content

[5.x] Feature/add user profile form tabs sections #11836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/Auth/UserTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ public function profileForm()

$data = $this->getFormSession('user.profile');

$data['fields'] = $this->getProfileFields();
$data['tabs'] = $this->getProfileTabs();
$data['sections'] = collect($data['tabs'])->flatMap->sections->all();
$data['fields'] = collect($data['sections'])->flatMap->fields->all();

$knownParams = ['redirect', 'error_redirect', 'allow_request_redirect'];

Expand Down Expand Up @@ -708,6 +710,41 @@ protected function getAdditionalRegistrationFields()
->all();
}

/**
* Get tabs with fields filled with extra data for looping over and rendering, using tabs defined in blueprint.
* The result is unpacked into a sections array and a fields array for choosing what to loop over and render.
*
* @return array
*/
protected function getProfileTabs()
{
$user = User::current();

$values = $user
? $user->data()->merge(['email' => $user->email()])->all()
: [];

return User::blueprint()->tabs()
->map(fn ($tab) => [
'display' => $tab->display(),
'sections' => $tab->sections()
->map(fn ($section) => [
'display' => $section->display(),
'instructions' => $section->instructions(),
'fields' => $section->fields()->addValues($values)->preProcess()->all()
->reject(fn ($field) => in_array($field->handle(), ['password', 'password_confirmation', 'roles', 'groups'])
|| $field->fieldtype()->handle() === 'assets'
)
->map(fn ($field) => $this->getRenderableField($field, 'user.profile'))
->values()
->all(),
])
->all(),
])
->values()
->all();
}

/**
* Get fields with extra data for looping over and rendering.
*
Expand Down
180 changes: 149 additions & 31 deletions tests/Tags/User/ProfileFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,85 @@ public function it_renders_form_with_fields_array()
$this->assertEquals($expected, $actual[0]);
}

#[Test]
public function it_renders_form_with_tabs_array_and_custom_blueprint()
{
$this->useCustomBlueprint();

$this->actingAs(User::make()
->email('test@example.com')
->data(['name' => 'Test User', 'phone' => '12345'])
->save());

$output = $this->normalizeHtml($this->tag(<<<'EOT'
{{ user:profile_form }}
{{ tabs }}
<h2 class="tab">{{ display }}</h2>
{{ sections }}
<h3 class="section">{{ display }}</h3>
{{ fields }}
<label>{{ display }}</label>{{ field }}
{{ /fields }}
{{ /sections }}
{{ /tabs }}
{{ /user:profile_form }}
EOT
));

preg_match_all('/(?:<h[23].+>.+<\/h[23]>|<label>.+<\/label><input.+>|<label>.+<\/label><label><input.+><input.+><\/label>)+/U', $output, $actual);

$expected = [
'<h2 class="tab">Main</h2>',
'<h3 class="section">Account</h3>',
'<label>Full Name</label><input id="userprofile-form-name-field" type="text" name="name" value="Test User">',
'<label>Email Address</label><input id="userprofile-form-email-field" type="email" name="email" value="test@example.com">',
'<h3 class="section">About you</h3>',
'<label>Phone Number</label><input id="userprofile-form-phone-field" type="text" name="phone" value="12345">',
'<label>Over 18 years of age?</label><input id="userprofile-form-age-field" type="text" name="age" value="" required>',
'<h2 class="tab">Options</h2>',
'<h3 class="section">Communication</h3>',
'<label>Newsletter</label><label><input type="hidden" name="newsletter" value="0"><input id="userprofile-form-newsletter-field" type="checkbox" name="newsletter" value="1" checked></label>',
];
$this->assertEquals($expected, $actual[0]);
}

#[Test]
public function it_renders_form_with_sections_array_and_custom_blueprint()
{
$this->useCustomBlueprint();

$this->actingAs(User::make()
->email('test@example.com')
->data(['name' => 'Test User', 'phone' => '12345'])
->save());

$output = $this->normalizeHtml($this->tag(<<<'EOT'
{{ user:profile_form }}
{{ sections }}
<h3 class="section">{{ display }}</h3>
{{ fields }}
<label>{{ display }}</label>{{ field }}
{{ /fields }}
{{ /sections }}
{{ /user:profile_form }}
EOT
));

preg_match_all('/(?:<h[23].+>.+<\/h[23]>|<label>.+<\/label><input.+>|<label>.+<\/label><label><input.+><input.+><\/label>)+/U', $output, $actual);

$expected = [
'<h3 class="section">Account</h3>',
'<label>Full Name</label><input id="userprofile-form-name-field" type="text" name="name" value="Test User">',
'<label>Email Address</label><input id="userprofile-form-email-field" type="email" name="email" value="test@example.com">',
'<h3 class="section">About you</h3>',
'<label>Phone Number</label><input id="userprofile-form-phone-field" type="text" name="phone" value="12345">',
'<label>Over 18 years of age?</label><input id="userprofile-form-age-field" type="text" name="age" value="" required>',
'<h3 class="section">Communication</h3>',
'<label>Newsletter</label><label><input type="hidden" name="newsletter" value="0"><input id="userprofile-form-newsletter-field" type="checkbox" name="newsletter" value="1" checked></label>',
];
$this->assertEquals($expected, $actual[0]);
}

#[Test]
public function it_renders_form_with_fields_array_and_custom_blueprint()
{
Expand All @@ -100,13 +179,14 @@ public function it_renders_form_with_fields_array_and_custom_blueprint()
EOT
));

preg_match_all('/<label>.+<\/label><input.+>/U', $output, $actual);
preg_match_all('/(?:<h[23].+>.+<\/h[23]>|<label>.+<\/label><input.+>|<label>.+<\/label><label><input.+><input.+><\/label>)+/U', $output, $actual);

$expected = [
'<label>Full Name</label><input id="userprofile-form-name-field" type="text" name="name" value="Test User">',
'<label>Email Address</label><input id="userprofile-form-email-field" type="email" name="email" value="test@example.com">',
'<label>Phone Number</label><input id="userprofile-form-phone-field" type="text" name="phone" value="12345">',
'<label>Over 18 years of age?</label><input id="userprofile-form-age-field" type="text" name="age" value="" required>',
'<label>Newsletter</label><label><input type="hidden" name="newsletter" value="0"><input id="userprofile-form-newsletter-field" type="checkbox" name="newsletter" value="1" checked></label>',
];

$this->assertEquals($expected, $actual[0]);
Expand Down Expand Up @@ -272,40 +352,78 @@ public function it_will_use_redirect_query_param_off_url()
private function useCustomBlueprint()
{
$blueprint = Blueprint::make()->setContents([
'fields' => [
[
'handle' => 'name', // Field already exists, but we're defining custom display string.
'field' => [
'type' => 'text',
'display' => 'Full Name',
],
],
[
'handle' => 'email', // Field is included by default, but we're just implying field order here.
'field' => [],
],
[
'handle' => 'password', // Field already exists, but we're defining custom validation rules.
'field' => [
'type' => 'text',
'input_type' => 'password',
'display' => 'Password',
'validate' => 'min:8',
],
],
'tabs' => [
[
'handle' => 'phone', // Adding custom phone field.
'field' => [
'type' => 'text',
'display' => 'Phone Number',
'handle' => 'main', // Default tab
'display' => 'Main',
'sections' => [
[
'handle' => 'account', // Basic information is grouped in a section
'display' => 'Account',
'fields' => [
[
'handle' => 'name', // Field already exists, but we're defining custom display string.
'field' => [
'type' => 'text',
'display' => 'Full Name',
],
],
[
'handle' => 'email', // Field is included by default, but we're just implying field order here.
'field' => [],
],
[
'handle' => 'password', // Field already exists, but we're defining custom validation rules.
'field' => [
'type' => 'text',
'input_type' => 'password',
'display' => 'Password',
'validate' => 'min:8',
],
],
],
],
[
'handle' => 'about-you', // Extra fields are grouped in another section
'display' => 'About you',
'fields' => [
[
'handle' => 'phone', // Adding custom phone field.
'field' => [
'type' => 'text',
'display' => 'Phone Number',
],
],
[
'handle' => 'age', // Adding custom age field.
'field' => [
'type' => 'text',
'display' => 'Over 18 years of age?',
'validate' => 'required',
],
],
],
],
],
],
[
'handle' => 'age', // Adding custom age field.
'field' => [
'type' => 'text',
'display' => 'Over 18 years of age?',
'validate' => 'required',
'handle' => 'options', // Adding an extra tab
'display' => 'Options',
'sections' => [
[
'handle' => 'communication', // Last test section in the blueprint
'display' => 'Communication',
'fields' => [
[
'handle' => 'newsletter', // Adding custom newsletter field.
'field' => [
'type' => 'toggle',
'display' => 'Newsletter',
'default' => 1,
],
],
],
],
],
],
],
Expand Down