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.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bvoynick created an issue. See original summary.

bvoynick’s picture

Status: Active » Needs review
florianmuellerch’s picture

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?

bvoynick’s picture

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.

  • florianmuellerch committed 21d1abae on 8.x-1.x
    Issue #3435929 by bvoynick: Add CacheableDependencyInterface and related...
florianmuellerch’s picture

Status: Needs review » Fixed

Thanks for the explanation and also the good work!
I couldn't merge the MR because of a Gitlab error, therefore I commited your changes manually.
Thank you very much!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.