Because Drupal 7's entity_label() function doesn't accept nor pass through a $langcode parameter to the label callback, the $langcode parameter in title_entity_label() will in fact always be NULL. I filed #1764174: Ensure that all core entity label and URL tokens actually use entity_label() and entity_uri() against Token module to fix this for all core label tokens like [node:title] and [term:name]. But even if we pass a specific language code to entity_lable() in Drupal 7 it does not work, so things like using [node:title] for nodes URL aliases in entity translation don't actually work.

  1. We should attempt to still fetch the $langcode parameter using debug_backtrace() in title_entity_label(). Is there a core feature request filed against D7 to add $langcode to entity_label()?
  2. We should remove title_tokens_alter() and encourage people to have token module installed to fix core tokens. Any other entities should be calling entity_label() for their tokens and if not, it's a bug. We should file an issue to fix this in core's token integration.

Also fun fact, title_tokens_alter() does not work for taxonomy terms since the entity type 'taxonomy_term' does not match the token type 'term'.

Comments

dave reid’s picture

Part one would look like this:

 function title_entity_label($entity, $type, $langcode = NULL) {
 +  if (!isset($langcode)) {
 +    // Attempt to fetch the langcode parameter from the call to entity_label().
 +    $backtrace = debug_backtrace();
 +    if ($backtrace[1]['function'] == 'entity_label' && !empty($backtrace[1]['args'][2])) {
 +      $langcode = $backtrace[1]['args'][2];
 +    }
 +  }
dave reid’s picture

dave reid’s picture

I've also filed #1775768: Backport langcode support for all entity labels to actually get real langcode support for Drupal 7's entity_label() since it can be added optionally without modifying or breaking existing code.

plach’s picture

Sorry, I'm pretty busy with porting ET to D8 these days. I'll get back to this ASAP.