Skip to main content
Notice removed Draw attention by mlanning
Bounty Ended with 4uk4's answer chosen by mlanning
edited tags
Link
Elin Y.
  • 7.4k
  • 1
  • 41
  • 65
deleted 13 characters in body
Source Link
mlanning
  • 441
  • 3
  • 21

My project requires two different pages to edit a user. The first one being at /settings/{user}/my-profile, where the user updates their name, address, etc. The other page is at /settings/{user}/login-credentials, where the user updates their username, password, etc.

After navigating to /admin/structure/display-modes/form, I have created a custom form mode called "My Profile" which has the id user.my_profile, and activated it at the bottom of admin/config/people/accounts/form-display.

Now I need to create a page that utilizes this new custom form mode, and I'm a bit lost.

I looked into how the standard user edit route is created at /user/{user}/edit, and I found the code in the route provider at namespace Drupal\user\Entity;. This is what is in that method (other routes have been removed)...

public function getRoutes(EntityTypeInterface $entity_type) {
  $route_collection = new RouteCollection();
  
  $route = (new Route('/user/{user}/edit'))
    ->setDefaults([
      '_entity_form' => 'user.default',
      '_title_callback' => 'Drupal\user\Controller\UserController::userTitle',
    ])
    ->setOption('_admin_route', TRUE)
    ->setRequirement('user', '\d+')
    ->setRequirement('_entity_access', 'user.update');
  $route_collection->add('entity.user.edit_form', $route);

  return $route_collection;
}

Then I did some research on the routing system, and I've created a routing.yml file. The name of my module is sc, and the file is at modules/custom/sc/src/Routing/sc.routing.yml.

sc.routing.yml

sc.my_profile.edit:
  path: '/settings/{user}/my-profile'
  defaults:
    _entity_form: 'user.my_profile',
    _title_callback: 'Drupal\user\Controller\UserController::userTitle'
  requirements:
    user: '\d+'
    _entity_access: 'user.update'
    _permission: 'access content'

I'm logged in as user1, cleared the cache, and when I navigate to /settings/1/my-profile, I get "Page Not Found". Am I on the right track? How do I create these pages with different form modes?

My project requires two different pages to edit a user. The first one being at /settings/{user}/my-profile, where the user updates their name, address, etc. The other page is at /settings/{user}/login-credentials, where the user updates their username, password, etc.

After navigating to /admin/structure/display-modes/form, I have created a custom form mode called "My Profile" which has the id user.my_profile, and activated it at the bottom of admin/config/people/accounts/form-display.

Now I need to create a page that utilizes this new custom form mode, and I'm a bit lost.

I looked into how the standard user edit route is created at /user/{user}/edit, and I found the code in the route provider at namespace Drupal\user\Entity;. This is what is in that method (other routes have been removed)...

public function getRoutes(EntityTypeInterface $entity_type) {
  $route_collection = new RouteCollection();
  
  $route = (new Route('/user/{user}/edit'))
    ->setDefaults([
      '_entity_form' => 'user.default',
      '_title_callback' => 'Drupal\user\Controller\UserController::userTitle',
    ])
    ->setOption('_admin_route', TRUE)
    ->setRequirement('user', '\d+')
    ->setRequirement('_entity_access', 'user.update');
  $route_collection->add('entity.user.edit_form', $route);

  return $route_collection;
}

Then I did some research on the routing system, and I've created a routing.yml file. The name of my module is sc, and the file is at modules/custom/sc/src/Routing/sc.routing.yml.

sc.routing.yml

sc.my_profile.edit:
  path: '/settings/{user}/my-profile'
  defaults:
    _entity_form: 'user.my_profile',
    _title_callback: 'Drupal\user\Controller\UserController::userTitle'
  requirements:
    user: '\d+'
    _entity_access: 'user.update'
    _permission: 'access content'

I'm logged in as user1, cleared the cache, and when I navigate to /settings/1/my-profile, I get "Page Not Found". Am I on the right track? How do I create these pages with different form modes?

