7.x common.inc entity_label($entity_type, $entity)

Returns the label of an entity.

See the 'label callback' component of the hook_entity_info() return value for more information.

Parameters

$entity_type: The entity type; e.g., 'node' or 'user'.

$entity: The entity for which to generate the label.

Return value

The entity label, or FALSE if not found.

1 call to entity_label()
EntityPropertiesTestCase::testEntityLabel in modules/field/tests/field.test
Tests label key and label callback of an entity.
1 string reference to 'entity_label'
taxonomy_menu in modules/taxonomy/taxonomy.module
Implements hook_menu().

File

includes/common.inc, line 8213
Common functions that many Drupal modules will need to reference.

Code

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;
}

Comments

alanom’s picture

Don't forget that $entity must be the object of the entity - so if you do something like, entity_label('node', entity_load('node',array($id)));, it won't work because entity_load gives an array wrapper around the object(s): use something equivalent to entity_label('node', reset(entity_load('node',array($id)))); or entity_label('node', entity_load('node',array($id))[$id]);

tien.xuan.vo’s picture

Now we have entity_load_single($entity_type, $id);

Dave Reid’s picture

The code receiving this result must ensure that check_plain() is called on it before it is printed to the page. Otherwise you could be making your module vulnerable to an XSS attack.

alberto56’s picture

Is there a Drupal 8 equivalent for this?

slashrsm’s picture