It is actually really easy, except removing all references to canonical from the boilerplate code I just needed to override one single function toUrl
in my custom entity class:
src/Entity/MyContentEntity.php
/**
* Defines the chunk entity class.
* Note the missing "links = { canonical }" entry in the annotation
*
*
* @ContentEntityType(
* id = "chunk",
* ...
* links = {
* "add-form" = "/admin/content/chunk/add/{chunk_type}",
* "add-page" = "/admin/content/chunk/add",
* "edit-form" = "/admin/content/chunk/{chunk}/edit",
* "delete-form" = "/admin/content/chunk/{chunk}/delete",
* "collection" = "/admin/content/chunk"
* },
* ...
* )
*/
class Chunk extends ContentEntityBase implements ChunkInterface {
/**
* this prevents WSOD when 3rd party modules call $entity->toUrl
*/
public function toUrl($rel = 'canonical', array $options = []) {
if ($rel == 'canonical') {
return Url::fromUri('route:<nolink>')->setOptions($options);
}
else {
return parent::toUrl($rel, $options);
}
}
}
The other stuff here is only needed if the entity is based on an auto-generated boilerplate code from drush generate
or similar:
src/Form/MyContentEntityForm.php
/**
* The following change is only necessary if you use boilerplate code from "drush generate" or similar
*/
class ChunkForm extends ContentEntityForm {
public function save(array $form, FormStateInterface $form_state) {
...
// change the following line
//$form_state->setRedirect('entity.chunk.canonical', ['chunk' => $entity->id()]);
// to something of your choice
$form_state->setRedirect('entity.chunk.edit-form', ['chunk' => $entity->id()]);
}
}
my_module.links.task.yml
:
# If existing, remove the following boilerplate code from "drush generate" or similar
entity.chunk.view:
title: View
route_name: entity.chunk.canonical
base_route: entity.chunk.canonical