Translating field labels does not invalidate cache

Created on 1 November 2016, about 8 years ago
Updated 21 April 2023, over 1 year ago

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 8 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.

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 8 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.

๐Ÿ› Bug report
Status

Needs work

Version

10.1 โœจ

Component
Localeย  โ†’

Last updated about 18 hours ago

Created by

๐Ÿ‡ง๐Ÿ‡ชBelgium mpp

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    This 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

  • fathima.asmat London, UK

    #29 doesn't work for me when translating taxonomy or node field labels. Technically clearing entity_field.manager cache 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:

    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    \Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
    \Drupal::service('cache.render')->invalidateAll();
    \Drupal::service('cache.dynamic_page_cache')->invalidateAll();
    

    Attached a patch if anyone wants to use the workaround for now.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Was previously tagged for tests which still needs to happen. Not ready for review.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia ayush.khare

    Fixed CCF in #33.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia mohit_aghera Rajkot

    I 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

            \Drupal::service('cache.dynamic_page_cache')->invalidateAll();
            \Drupal::service('cache.render')->invalidateAll();
            \Drupal::service('cache.discovery')->invalidateAll();
            \Drupal::service('cache.page')->invalidateAll();
    

    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 of FormatterBase.php class.
    $elements['#cache']['tags'][] = 'field_config';

    - Later in ConfigTranslationFormBase.php's submit() callback, I tried to invalidate cache tag along with discovery cache. See following snippet.

        Cache::invalidateTags(['field_config']);
        \Drupal::cache('discovery')->invalidate('entity_bundle_field_definitions:node:article:fr');
    

    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.

  • last update over 1 year ago
    29,277 pass, 4 fail
Production build 0.71.5 2024