summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/common.inc24
-rw-r--r--modules/comment/comment.module1
-rw-r--r--modules/field/tests/field.test48
-rw-r--r--modules/field/tests/field_test.entity.inc47
-rw-r--r--modules/field/tests/field_test.install7
-rw-r--r--modules/field/tests/field_test.module13
-rw-r--r--modules/node/node.module1
-rw-r--r--modules/system/system.api.php10
-rw-r--r--modules/system/system.module1
-rw-r--r--modules/taxonomy/taxonomy.module2
-rw-r--r--modules/user/user.module1
11 files changed, 154 insertions, 1 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 2785b3f5e..dc4624d6c 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -6912,6 +6912,30 @@ 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);
+ }
+ 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) {
diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index dc211b424..2d038d1d1 100644
--- a/modules/comment/comment.module
+++ b/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 a/modules/field/tests/field.test b/modules/field/tests/field.test
index 479b0a966..e865ac22d 100644
--- a/modules/field/tests/field.test
+++ b/modules/field/tests/field.test
@@ -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;
+ }
+ }
+ }
+}
diff --git a/modules/field/tests/field_test.entity.inc b/modules/field/tests/field_test.entity.inc
index b7887c812..149c09a2c 100644
--- a/modules/field/tests/field_test.entity.inc
+++ b/modules/field/tests/field_test.entity.inc
@@ -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;
}
diff --git a/modules/field/tests/field_test.install b/modules/field/tests/field_test.install
index d4937b620..268d271c7 100644
--- a/modules/field/tests/field_test.install
+++ b/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 a/modules/field/tests/field_test.module b/modules/field/tests/field_test.module
index f615129ff..5adc3a4c1 100644
--- a/modules/field/tests/field_test.module
+++ b/modules/field/tests/field_test.module
@@ -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;
+}
diff --git a/modules/node/node.module b/modules/node/node.module
index c65ca30f9..b5de4d739 100644
--- a/modules/node/node.module
+++ b/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 a/modules/system/system.api.php b/modules/system/system.api.php
index c9b140167..a7720f145 100644
--- a/modules/system/system.api.php
+++ b/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., $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
diff --git a/modules/system/system.module b/modules/system/system.module
index f0cab512c..21b2f79ad 100644
--- a/modules/system/system.module
+++ b/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 a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index 45ae7c091..3d06f111f 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -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,
);
diff --git a/modules/user/user.module b/modules/user/user.module
index bc864cee2..89c0efabf 100644
--- a/modules/user/user.module
+++ b/modules/user/user.module
@@ -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',