I have a custom entity, sample. I’ve used the entity display settings interface to set how I want this entity to display on the screen. However, I would like to add a link to another route when it is rendered. To accomplish this, I created a controller to render the sample entity outside the admin space, and added the correct link to my other entity. The problem I am now having is, I have other entities (e.g, batch) that contain entity reference links to samples. When these entities are rendered, the samples are created with links to the sample.canonical route, which is still in the admin area.

I can think of a few ways my problem could be solved. Which of these should I do?

  • I can somehow modify sample.canonical to include the necessary link. (Don’t know how to do this)
  • I can create a custom field formatter to render the sample with a different link.
  • Alter the batch entity to use a different route when rendering Samples (don’t know how to do this)

The first option seems the most flexible. I assume I need to work with the entity twig template, which I’ve tried but have not seen any changes appear on-screen.

Any advice? Is there an easier way I don’t know about?

  • Yes, the first options seems to be impossible if you want to keep the route in the admin space, because an entity can only have one canonical route. But you can add as many non-canonical routes as you like. Then you could create a custom field formatter with an option to configure entity routes other than canonical. Or simply use the core field formatter to produce a link with the canonical route and alter the route name in a hook. – 4k4 Jan 28 '19 at 20:50
  • I was actually able to get the first option working. I preprocessed my entity and added the rendered version of the entity I wanted to link to. I then added a twig template for the entity, and created a link. – Kevin Nowaczyk Jan 28 '19 at 21:44

Your Answer

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

Browse other questions tagged or ask your own question.