3

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?

1 Answer 1

3
+50

You have to add the form mode in the handlers section of the user entity:

/**
 * Implements hook_entity_type_build().
 */
function mymodule_entity_type_build(array &$entity_types) {
  $entity_types['user']->setFormClass('my_profile', 'Drupal\user\ProfileForm');
}

Use the standard class ProfileForm as you only want a set new form mode, not a new form class.

Then you can use this new handler in place of user.default in the routing file:

'_entity_form' => 'user.my_profile',
9
  • I've been trying this and it hasn't worked yet for me. How to I edit the routing file? In my sc.routing.yml file as listed above, I have the setting _entity_form: 'user.my_profile', in the defaults section. When I navigate to /settings/1/my-profile, it still shows "Page not found".
    – mlanning
    Commented Sep 3, 2017 at 18:33
  • ... and when I navigate to /user/1/edit, it is still showing the old original default form mode.
    – mlanning
    Commented Sep 3, 2017 at 18:39
  • Is user.my_profile really the complete machine name of the form mode, then you get user twice in the route definition, which is not pretty, so I would name the form mode my_profile like in my answer. To debug this you can use drupal console to check the route table.
    – 4uk4
    Commented Sep 3, 2017 at 21:48
  • ... you didn't change the core route or did you?
    – 4uk4
    Commented Sep 3, 2017 at 21:49
  • No. I'm trying to create two new routes. 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. I want each route to use a different form mode.
    – mlanning
    Commented Sep 3, 2017 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.