Change record status: 
Project: 
Introduced in branch: 
7.x
Description: 

Note: This change notice was about 8.x too, but the $entity_type argument has been removed again there, as $entity is now an instance of EntityInterface and the entity type can be checked with $entity->getEntityTypeId().

Any label callback defined in an entity now receives two arguments: $entity and $entity_type. This allows label callbacks that apply to multiple entity types.

Example:

function example_entity_info() {
  ...
  $entities['example']['label callback'] = 'example_label';
  return $entities;
}

// Drupal 7.0 and up:
function example_label($entity) {
  ...
}

// Drupal 7.2 and up:
function example_label($entity, $entity_type) {
  ...
}

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

imadalin’s picture

Actually based on the code and the API docs, params are reversed:

/**
 * Returns the label of an entity.
 *
 * See the 'label callback' component of the hook_entity_info() return value
 * for more information.
 *
 * @param $entity_type
 *   The entity type; e.g., 'node' or 'user'.
 * @param $entity
 *   The entity for which to generate the label.
 *
 * @return
 *   The entity label, or FALSE if not found.
 */
function entity_label($entity_type, $entity) {
  $label = FALSE;
  $info = entity_get_info($entity_type);
  if (isset($info['label callback']) && function_exists($info['label callback'])) {
    $label = $info['label callback']($entity, $entity_type);
  }
  elseif (!empty($info['entity keys']['label']) && isset($entity->{$info['entity keys']['label']})) {
    $label = $entity->{$info['entity keys']['label']};
  }

  return $label;
}