diff --git includes/common.inc includes/common.inc index e0a947c..64a8808 100644 --- includes/common.inc +++ includes/common.inc @@ -2108,7 +2108,7 @@ function url($path = NULL, array $options = array()) { * @param $path * The internal path or external URL being linked to, such as "node/34" or * "http://example.com/foo". - * @return + * @return * Boolean TRUE or FALSE, where TRUE indicates an external path. */ function url_is_external($path) { @@ -6642,6 +6642,31 @@ function entity_uri($entity_type, $entity) { } /** + * 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; +} + + +/** * Helper function for attaching field API validation to entity forms. */ function entity_form_field_validate($entity_type, $form, &$form_state) { @@ -6669,7 +6694,7 @@ function entity_form_field_validate($entity_type, $form, &$form_state) { * For some entity forms (e.g., forms with complex non-field data and forms that * simultaneously edit multiple entities), this behavior may be inappropriate, * so the #builder_function for such forms needs to implement the required - * functionality instead of calling this function. + * functionality instead of calling this function. */ function entity_form_submit_build_entity($entity_type, $entity, $form, &$form_state) { $info = entity_get_info($entity_type); diff --git modules/comment/comment.module modules/comment/comment.module index be9def2..bab0ff4 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 ad53c0a..39547df 100644 --- modules/field/tests/field.test +++ modules/field/tests/field.test @@ -2966,3 +2966,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; + } + } + } +} diff --git modules/field/tests/field_test.entity.inc modules/field/tests/field_test.entity.inc index e05b762..0333f1e 100644 --- modules/field/tests/field_test.entity.inc +++ modules/field/tests/field_test.entity.inc @@ -73,6 +73,50 @@ function field_test_entity_info() { 'bundles' => array('test_entity_2' => array('label' => 'Test entity 2')), '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', + 'bundle' => 'fttype', + ), + 'bundles' => $bundles, + 'view modes' => $test_entity_modes, + ), + + ); } @@ -163,7 +207,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)) { @@ -174,6 +218,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 9a81571..7ccb61e 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 0d3cd41..f424aa9 100644 --- modules/field/tests/field_test.module +++ modules/field/tests/field_test.module @@ -212,3 +212,18 @@ function field_test_dummy_field_storage_query(EntityFieldQuery $query) { ), ); } + +/** + * 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 79a18c5..052ef57 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 3115ad4..becc3be 100644 --- modules/system/system.api.php +++ modules/system/system.api.php @@ -87,6 +87,11 @@ 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. This property can be used when the label is a + * result of complex logic, otherwise the "label" property under + * the "entity keys" array, should be used. * - 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: @@ -103,6 +108,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 4f86a30..6a25d9b 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 f32b946..02f49e4 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 d2efcf8..c64b593 100644 --- modules/user/user.module +++ modules/user/user.module @@ -146,6 +146,7 @@ function user_entity_info() { 'controller class' => 'UserController', 'base table' => 'users', 'uri callback' => 'user_uri', + 'label callback' => 'user_label', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'uid', @@ -180,6 +181,22 @@ function user_uri($user) { } /** + * Entity label callback. + * + * @param $entity_type + * The entity type, which is always 'user'. + * @param $account + * The user object. + * @return + * The user name, using format_username(). + * + * @see entity_label() + */ +function user_label($entity_type, $account) { + return format_username($account); +} + +/** * Implements hook_field_extra_fields(). */ function user_field_extra_fields() {