Problem/Motivation
Translating field labels requires manually clearing all caches in order to translated field label to be updated to anonymous users. Translating other strings (e.g. "Home") does not need any manual cache clearing.
Steps to reproduce
1. Install Drupal with standard installation profile. Do not use any development settings like cache.backend.null or turn off Twig cache.
2. Enable following modules language, locale.
3. Add language (e.g. Finnish).
4. Create article node as admin user add some tag. This way the field label Tags becomes visible.
5. Visit the created article at /fi/node/1 as an anonymous user and translation for "Tags" field label, imported when language was added (3.) is rendered correctly.
6. As an admin user change translation for "Tags" field label at /admin/config/regional/translate and submit form.
7. As an anonymous user, visit /fi/node/1 and same translation as in 5. is still visible.
Also, even translated field labels on the Content type fields page (/admin/structure/types/manage/article/fields) show the original values, until all caches are cleared.
Proposed resolution
Updating field label translations should invalidate appropriate caches.
Need to discuss and review more general solution, if possible.
Original report:
--
I have a Drupal multilingual website, when I translate the label for a content type field, and I set the label to be shown in the Manage display options, it's shown in the source language when visiting a translation.
See http://stackoverflow.com/questions/35683639/translated-content-field-lab...
The problem doesn't always occur and seems to be cache related, setting the following lines in settings.local.php will disable caching and "solves" the problem:
$settings['class_loader_auto_detect'] = FALSE;
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
Edit:
- as I'm using a node tease with views, this might be a views issue.
Workaround
Disable all caches during development, by turning on development mode with drush theme:dev on (new in Drush 13.6) which disables all caching (Drupal+Twig). (Toggle Twig debug, caching and CSS/JS aggregation with Drush)
| Comment | File | Size | Author |
|---|---|---|---|
| #37 | test-only-2824003-37.patch | 4.9 KB | mohit_aghera |
| #36 | 2824003.36.patch | 4.33 KB | ayush.khare |
| #33 | config-translate-label-cache-2824003-33.patch | 4.33 KB | fathima.asmat |
| #29 | interdiff-2824003-27-29.txt | 1009 bytes | yogeshmpawar |
| #29 | 2824003-29.patch | 675 bytes | yogeshmpawar |
Comments
Comment #2
mpp commentedThis patch is a workaround, it will (re-)translate the label. This might be a views issue as the display I'm using is a node teaser in a views list.
Comment #3
mpp commentedComment #4
mpp commentedComment #5
mpp commentedComment #13
sokru commentedI can confirm that this is still an issue in Drupal 8.7.8, I'm facing this on user profile, so not views related.
Comment #14
sokru commentedSpend many hours trying to figure out correct cache to flush, but ended up to obviously bad practice of flushing all caches on form submit. Hopefully someone else finds proper solution for this.
Comment #15
sokru commentedComment #16
sokru commentedComment #17
sokru commentedComment #18
sokru commentedComment #19
manafov_mehman commentedas there is no way to know if the translated string is in a node or not so you don't have any other options besides clearing the whole cache. Clearing cache is a time consuming and resource intensive each time a string is translated, so i think this is a desired behavior
Comment #20
manafov_mehman commentedComment #22
kostask commentedI had the same problem and clearing the Plugin-cache translated the field label.
Comment #26
larowlanCan we try clearing just the plugin cache?
Comment #27
yogeshmpawarTrying to clear plugin cache.
Comment #29
yogeshmpawarUpdated patch will resolve test failures.
Comment #32
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
Did not test
Moving to NW for test cases
Comment #33
fathima.asmat commented#29 doesn't work for me when translating taxonomy or node field labels. Technically clearing
entity_field.managercache should do the trick, however it does not unfortunately. Only the combination of clearing render, page cache along with the entity field manager work for me. Though I don't like this workaround, I'm using this as an interim solution for my projects until a proper fix is released.I have got these in
config_translation/src/Form/ConfigTranslationFormBase.php:Attached a patch if anyone wants to use the workaround for now.
Comment #34
fathima.asmat commentedComment #35
smustgrave commentedWas previously tagged for tests which still needs to happen. Not ready for review.
Comment #36
ayush.khare commentedFixed CCF in #33.
Comment #37
mohit_aghera commentedI got the slightly different results with the test case.
Cache was not invalidated with the services used in #33 or #36
I was able to make it work using following cache invalidation
I feel that invalidating the cache that way isn't a good idea.
Probably we should emit the cache tags related to field_instance config whenever title is visible and invalidate those cache tags.
I tried to do that, however, this wasn't working as expected. Tried using following approach:
- Added a dummy cache tag called "field_config" in
view()method ofFormatterBase.phpclass.$elements['#cache']['tags'][] = 'field_config';- Later in
ConfigTranslationFormBase.php'ssubmit()callback, I tried to invalidate cache tag along with discovery cache. See following snippet.Since this isn't a generic approach, I haven't added that in the patch. Because all the configurations are not field config etc.
Probably someone more experienced with configuration api/translation API can chime in and throw other ideas.
Meanwhile, I've added a test-only patch so others can validate that when we do further work.
This is failing for me on local.
Keeping this in needs work since the discussion related to approach is required.
Comment #39
ressaI ran into this, and was puzzled that only field labels from one language were getting used in rendered html in Solr. Turning on development mode with
drush theme:dev on(new in Drush 13.6) which disables all caching (Drupal+Twig) and refreshing fixed it, and the correct, translated labels were shown, both under the content type, and in the rendered html in Solr.I would say this is a pretty big deal, so maybe raising the Status level a notch could be considered?
PS. Something else I noticed: If you enter a new value in a Label field and press Enter, it does not save the form, even though there seems to be a page reload, and the value stays the new one. But if you revisit the form, the original value is still there. Perhaps there's an issue for this already, or we need to create one?