Commit bfa62876 authored by webchick's avatar webchick

Issue #1616952 by webflo: Add a langcode parameter to EntityInterface::label().

parent aa9dd8ff
......@@ -42,16 +42,18 @@
* - 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. The entity label is the main string
* associated with an entity; for example, the title of a node or the
* subject of a comment. If there is an entity object property that defines
* the label, use the 'label' element of the 'entity keys' return
* 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 Drupal\entity\Entity::label() method, which
* implements this logic.
* - label callback: (optional) A function taking an entity and optional langcode
* argument, and returning the label of the entity. If langcode is omitted, the
* entity's default language is used.
*
* The entity label is the main string associated with an entity; for
* example, the title of a node or the subject of a comment. If there is an
* entity object property that defines the label, use the 'label' element
* of the 'entity keys' return 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
* 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
......
......@@ -420,14 +420,18 @@ function entity_uri($entity_type, $entity) {
*
* @param Drupal\entity\EntityInterface $entity
* The entity for which to generate the label.
* @param $langcode
* (optional) The language code of the language that should be used for
* getting the label. If set to NULL, the entity's default language is
* used.
*
* @return
* The label of the entity, or NULL if there is no label defined.
*
* @see Drupal\entity\EntityInterface::label()
*/
function entity_page_label(EntityInterface $entity) {
return $entity->label();
function entity_page_label(EntityInterface $entity, $langcode = NULL) {
return $entity->label($langcode);
}
/**
......
......@@ -87,11 +87,11 @@ public function bundle() {
/**
* Implements EntityInterface::label().
*/
public function label() {
public function label($langcode = NULL) {
$label = FALSE;
$entity_info = $this->entityInfo();
if (isset($entity_info['label callback']) && function_exists($entity_info['label callback'])) {
$label = $entity_info['label callback']($this->entityType, $this);
$label = $entity_info['label callback']($this->entityType, $this, $langcode);
}
elseif (!empty($entity_info['entity keys']['label']) && isset($this->{$entity_info['entity keys']['label']})) {
$label = $this->{$entity_info['entity keys']['label']};
......
......@@ -79,10 +79,15 @@ public function bundle();
/**
* Returns the label of the entity.
*
* @param $langcode
* (optional) The language code of the language that should be used for
* getting the label. If set to NULL, the entity's default language is
* used.
*
* @return
* The label of the entity, or NULL if there is no label defined.
*/
public function label();
public function label($langcode = NULL);
/**
* Returns the URI elements of the entity.
......
......@@ -211,11 +211,13 @@ function field_test_dummy_field_storage_query(EntityFieldQuery $query) {
* The entity type.
* @param $entity
* The entity object.
* @param $langcocde
* (optional) The langcode.
*
* @return
* The label of the entity prefixed with "label callback".
*/
function field_test_entity_label_callback($entity_type, $entity) {
function field_test_entity_label_callback($entity_type, $entity, $langcode = NULL) {
return 'label callback ' . $entity->ftlabel;
}
......
......@@ -186,6 +186,9 @@ function user_uri($user) {
/**
* Entity label callback.
*
* This label callback has langcode for security reasons. The username is the
* visual identifier for a user and needs to be consistent in all languages.
*
* @param $entity_type
* The entity type.
* @param $entity
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment