#629484 by Amitaibu, fago, et al: Added Add entity 'label' key info (e.g. title on node).

merge-requests/26/head
Angie Byron 2010-09-11 06:03:12 +00:00
parent f096a70e67
commit b06d19e349
11 changed files with 154 additions and 1 deletions

View File

@ -6911,6 +6911,30 @@ 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;
}
/**
* Helper function for attaching field API validation to entity forms.
*/

View File

@ -103,6 +103,7 @@ function comment_entity_info() {
'entity keys' => array(
'id' => 'cid',
'bundle' => 'node_type',
'label' => 'subject',
),
'bundles' => array(),
'view modes' => array(

View File

@ -2988,3 +2988,51 @@ class FieldBulkDeleteTestCase extends FieldTestCase {
$this->assertEqual(count($fields), 0, 'The field is purged.');
}
}
/**
* Tests entity properties.
*/
class EntityPropertiesTestCase extends FieldTestCase {
public static function getInfo() {
return array(
'name' => 'Entity properties',
'description'=> 'Tests entity properties.',
'group' => 'Entity API',
);
}
function setUp() {
parent::setUp('field_test');
}
/**
* Tests 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;
}
}
}
}

View File

@ -73,6 +73,48 @@ function field_test_entity_info() {
'bundles' => array('test_entity_2' => array('label' => 'Test entity 2')),
'view modes' => $test_entity_modes,
),
// @see EntityPropertiesTestCase::testEntityLabel()
'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 +205,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 +216,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;
}

View File

@ -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'),

View File

@ -214,3 +214,16 @@ function field_test_dummy_field_storage_query(EntityFieldQuery $query) {
),
);
}
/**
* Entity label callback.
*
* @param $entity
* The entity object.
*
* @return
* The label of the entity prefixed with "label callback".
*/
function field_test_entity_label_callback($entity) {
return 'label callback ' . $entity->ftlabel;
}

View File

@ -181,6 +181,7 @@ function node_entity_info() {
'id' => 'nid',
'revision' => 'vid',
'bundle' => 'type',
'label' => 'title',
),
'bundle keys' => array(
'bundle' => 'type',

View File

@ -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., $node->title or
* $comment->subject. A callback should be specified when the label is the
* result of complex logic. Otherwise, the 'label' property of the
* 'entity keys' the property should be used.
* - fieldable: Set to TRUE if you want your entity type to be fieldable.
* - translation: An associative array of modules registered as field
* translation handlers. Array keys are the module names, array values
@ -107,6 +112,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 property name of the entity that contains the label. For
* example, if the entity's label is located in $entity->subject, then
* 'subect' should be specified here. In case complex logic is required to
* build the label, a 'label callback' should be implemented instead. See
* entity_label() for details.
* - 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

View File

@ -264,6 +264,7 @@ function system_entity_info() {
'base table' => 'file_managed',
'entity keys' => array(
'id' => 'fid',
'label' => 'filename',
),
'static cache' => FALSE,
),

View File

@ -100,6 +100,7 @@ function taxonomy_entity_info() {
'entity keys' => array(
'id' => 'tid',
'bundle' => 'vocabulary_machine_name',
'label' => 'name',
),
'bundle keys' => array(
'bundle' => 'machine_name',
@ -131,6 +132,7 @@ function taxonomy_entity_info() {
'base table' => 'taxonomy_vocabulary',
'entity keys' => array(
'id' => 'vid',
'label' => 'name',
),
'fieldable' => FALSE,
);

View File

@ -146,6 +146,7 @@ function user_entity_info() {
'controller class' => 'UserController',
'base table' => 'users',
'uri callback' => 'user_uri',
'label callback' => 'format_username',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'uid',