As the label should be translatable, we should add an optional langcode parameter to EntiyInterface::label(). As default, I'd suggest to go with the same handling as we do now for getters/setters, i.e. go with the default language of entities.

Having a better default language, let's say current interface or content language might be feasible here, but let's keep that out of this issue for now as it would involve respective fallback handling as well. Let's solve that in another issue, but just make sure we have a language-enabled EntiyInterface::label() for now.

Comments

fago’s picture

Title: add an langcode parameter to EntiyInterface::label() » add a langcode parameter to EntiyInterface::label()
webflo’s picture

Title: add a langcode parameter to EntiyInterface::label() » Add a langcode parameter to EntityInterface::label()
webflo’s picture

Status: Active » Needs review
StatusFileSize
new3.56 KB

Should i add the $langcode parameter to 'user_label' too?

berdir’s picture

Status: Needs review » Needs work

I just RTBC'd #1616952: Add a langcode parameter to EntityInterface::label(), which removes/renames the entity_label() function, not sure if entity_page_label() needs the langcode as well. If not, we could simply remove it from this function and then they wouldn't conflict.

+++ b/core/modules/entity/lib/Drupal/entity/Entity.phpundefined
@@ -89,13 +89,14 @@ class Entity implements EntityInterface {
     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);

I guess this needs to be documented somewhere and the existing label callbacks, if we have any (?) should be updated.

berdir’s picture

webflo’s picture

Status: Needs work » Needs review
StatusFileSize
new2.6 KB

I think $langcode on entity_page_label() is still useful. Rerolled against 8.x HEAD after #1615240: Remove entity_label() in favor of EntityInterface::label() was committed.

berdir’s picture

Status: Needs review » Needs work

We should document the additional argument to the label callback documentation in hook_entity_info() and update the user_label() and field_test_entity_label_callback().

Is there already an issue to remove entity type from the callback? If not then we should remove it. Or, can we somehow integrate a defaultLabel() method to the entity class, similar to 7.x contrib entity class. (Separate issue, of course).

webflo’s picture

Adding a $langcode to user_label() makes no sense because the username is not a multilingual property. Updated field_test_entity_label_callback() and documentation.

berdir’s picture

Status: Needs review » Needs work

Hm, yes, but user_label() is a callback that receives a pre-defined list of arguments. IMHO the function definition should match to what it is called with. However, I don't want to decide that, who can we ask who can? :)

+++ b/core/modules/entity/entity.api.phpundefined
@@ -42,15 +42,17 @@
+ *   - label callback: (optional) A function taking an entity and langcode as
+ *     argument and returning the label of the entity. The langcode is
+ *     optional. 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

It looks like there is a lot of empty space on various lines. Should be able to group that differently, as we need to change it anyway, as it seems. E.g. "The langcode is optional" should fit on that line without going over 80 chars.

webflo’s picture

We discussed the usage of user_label() at drupaldevdays. We (xjm, webchick, heyrocker, me) reached consensus that the username is not a multilingual property. The username is the visual identifier for a user and needs to be consistent in all languages. Therefor $langcode is not required.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Ok, looks good to go then. Not exactly sure what the "security reasons" comment exactly means.

I fully agree that the user name is not translatable. That wasn't my point. My point was only that this is a callback function, which receives a fixed set of arguments, and the function definition should reflect that. The implementation within is free to ignore the language of course. Doesn't matter much.

webchick’s picture

Sorry, by "security" I meant more "identity." Your username is the only way (in core anyway) to canonically identify YOUR comments/posts on the site to YOU. It's fully conceivable that Drupal.org has both a "sun" and a "sol" user. sun's comments should not get misattributed to sol when the user chooses Spanish as their language.

Status: Reviewed & tested by the community » Needs work
Issue tags: -Entity system, -D8MI, -sprint

The last submitted patch, add-langcode-to-EntityInterface-label-1616952-11.patch, failed testing.

webflo’s picture

Status: Needs work » Needs review
Issue tags: +Entity system, +D8MI, +sprint
webflo’s picture

Status: Needs review » Reviewed & tested by the community

Test is green. Back to rtbc.

webchick’s picture

Title: Add a langcode parameter to EntityInterface::label() » Change notice for: Add a langcode parameter to EntityInterface::label()
Priority: Normal » Critical
Status: Reviewed & tested by the community » Active

Looks good! I just made a quick tweak to the docs to consolidate them a bit:

from...

+ *   - label callback: (optional) A function taking an entity and langcode as
+ *     argument and returning the label of the entity. The langcode is
+ *     optional. If langcode is omitted, the entity's default language is used.
+ *     The entity label is the main string associated with an entity; for

to...

 *   - 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

Committed and pushed to 8.x. Thanks!

This needs a change notice.

berdir’s picture

Status: Active » Needs review

Improved and extended http://drupal.org/node/1642962 so that it also covers this change.

aspilicious’s picture

Title: Change notice for: Add a langcode parameter to EntityInterface::label() » Add a langcode parameter to EntityInterface::label()
Priority: Critical » Normal
Status: Needs review » Fixed

Great!

dave reid’s picture

We discussed the usage of user_label() at drupaldevdays. We (xjm, webchick, heyrocker, me) reached consensus that the username is not a multilingual property. The username is the visual identifier for a user and needs to be consistent in all languages. Therefor $langcode is not required.

HOWEVER, user_label() eventually invokes drupal_alter('user_format_name') which is typically used by Realname module. When performing token replacement, we would want to know which langcode the username is being used as since we could provide the langcode for tokens. There *is* an actual valid use case for this.

berdir’s picture

Hm, there is actually even a much more obvious use case here: t('Anonymous').

dave reid’s picture

Yep, that's the more obvious one too.

gábor hojtsy’s picture

Issue tags: -sprint +language-content

Done, moving off sprint.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

webchick’s picture

This change has caused XSS by default in D7 templates being ported to D8. See #1811684: XSS: Bartik's node.tpl.php prone to XSS (prints $title).

gábor hojtsy’s picture