My project requires two different pages to edit a user. The first one being at /settings/{user}/my-profile, where the user updates their name, address, etc. The other page is at /settings/{user}/login-credentials, where the user updates their username, password, etc.

After navigating to /admin/structure/display-modes/form, I have created a custom form mode called "My Profile" which has the id user.my_profile, and activated it at the bottom of admin/config/people/accounts/form-display.

Now I need to create a page that utilizes this new custom form mode, and I'm a bit lost.

I looked into how the standard user edit route is created at /user/{user}/edit, and I found the code in the route provider at namespace Drupal\user\Entity;. This is what is in that method (other routes have been removed)...

public function getRoutes(EntityTypeInterface $entity_type) {
  $route_collection = new RouteCollection();
  
  $route = (new Route('/user/{user}/edit'))
    ->setDefaults([
      '_entity_form' => 'user.default',
      '_title_callback' => 'Drupal\user\Controller\UserController::userTitle',
    ])
    ->setOption('_admin_route', TRUE)
    ->setRequirement('user', '\d+')
    ->setRequirement('_entity_access', 'user.update');
  $route_collection->add('entity.user.edit_form', $route);

  return $route_collection;
}

Then I did some research on the routing system, and I've created a routing.yml file. The name of my module is sc, and the file is at modules/custom/sc/sc.routing.yml.

sc.routing.yml

sc.my_profile.edit:
  path: '/settings/{user}/my-profile'
  defaults:
    _entity_form: 'user.my_profile'
    _title_callback: 'Drupal\user\Controller\UserController::userTitle'
  requirements:
    user: '\d+'
    _entity_access: 'user.update'
    _permission: 'access content'

I'm logged in as user1, cleared the cache, and when I navigate to /settings/1/my-profile, I get "Page Not Found". Am I on the right track? How do I create these pages with different form modes?

Notice added Draw attention by mlanning
Bounty Started worth 50 reputation by mlanning
Source Link
mlanning
  • 441
  • 3
  • 21

How to utilize custom form modes to edit a user entity?

My project requires two different pages to edit a user. The first one being at /settings/{user}/my-profile, where the user updates their name, address, etc. The other page is at /settings/{user}/login-credentials, where the user updates their username, password, etc.

After navigating to /admin/structure/display-modes/form, I have created a custom form mode called "My Profile" which has the id user.my_profile, and activated it at the bottom of admin/config/people/accounts/form-display.

Now I need to create a page that utilizes this new custom form mode, and I'm a bit lost.

I looked into how the standard user edit route is created at /user/{user}/edit, and I found the code in the route provider at namespace Drupal\user\Entity;. This is what is in that method (other routes have been removed)...

public function getRoutes(EntityTypeInterface $entity_type) {
  $route_collection = new RouteCollection();
  
  $route = (new Route('/user/{user}/edit'))
    ->setDefaults([
      '_entity_form' => 'user.default',
      '_title_callback' => 'Drupal\user\Controller\UserController::userTitle',
    ])
    ->setOption('_admin_route', TRUE)
    ->setRequirement('user', '\d+')
    ->setRequirement('_entity_access', 'user.update');
  $route_collection->add('entity.user.edit_form', $route);

  return $route_collection;
}

Then I did some research on the routing system, and I've created a routing.yml file. The name of my module is sc, and the file is at modules/custom/sc/src/Routing/sc.routing.yml.

sc.routing.yml

sc.my_profile.edit:
  path: '/settings/{user}/my-profile'
  defaults:
    _entity_form: 'user.my_profile',
    _title_callback: 'Drupal\user\Controller\UserController::userTitle'
  requirements:
    user: '\d+'
    _entity_access: 'user.update'
    _permission: 'access content'

I'm logged in as user1, cleared the cache, and when I navigate to /settings/1/my-profile, I get "Page Not Found". Am I on the right track? How do I create these pages with different form modes?