1

My custom content Entity overrides label() to provide a dynamically generated, human-readable label like:

public function label() {
  return $this->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->getEntityType()->getKey('label');
if (isset($page[$label_field])) {
  $page['#title'] = $this->renderer->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?

4

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).

  • Sounds plausible. I guess, if I override EntityViewController::buildTitle(), I won't need the _title_callback? – Hendrik Aug 12 '16 at 14:35
  • That is how I understood the titles: The page title is generated by buildTitle(), and the breadcrumbs by either _title or _title_callback – Maouna Aug 12 '16 at 15:45
  • So, just to get an understanding of this: Why doesn't EntityViewController use EntityInterface::label() to generate page titles and bread crumbs? – Hendrik Aug 17 '16 at 9:29
  • 1
    EntityViewController contains a comment explaining the reason: // If the entity's label is rendered using a field formatter, set the // rendered title field formatter as the page title instead of the default // plain text title. This allows attributes set on the field to propagate // correctly (e.g. RDFa, in-place editing). It does not use the string of the label, but renders it with its field formatter. – Maouna Aug 17 '16 at 14:39

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.