1

I am trying to enable translation for a field which was defined by a different module.

The problem is this field does not list in the UI. When I checked the definition for this field in hook_entity_field_storage_info I can see the setTranslatable(TRUE) is missing. So I added the same in hook_entity_field_storage_info_alter . After this I can see the field listing in the UI for translation, but when I enable the checkbox and submit the value is not saved.

How can I enable translation for this field?

  • 1
    Could you provide some more information, what module does the field come from, what is the name of the entity, what is the name of the field etc. That would make it easier to help you, thanks! – user72672 Jan 11 at 8:26
  • So, the module which I'm using is drupal.org/project/workbench_reviewer . workbench_reviewer is the field which I'm trying to update to enable translation. After checking here - drupal.org/node/3034742 I tried Updating an existing field storage definition in hook_update_n . Then I added hook_entity_field_storage_info_alter and hook_entity_bundle_field_info_alter to my module. After this I could see the field appearing in UI for translation. This method works for me. Just wanted to see if a better solution is out there. If not I can add this as answer. – wilNev Jan 13 at 9:36
  • Is the solution in the OP fully functional or not? You mention "when I enable the checkbox and submit the value is not saved.", which sounds as if this is not working. Can you confirm that? – Stefanos Petrakis Jan 14 at 13:41
  • 1
    Unfortunately the field is added by code so there is no other way to modify its definition other that implementing an hook_update_N. For fields installed using configuration files (e.g. body field for node), once the field is installed the site owns the configuration so can be easily edited and updated (if the field has no content). – d70rr3s Jan 14 at 14:07
1

After a bit of struggle and research, here is the approach taken -

custom_module.install file -

/**
 * Enable translation for workbench_reviewer field.
 * Implements hook_update_n.
*/
function hook_update_xxxx() {
  $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $field_storage_definition_node = $entity_definition_update_manager->getFieldStorageDefinition('workbench_reviewer', 'node');
  $field_storage_definition_node->setTranslatable(TRUE);
  $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition_node);

}

custom_module.module file -

/**
 * Implements hook_entity_field_storage_info_alter().
 */
function hook_entity_field_storage_info_alter(&$fields, EntityTypeInterface $entity_type) {
  // Alter translation settings.
  if (($entity_type->id() == 'node' || $entity_type->id() == 'taxonomy_term') && !empty($fields['workbench_reviewer'])) {
    $fields['workbench_reviewer']->setTranslatable(TRUE);
  }
}

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function hook_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
  // Alter translation settings.
  if (($entity_type->id() == 'node' || $entity_type->id() == 'taxonomy_term') && !empty($fields['workbench_reviewer'])) {
    $fields['workbench_reviewer']->setTranslatable(TRUE);
  }
}

Once running drush updb I can see the field appearing in translation UI with the checkbox enabled.

| improve this answer | |
  • 1
    Could you patch the workbench reviewer module ? – Daniel Harper Jan 16 at 19:38
  • Yes, in this case that would be the proper way to proceed. Drupal core's is multilingual by design so any contrib should follow that pattern. Thanks @DanielHarper for point it out. – d70rr3s Jan 17 at 9:00
-1

You can set it to “Translatable” at the “Entity” level and as such you need the following hook.

hook_entity_base_field_info_alter

It alters existing field definitions -like ones defined by other modules. There is also

[hook_entity_base_field_info]

to provide new field definitions

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_base_field_info/8.8.x

The hook you used [hook_entity_field_storage_info_alter] works at the field level.

For example, if some module wanted to create Entity for new field ( called my-title) and make it Translatable at the “Entity” level it would be like:

$fields[‘my-title’] = BaseFieldDefinition::create(‘string’)
   →setTranslatable(TRUE);

So, the the hook(described above) is changing such a “base definition of the given Entity”.

The Steps you will need in your code is:

1) Load the given Entity.

2) Set it by using [setTranslatable(TRUE)].

3) save it (similar to $node→save();).

For example, for the “Article” NodeType, step 1 can be

$type = \Drupal::entityTypeManager()→getStorage(‘node_type’)→
load(article’);

Check out the following for deeper understanding:

\Drupal\core\Entity\Query\QueryInterface.

 BaseFieldDefinition’s setTranslatable method
  & isTranslatable method

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21BaseFieldDefinition.php/class/BaseFieldDefinition/8.8.x

The docs have it buried it

In Drupal 8 field language is no longer exposed in the public API, instead fields are attached to language-aware entity objects from which they "inherit" their language

https://www.drupal.org/docs/8/api/entity-api/entity-translation-api

| improve this answer | |
  • 1
    This already dicussed by OP on the question itself and the comments and also does not provides an actual answer or relevant facts about the question per-se. The OP is asking about if theres any other way (besides hook_entity_field_storage_info_alter) to enable translation for a field provided by a contrib module – d70rr3s Jan 14 at 13:10
  • Nope. The hooks are different -look closely and remove the downvote. The one I provided has base. – salah-1 Jan 14 at 13:25
  • The hook I provided targets the “Entity” not the “Field” – salah-1 Jan 14 at 13:33
  • 2
    I know they are different the point is the question make by the OP: This method works for me. Just wanted to see if a better solution is out there. If not I can add this as answer. The answer no theres no other method regardless the fields is a base or bundle field. also the module referred by the OP uses a bundle not a base field git.drupalcode.org/project/workbench_reviewer/blob/8.x-2.x/… – d70rr3s Jan 14 at 13:56
  • 1
    Also your answer is incomplete, you cannot alter a field definition once installed, you need to implement an update, the OP already discovered that. – d70rr3s Jan 14 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.