Issue #1615240 by Berdir, webflo, fago: Remove entity_label() in favor of EntityInterface::label().

8.0.x
webchick 2012-06-15 18:57:57 +02:00
parent 663c2748e3
commit 9aaf322992
6 changed files with 17 additions and 27 deletions

View File

@ -50,8 +50,8 @@
* value component to provide this information (see below). If more complex
* logic is needed to determine the label of an entity, you can instead
* specify a callback function here, which will be called to determine the
* entity label. See also the entity_label() function, which implements this
* logic.
* entity label. See also the Drupal\entity\Entity::label() method, which
* implements this logic.
* - 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

View File

@ -6,6 +6,7 @@
*/
use Drupal\entity\EntityMalformedException;
use Drupal\entity\EntityInterface;
/**
* Implements hook_help().
@ -414,31 +415,19 @@ function entity_uri($entity_type, $entity) {
/**
* Returns the label of an entity.
*
* See the 'label callback' component of the hook_entity_info() return value
* for more information.
* This is a wrapper for Drupal\entity\EntityInterface::label(). This function
* should only be used as a callback, e.g. for menu title callbacks.
*
* @param $entity_type
* The entity type; e.g., 'node' or 'user'.
* @param $entity
* @param Drupal\entity\EntityInterface $entity
* The entity for which to generate the label.
*
* @return
* The entity label, or FALSE if not found.
* The label of the entity, or NULL if there is no label defined.
*
* @todo
* Remove once all entity types are implementing the EntityInterface.
* @see Drupal\entity\EntityInterface::label()
*/
function entity_label($entity_type, $entity) {
$label = FALSE;
$info = entity_get_info($entity_type);
if (isset($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;
function entity_page_label(EntityInterface $entity) {
return $entity->label();
}
/**

View File

@ -86,8 +86,6 @@ class Entity implements EntityInterface {
/**
* Implements EntityInterface::label().
*
* @see entity_label()
*/
public function label() {
$label = FALSE;

View File

@ -36,7 +36,7 @@ class EntityPropertiesTest extends FieldTestBase {
$entity = field_test_create_stub_entity();
foreach ($entity_types as $entity_type) {
$label = entity_label($entity_type, $entity);
$label = entity_create($entity_type, (array) $entity)->label();
switch ($entity_type) {
case 'test_entity_no_label':

View File

@ -81,6 +81,7 @@ function field_test_entity_info() {
'fieldable' => TRUE,
'field cache' => FALSE,
'base table' => 'test_entity',
'revision table' => 'test_entity_revision',
'entity keys' => array(
'id' => 'ftid',
'revision' => 'ftvid',
@ -94,6 +95,7 @@ function field_test_entity_info() {
'fieldable' => TRUE,
'field cache' => FALSE,
'base table' => 'test_entity',
'revision table' => 'test_entity_revision',
'entity keys' => array(
'id' => 'ftid',
'revision' => 'ftvid',
@ -108,6 +110,7 @@ function field_test_entity_info() {
'fieldable' => TRUE,
'field cache' => FALSE,
'base table' => 'test_entity',
'revision table' => 'test_entity_revision',
'label callback' => 'field_test_entity_label_callback',
'entity keys' => array(
'id' => 'ftid',

View File

@ -25,19 +25,19 @@ class UserEntityCallbacksTest extends WebTestBase {
parent::setUp('user');
$this->account = $this->drupalCreateUser();
$this->anonymous = drupal_anonymous_user();
$this->anonymous = entity_create('user', (array) drupal_anonymous_user());
}
/**
* Test label callback.
*/
function testLabelCallback() {
$this->assertEqual(entity_label('user', $this->account), $this->account->name, t('The username should be used as label'));
$this->assertEqual($this->account->label(), $this->account->name, t('The username should be used as label'));
// Setup a random anonymous name to be sure the name is used.
$name = $this->randomName();
variable_set('anonymous', $name);
$this->assertEqual(entity_label('user', $this->anonymous), $name, t('The variable anonymous should be used for name of uid 0'));
$this->assertEqual($this->anonymous->label(), $name, t('The variable anonymous should be used for name of uid 0'));
}
/**