I learned that in EntityBase the label method gets called to determine the label for an entity, for instance when it is rendered via the entity reference module as label.

Now, for specific node types I want to alter that label. In the module apparently a callback (alter?) gets called:

  public function label() {
    $label = NULL;
    $entity_type = $this->getEntityType();
    if (($label_callback = $entity_type->getLabelCallback()) && is_callable($label_callback)) {
      $label = call_user_func($label_callback, $this);
    elseif (($label_key = $entity_type->getKey('label')) && isset($this->{$label_key})) {
      $label = $this->{$label_key};
    return $label;

How to register my own function for $entity_type->getLabelCallback()?

  • 1
    The lingo can be confusing at times. A node type is a bundle, such as article or page, therefore it's a subset/category of a specific entity type, not an entity type in itself, such as user or node. Perhaps you misunderstood? What are you trying to achieve? I am not sure if it's possible to specify different label callbacks for different node bundles.
    – Beebee
    Aug 22, 2019 at 15:40
  • @Beebee thanks, im basically having this problem: drupal.stackexchange.com/questions/284674/…
    – Alex
    Aug 22, 2019 at 16:06
  • 2
    I’m almost certain entity label callbacks have been deprecated in favour of the label method - I’d advise against using them. If you need different label logic per bundle, subclass Node and replace the class for the entity type via hook_entity_type_build
    – Clive
    Aug 22, 2019 at 16:08
  • @Alex how are you displaying your referenced nodes? Perhaps you could just do something through the display manager (see Hudri's answer on your other Q) or something with a simple hook like drupal.stackexchange.com/a/87079/5284 - you just need to set a condition to check the node type.
    – Beebee
    Aug 22, 2019 at 17:08


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.