Score:1

Add simple config object as a cacheable dependency?

in flag

I am building a custom breadcrumb that has some admin settings from a module in Drupal 9. When that settings form is saved, I want any breadcrumb cache that was built with this builder to be invalidated.

I tried passing my config object into the builder as such:

$breadcrumb->addCacheableDependency($this->config);

Only when I save the form, the breadcrumb serves the cached version until I clear it. Then it shows the right breadcrumb. My constructor has:

  public function __construct(
    AdminContext $admin_context,
    AliasManagerInterface $alias_manager,
    EntityTypeManagerInterface $entity_type_manager,
    ConfigFactoryInterface $config_factory
  ) {
    $this->adminContext = $admin_context;
    $this->aliasManager = $alias_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->config = $config_factory->get('mymodule.settings');
  }
Score:0
cn flag

To answer the question. Yes, this is the correct way.

You can check whether the cache tag config:* is passed to the block variables:

function mymodule_preprocess_block__system_breadcrumb_block(&$variables) {
  var_dump($variables['content']['#cache']);
}

Then it can only be a problem with the template not rendering the entire content of the content variable.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.