Problem/Motivation

We have a custom block that just returns a '#type' => 'link'.
The render array also contains a #cache key and we noticed the cache context is ignored and does not bubble.

It seems the Link render element does not handle #cache correctly and loses it during render.

Steps to reproduce

Here the cache context is lost:

  public function build(): array {
    return [
      '#title' => 'foo',
      '#type' => 'link',
      '#url' => Url::fromUri('http://example.com'),
      '#cache' => ['contexts' => ['timezone']]
    ];
  }

But this works:

  public function build(): array {
    return [
      '#markup' => 'foo',
      '#cache' => ['contexts' => ['timezone']]
    ];
  }

This also works:

  public function build(): array {
    return [
      [
        '#title' => 'foo',
        '#type' => 'link',
        '#url' => Url::fromUri('http://example.com'),
      ],
      '#cache' => ['contexts' => ['timezone']],
    ];
  }

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3624822

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

prudloff created an issue.

prudloff’s picture

It seems I can't reproduce this on a clean install so it might be caused by a specific module.