0

I'm curious about how Drupal 8 handles nodes 404 requests. I think there might be a route of 404 and then an internal check - if node id doesn't exists, it simply calls 404 route path.

If this scenario is right, how can I achieve the same for custom entity?

I have a content entity type, but want a 404 redirection if someone access wrong entity id directly similar to any node page which doesn't exists.

1
  • There is only one 404 page. If a page is not found, then the configured 404 page (in the site information settings page) is shown. That's it. There is no difference between nodes or not.
    – Berdir
    Commented Jun 23, 2016 at 14:46

1 Answer 1

4

Ah, seeing that this is a follow-up to How to check if entity id exists or valid?, I think I know what the problem is.

If you ensure upcasting through the route, then Drupal throws a not found exception automatically and shows the 404 page.

So, you need to define your route as /entityform/{your_entity_type_id} and in the controller, type hint the argument YourEntityTypeId $your_entity_type_id. Then Drupal automatically loads the object, gives it to you and returns a 404 page if not found.

2
  • what does this line mean 'type hint the argument YourEntityTypeId $your_entity_type_id;' ? Can you explain this more? Commented Jun 24, 2016 at 5:20
  • @SugandhKhanna Type hinting isn't a Drupal thing: php.net/manual/en/…
    – Clive
    Commented Jun 24, 2016 at 9:42

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.