diff --git includes/common.inc includes/common.inc index d9f0509..0cb9430 100644 --- includes/common.inc +++ includes/common.inc @@ -4649,7 +4649,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 @@ -6609,6 +6609,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_type, $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 af012ea..c2fd85e 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/tests/field.test modules/field/tests/field.test index e090c4d..aa7defa 100644 --- modules/field/tests/field.test +++ modules/field/tests/field.test @@ -728,6 +728,8 @@ class FieldAttachStorageTestCase extends FieldAttachTestCase { $entities[1]->ftid => field_test_create_stub_entity($entities[1]->ftid, $entities[1]->ftvid), ) ); + // Unset the entity label from the expected results. + unset($expected[$entity_types[1]][$entities[1]->ftid]->ftlabel); $this->assertEqual($result, $expected, t('Result format is correct.')); // Now test the count/offset paging capability. @@ -830,6 +832,8 @@ class FieldAttachStorageTestCase extends FieldAttachTestCase { $entities[1]->ftid => field_test_create_stub_entity($entities[1]->ftid, $entities[1]->ftvid), ) ); + // Unset the entity label from the expected results. + unset($expected[$entity_type][$entities[1]->ftid]->ftlabel); $this->assertEqual($result, $expected, t('FIELD_QUERY_RETURN_IDS result format returns the expect result')); } } @@ -3157,3 +3161,52 @@ class FieldBulkDeleteTestCase extends FieldTestCase { $this->assertEqual(count($fields), 0, 'The field is purged.'); } } + + +/** + * Test properties of entity. + */ +class EntityProperties extends FieldTestCase { + public static function getInfo() { + return array( + 'name' => 'Entity tests', + 'description'=> 'Test entity functionality.', + 'group' => 'System', + ); + } + + function setUp() { + parent::setUp('field_test'); + } + + /** + * Test label key and label callback of an entity. + */ + function testEntityLabel() { + $entity_types = array( + 'test_entity_no_label', + 'test_entity_label', + 'test_entity_label_callback', + ); + + $entity = field_test_create_stub_entity(); + + foreach ($entity_types as $entity_type) { + $label = entity_label($entity_type, $entity); + + switch ($entity_type) { + case 'test_entity_no_label': + $this->assertFalse($label, 'Entity with no label property or callback returned FALSE.'); + break; + + case 'test_entity_label': + $this->assertEqual($label, $entity->ftlabel, 'Entity with label key returned correct label.'); + break; + + case 'test_entity_label_callback': + $this->assertEqual($label, 'label callback ' . $entity->ftlabel, 'Entity with label callback returned correct label.'); + break; + } + } + } +} \ No newline at end of file diff --git modules/field/tests/field_test.entity.inc modules/field/tests/field_test.entity.inc index 1126cbb..e8640aa 100644 --- modules/field/tests/field_test.entity.inc +++ modules/field/tests/field_test.entity.inc @@ -27,6 +27,7 @@ function field_test_entity_info() { 'name' => t('Test Entity'), 'fieldable' => TRUE, 'field cache' => FALSE, + 'base table' => 'test_entity', 'entity keys' => array( 'id' => 'ftid', 'revision' => 'ftvid', @@ -40,6 +41,49 @@ function field_test_entity_info() { 'name' => t('Test Entity, cacheable'), 'fieldable' => TRUE, 'field cache' => TRUE, + 'base table' => 'test_entity', + 'entity keys' => array( + 'id' => 'ftid', + 'revision' => 'ftvid', + 'bundle' => 'fttype', + ), + 'bundles' => $bundles, + 'view modes' => $test_entity_modes, + ), + + 'test_entity_no_label' => array( + 'name' => t('Test entity without label'), + 'fieldable' => TRUE, + 'field cache' => FALSE, + 'base table' => 'test_entity', + 'entity keys' => array( + 'id' => 'ftid', + 'revision' => 'ftvid', + 'bundle' => 'fttype', + ), + 'bundles' => $bundles, + 'view modes' => $test_entity_modes, + ), + 'test_entity_label' => array( + 'name' => t('Test entity label'), + 'fieldable' => TRUE, + 'field cache' => FALSE, + 'base table' => 'test_entity', + 'entity keys' => array( + 'id' => 'ftid', + 'revision' => 'ftvid', + 'bundle' => 'fttype', + 'label' => 'ftlabel', + ), + 'bundles' => $bundles, + 'view modes' => $test_entity_modes, + ), + 'test_entity_label_callback' => array( + 'name' => t('Test entity label callback'), + 'fieldable' => TRUE, + 'field cache' => FALSE, + 'base table' => 'test_entity', + 'label callback' => 'field_test_entity_label_callback', 'entity keys' => array( 'id' => 'ftid', 'revision' => 'ftvid', @@ -138,7 +182,7 @@ function field_test_delete_bundle($bundle) { /** * Creates a basic test_entity entity. */ -function field_test_create_stub_entity($id = 1, $vid = 1, $bundle = 'test_bundle') { +function field_test_create_stub_entity($id = 1, $vid = 1, $bundle = 'test_bundle', $label = '') { $entity = new stdClass(); // Only set id and vid properties if they don't come as NULL (creation form). if (isset($id)) { @@ -149,6 +193,9 @@ function field_test_create_stub_entity($id = 1, $vid = 1, $bundle = 'test_bundle } $entity->fttype = $bundle; + $label = !empty($label) ? $label : $bundle . ' label'; + $entity->ftlabel = $label; + return $entity; } diff --git modules/field/tests/field_test.install modules/field/tests/field_test.install index 538ba49..f502c1d 100644 --- modules/field/tests/field_test.install +++ modules/field/tests/field_test.install @@ -44,6 +44,13 @@ function field_test_schema() { 'not null' => TRUE, 'default' => '', ), + 'ftlabel' => array( + 'description' => 'The label of this test_entity.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), ), 'unique keys' => array( 'ftvid' => array('ftvid'), diff --git modules/field/tests/field_test.module modules/field/tests/field_test.module index 85f1a9e..63f6a0a 100644 --- modules/field/tests/field_test.module +++ modules/field/tests/field_test.module @@ -191,3 +191,18 @@ function field_test_field_delete($entity_type, $entity, $field, $instance, $item $args = func_get_args(); field_test_memorize(__FUNCTION__, $args); } + +/** + * Entity label callback. + * + * @param $entity_type + * The entity type. + * @param $entity + * The entity object. + * + * @return + * The label of the entity prefixed with "label callback". + */ +function field_test_entity_label_callback($entity_type, $entity) { + return 'label callback ' . $entity->ftlabel; +} \ No newline at end of file diff --git modules/node/node.module modules/node/node.module index a6b3bb6..7310e9a 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 24cb7a0..df55c89 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 d41c555..9f3526f 100644 --- modules/system/system.module +++ modules/system/system.module @@ -264,6 +264,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 de7258b..82ba648 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', @@ -123,6 +124,7 @@ function taxonomy_entity_info() { 'base table' => 'taxonomy_vocabulary', 'entity keys' => array( 'id' => 'vid', + 'label' => 'name', ), 'fieldable' => FALSE, ); diff --git modules/user/user.module modules/user/user.module index 97e1dca..c10ec09 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(