Add CacheableDependencyInterface and related cache metadata to TranslatedImmutableConfig object

Created on 25 March 2024, 8 months ago
Updated 28 March 2024, 8 months ago

Problem/Motivation

This module currently does not help with setting cache metadata. A developer can still get & add such data manually, but has to either hardcode the relevant strings, or make their own separate calls to the config factory.

Proposed resolution

Add CacheableDependencyInterface to TranslatedImmutableConfig, and set the necessary data in the helper service.

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bvoynick

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @bvoynick
  • Status changed to Needs review 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States bvoynick
  • πŸ‡¨πŸ‡­Switzerland florianmuellerch Aarau, Switzerland

    Hi @bvoynick

    Thank you very much for your inputs and efforts for the merge request! :-)

    I very much like the idea of handling cache properly. However I don't quite understand the use case in this particular example.

    Could you provide an example where this comes into play? At the moment, the config is read directly on call, and then held in local memory. How does cache metadata affect this process?

  • πŸ‡ΊπŸ‡ΈUnited States bvoynick

    The use case is downstream rendering or other things that may need cache metadata.

    For example, let's say I render something using text from a config. Using core's ImmutableConfig object I could write code like this:

    $config = \Drupal::config('my_config');
    $render_array = [
      '#plain_text' => $config->get('text_property'),
    ];
    CacheableMetadata::createFromObject($config)
      ->applyTo($render_array);
    

    But what if I want to use translated_config? Since TranslatedImmutableConfig doesn't implement the CacheableDependencyInterface, currently my code would have to look something like this:

    $translated_config = \Drupal::service('translated_config.helper')->getTranslatedConfig('my_config');
    $render_array = [
      '#plain_text' => $translated_config->get('text_property'),
      '#cache' => [
        'tags' => [
          'config:my_config',
        ],
        'contexts' => [
          'languages:language_interface',
        ],
      ],
    ];
    

    Which requires me to know the right metadata to hardcode.

    Or alternately, I could make my own separate call to \Drupal::config('my_config') as in the first example, using the TranslatedImmutableConfig object for values only and not metadata.

    But anyhow, that's the purpose of this feature request. To have the same cache-related "developer experience," when it comes to handling cache metadata, as with the core ImmutableConfig object, where one can simply send it to the right addCacheableDependency() or similar method.

Production build 0.71.5 2024