0

Assume we have two entity types with a 1:n relationship, e.g. an Event type that has a number of Participants that belong to that type. I have implemented this such that Participant has an entity_reference field that specifies the Event.

Now I would like to change the dependent (Participant) entity's add_form route, so that I can easily add Participants to an Event:

@ContentEntityType(
    id = "participant",
// ...
  links = {
    "add-form" = "/admin/structure/event/{event}/participants/add",
// ...
  },

I've managed to make this work on the AddForm. But now I get lots of routing errors like this in other places (such as when deleting a participant entity):

Symfony\Component\Routing\Exception\MissingMandatoryParametersException: Some mandatory parameters are missing ("event") to generate a URL for route "entity.participant.add_form".

The add_form route comes from DefaultHtmlRouteProvider. I've tried overriding it with a static route that includes a default value:

entity.participant.add_form:
  path: '/admin/structure/event/{event}/participant/add'
  defaults:
    _title: 'Add participant'
    _entity_form: participant.add
    event: NULL

but this doesn't work – page not found.

What would be the best way to supply a default value, or is there a better way to handle this kind of entity relationship?

2
  • There can not be a default value, you have an argument in the route which means you need to provide a value for it. You have a reference somewhere to that route that doesn't provide that value, could be a local task for example. You can't have a local task with a dynamic value
    – Berdir
    Apr 2, 2018 at 18:49
  • I've removed all local tasks, and all static routes to add_form. My guess is that the conflicting route comes from DefaultHtmlRouteProvider. I'm not sure how to get rid of it – or how to provide a default value as in drupal.org/docs/8/api/routing-system/using-parameters-in-routes under Optional Parameters
    – Hendrik
    Apr 4, 2018 at 10:24

1 Answer 1

0

A default value does not work in this case, as - strangely - this will override the context provided event id. Some default entity methods, such as delete, seem to require the route URLs and therefore fail when the parameter is not given.

The add_form route, which is automatically created by DefaultHtmlRouteProvider from the @ContentEntityType annotation, causes problems here. The easiest solution is probably to remove this route by deleting the line

"add-form" = "/admin/structure/event/{event}/participants/add"

from @ContentEntityType (under "links") – do not remove the add form in section "form" – and providing the route in modulename.routing.yml:

entity.participant.add_form:
  path: '/admin/structure/event/{event}/participant/add'
  defaults:
    _entity_form: participant.add
    entity_type_id: 'participant'
    _admin_route: TRUE
  requirements:
    _entity_create_access: 'participant'

Your Answer

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

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