0

I am working on a custom content entity that I want to 'link' to a user by adding it as a tab to the user profile. Each user can have one such entity.

My route is defined as follows:

entity.my_entity.edit_form:
  path: '/user/{user}/my_entity'
  defaults:
    _entity_form: my_entity.edit
    _title: 'Edit entity'
  requirements:
    _entity_access: my_entity.update
  options:
    user: \d+

And the tab (module.links.task.yml):

entity.my_entity.edit_form:
  route_name: entity.my_entity.edit_form
  base_route: entity.user.canonical
  title: Entity

Though this works and the tabs show up, they result in a fatal error (cannot return getValue() on null from the getters) presumably because the form does not have an assigned entity.

If I change the route path to /user/{user}/my_entity/{my_entity}, the form works, but I cannot create a tab as the last value is unknown to the routing system.

Can I use the provided user entity instead from the route instead and during the building of the entity form retrieve (and set) the entity I need?

0

You can use a controller with the user as only route parameter. There you can put the code to retrieve the entity that matches the user and build the entity form:

public function myEntityForm(UserInterface $user) {

  // get $my_entity that matches $user

  $form = $this->entityFormBuilder()->getForm($my_entity);

  return $form;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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