diff --git includes/common.inc includes/common.inc index 257542e..00ab385 100644 --- includes/common.inc +++ includes/common.inc @@ -4647,7 +4647,7 @@ function drupal_cron_cleanup() { /** * Returns information about system object files (modules, themes, etc.). - * + * * This function is used to find all or some system object files (module files, * theme files, etc.) that exist on the site. It searches in several locations, * depending on what type of object you are looking for. For instance, if you @@ -6605,6 +6605,32 @@ function entity_uri($entity_type, $entity) { return $entity->uri ? $entity->uri : NULL; } + +/** + * Returns the label of an entity. + * + * @param $entity_type + * The entity type; e.g. 'node' or 'user'. + * @param $entity + * The entity for which to generate a path. + * + * @return + * A string with the entity label (e.g. node title), 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); + } + elseif (!empty($info['entity keys']['label']) && isset($entity->{$info['entity keys']['label']})) { + $label = $entity->{$info['entity keys']['label']}; + } + + return $label; +} + + /** * Invokes entity insert/update hooks. * diff --git modules/comment/comment.module modules/comment/comment.module index bda5f6d..af25f13 100644 --- modules/comment/comment.module +++ modules/comment/comment.module @@ -103,6 +103,7 @@ function comment_entity_info() { 'entity keys' => array( 'id' => 'cid', 'bundle' => 'node_type', + 'label' => 'subject', ), 'bundles' => array(), 'view modes' => array( diff --git modules/field/field.crud.inc modules/field/field.crud.inc index dbb0e03..efe0d06 100644 --- modules/field/field.crud.inc +++ modules/field/field.crud.inc @@ -266,6 +266,10 @@ function field_create_field($field) { // collisions with existing entity properties, but some is better // than none. foreach (entity_get_info() as $type => $info) { + + // The label property marks the label location (e.g. title on a node object) + // we don't need to treat it as a reserved field name. + unset($info['entity keys']['label']); if (in_array($field['field_name'], $info['entity keys'])) { throw new FieldException(t('Attempt to create field name %name which is reserved by entity type %type.', array('%name' => $field['field_name'], '%type' => $type))); } diff --git modules/node/node.module modules/node/node.module index 9ab2454..b8207d1 100644 --- modules/node/node.module +++ modules/node/node.module @@ -181,6 +181,7 @@ function node_entity_info() { 'id' => 'nid', 'revision' => 'vid', 'bundle' => 'type', + 'label' => 'title', ), 'bundle keys' => array( 'bundle' => 'type', diff --git modules/system/system.api.php modules/system/system.api.php index a4d3510..ee49187 100644 --- modules/system/system.api.php +++ modules/system/system.api.php @@ -90,6 +90,9 @@ function hook_hook_info_alter(&$hooks) { * - uri callback: A function taking an entity as argument and returning the * uri elements of the entity, e.g. 'path' and 'options'. The actual entity * uri can be constructed by passing these elements to url(). + * - label callback: Optional; A function taking an entity as argument and + * returning the label of the entity, e.g. the title of a node, or the + * subject of a comment. * - fieldable: Set to TRUE if you want your entity type to be fieldable. * - entity keys: An array describing how the Field API can extract the * information it needs from the objects of the type. Elements: @@ -106,6 +109,11 @@ function hook_hook_info_alter(&$hooks) { * omitted if this entity type exposes a single bundle (all entities have * the same collection of fields). The name of this single bundle will be * the same as the entity type. + * - label: The name of the property that contains the entity label. For + * example, the value of this property in the node entity will be 'title'. + * Note that it is possible that an entity label is a result of more + * complex logic, in such a case the "label callback" should be used. + * see entity_label(). * - bundle keys: An array describing how the Field API can extract the * information it needs from the bundle objects for this type (e.g * $vocabulary objects for terms; not applicable for nodes). This entry can diff --git modules/system/system.module modules/system/system.module index 8182427..01d8fc3 100644 --- modules/system/system.module +++ modules/system/system.module @@ -263,6 +263,7 @@ function system_entity_info() { 'base table' => 'file_managed', 'entity keys' => array( 'id' => 'fid', + 'label' => 'filename', ), 'static cache' => FALSE, ), diff --git modules/taxonomy/taxonomy.module modules/taxonomy/taxonomy.module index a2a0ce0..401ef8d 100644 --- modules/taxonomy/taxonomy.module +++ modules/taxonomy/taxonomy.module @@ -92,6 +92,7 @@ function taxonomy_entity_info() { 'entity keys' => array( 'id' => 'tid', 'bundle' => 'vocabulary_machine_name', + 'label' => 'name', ), 'bundle keys' => array( 'bundle' => 'machine_name', diff --git modules/user/user.module modules/user/user.module index ea934cc..1fb9f6d 100644 --- modules/user/user.module +++ modules/user/user.module @@ -133,6 +133,7 @@ function user_entity_info() { 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'uid', + 'label' => 'name', ), 'bundles' => array( 'user' => array(