How to make Entity View show Entity's custom dynamic label as page title (D8) My custom content Entity overrides label() to provide a dynamically generated, human-readable label like:

public function label() {
  return $this-&gt;displayName();
}


Unfortunately, that label does not get used by the EntityViewController. In my module.routing.yml file I have:

entity.veranstaltung.canonical:
  path: '/admin/veranstaltung/{veranstaltung}'
  defaults:
    _entity_view: 'veranstaltung'
  requirements:
    _entity_access: 'veranstaltung.view'


I can now view my "veranstaltung" entities, but the page title is blank and the breadcrumb just shows the entity's id instead of its label.

Looking in EntityViewController::buildTitle() I can now see why this is the case:

$label_field = $entity-&gt;getEntityType()-&gt;getKey('label');
if (isset($page[$label_field])) {
  $page['#title'] = $this-&gt;renderer-&gt;render($page[$label_field]);
}


I don't have a label key, as I want the labels to be dynamically generated.

Any suggestions how to best deal with this problem?
Similar to the NodeViewController, you can write you own VeranstaltungViewController extending EntityViewController, and implement a title function. In your route can then set _title_callback to use this function (see NodeRouteProvider as reference).