2

I am building a custom component in Drupal 8 that has an entity with child entities i.e., TeamEntity has one or more MeetEntity objects.

When I navigate to the "Delete" link for a given MeetEntity, I get the following error:

The website encountered an unexpected error. Please try again later.

Symfony\Component\Routing\Exception\RouteNotFoundException: Route "entity.meet_entity.collection" does not exist. in Drupal\Core\Routing\RouteProvider->getRouteByName() (line 191 of core/lib/Drupal/Core/Routing/RouteProvider.php).
Drupal\Core\Routing\UrlGenerator->getRoute('entity.meet_entity.collection') (Line: 285)
Drupal\Core\Routing\UrlGenerator->generateFromRoute('entity.meet_entity.collection', Array, Array, 1) (Line: 110)
Drupal\Core\Render\MetadataBubblingUrlGenerator->generateFromRoute('entity.meet_entity.collection', Array, Array, 1) (Line: 754)
Drupal\Core\Url->toString(1) (Line: 161)
Drupal\Core\Utility\LinkGenerator->generate(Object, Object) (Line: 99)
Drupal\Core\Render\Element\Link::preRenderLink(Array)
call_user_func(Array, Array) (Line: 384)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 451)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 451)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 200)
Drupal\Core\Render\Renderer->render(Array) (Line: 471)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 43)
__TwigTemplate_4a2ed85f2bf4ceab0412cdb1168c80023104264819c7f1db9b016550543c33e9->doDisplay(Array, Array) (Line: 381)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355)
Twig_Template->display(Array) (Line: 366)
Twig_Template->render(Array) (Line: 64)
twig_render_template('core/themes/classy/templates/form/confirm-form.html.twig', Array) (Line: 389)
Drupal\Core\Theme\ThemeManager->render('confirm_form', Array) (Line: 438)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 200)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 231)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 577)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 232)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 122)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 95)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 116)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 62)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 53)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 103)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 82)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 55)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 632)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

I have a "collection" link defined in my MeetEntity class like this:

 *   links = {
 *     "canonical" = "/meet/{meet_entity}",
 *     "add-form" = "/team/{team_entity}/meet/add",
 *     "edit-form" = "/meet/{meet_entity}/edit",
 *     "delete-form" = "/meet/{meet_entity}/delete",
 *     "collection" = "/team/{team_entity}/meets",
 *   },

I don't have that route defined in my routing.yml file because that "collection" path defined in the links in my Entity class points to a view called "views.view.meets.yml". However, when I do add a route for that in my routing.yml file, I get a MissingMandatoryParametersException on the {team_entity}

I imagine I need to override some method somewhere but I can't figure out where. Can someone please help?

1 Answer 1

1

I finally figured it out. I had to override both Drupal\Core\Entity\EntityDeleteFormTrait::getRedirectUrl() and Drupal\Core\Entity\EntityDeleteFormTrait::getCancelUrl() in my MeetEntityDeleteForm class. Here's my code:

public function getRedirectUrl() {
    $entity = $this->getEntity();
    return Url::fromRoute('view.meets.page_1', ['arg_0',$entity->getTeamId()]);
}

public function getCancelUrl() {
    return $this->getRedirectUrl();
}

arg_0 in the call to fromRoute is the Team ID defined in the view's contextual relationship to the Team entity.

I was able to keep the meet_entity.collection route out of my routing.yml file. And my delete_form route was very simple:

entity.meet_entity.delete_form:
  path: '/meet/{meet_entity}/delete'
  defaults:
    # Calls the form.delete controller, defined in the team entity.
    _entity_form: meet_entity.delete
    _title: 'Delete Meet'
  requirements:
    _entity_access: 'meet_entity.delete'

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.