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.

  • 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 Jun 23 '16 at 14:46

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.

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

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.