Our Blog

We always make sure you get the latest updates from devEngineering therefore we have crafted a well organized blog to share what we are planning for you.

How to add translation to custom entity in Drupal 7

drupal 7 custom entity translation

I want to tell you how to make an entity provided by a contributed module or your custom entity translatable in Drupal 7.
In this article, I'll make it for the 'Asset' custom entity.
Assets is a contribute module, that's why I've not included its code to this Article. Also, as you know, changing the contributed module's code is a bad practice, that's why all changes I'll write in my own custom module. So let's begin.

First of all, I need to change my entity's basic info. I'm using hook_entity_info_alter for this:

 * Implements hook_entity_info_alter().
 * @param $entity_info
function mymodule_entity_info_alter(&$entity_info) {
  $entity_info['asset']['entity keys']['language'] = 'language';
  $entity_info['asset']['translation'] = array(
    'entity_translation' => array(
      'class' => 'EntityTranslationDefaultHandler',
      'default settings' => array(
        'default_language' => LANGUAGE_NONE,
        'hide_language_selector' => FALSE,
      'base path' => 'admin/content/assets/manage/%asset',
      'translate path' => 'admin/content/assets/manage/%asset/translate',
      'path wildcard' => '%asset',

As you can see, I've added a new entity key and translation settings. I've used 'EntityTranslationDefaultHandler' class in this example, but, if you need, you can create your own class.
'base path' parameter contains the main entity's link.
'translate path' is the path for entity translation page.

In addition, we need to add a new field for the entity - language field. The best way to do this for an existing entity is to write an update function in the .install file in your custom module (mymodule.install).

 * Add language field to asset table.
function mymodule_update_7001() {
  $spec = array(
    'type' => 'varchar',
    'length' => 32,
    'not null' => FALSE,
  db_add_field('asset', 'language', $spec);

After adding this update run Drupal site's update script (example.com/update.php). Don't forget to clear caches.
Then you should enable translation for custom entity here: /admin/config/regional/entity_translation.

To make fields translatable you should check, which fields belong to the custom entity, then you need to make these fields translatable.
Such info is stored in 'field_config' table, so we need to update this table. Let's create a new update function for this:

 * Make all Asset fields translatable.
function mymodule_update_7002() {
    ->fields(array('translatable' => 1))
      ->condition('field_name', '%_asset_%', 'LIKE')
      ->condition('field_name', 'field_html', '=')
      ->condition('field_name', 'field_tables', '=')
      ->condition('field_name', 'field_description', '=')
      ->condition('field_name', 'field_tab_item', '=')
      ->condition('field_name', 'field_block', '=')

I've added all fields from the custom entity. It's up to you how you will add necessary fields to your update function.
After performing this update, clear all caches.
Now you can translate your custom entity.