Problem/Motivation

The "Sitewide Alert" block return an empty array if no alert exists.
But it should at list return a cache invalidation in order to render the sitewide_alert/init library and call the alerts endpoint.
In this way when a new alert is created from the back office the cache is invalidated and the necessary renderable array with the library is rendered from the block.

Steps to reproduce

- Enable the module
- Go to the front page for example in anonymous context
- From the back office add any new alert
- Go back to the front page in anonymous context, the alert is not shown

Proposed resolution

From the SitewideAlertRenderer.php return an entity list cache tag :

if (!$this->sitewideAlertManager->activeSitewideAlerts()) {
      return [
        '#cache' => ['tags' => ['sitewide_alert_list']],
      ];
    }

Or add the same cache tag in the dedicated block SitewideAlertBlock.php :

  /**
   * {@inheritdoc}
   */
  public function getCacheTags(): array {
    return Cache::mergeTags(parent::getCacheTags(), [
      'sitewide_alert_list',
    ]);
  }
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

guenole created an issue. See original summary.

jmaxant made their first commit to this issue’s fork.

jmaxant’s picture

Assigned: Unassigned » jmaxant

jmaxant’s picture

Assigned: jmaxant » Unassigned
Status: Active » Needs review

Hi there,

I just pushed a fix and created a MR with said changes (basically what @guenole suggested).

smustgrave’s picture

Status: Needs review » Needs work

Left a comment on the MR, wonder how hard it would be to add a test case for this too.

  • smustgrave committed 0e8f1161 on 3.0.x authored by jmaxant
    Issue #3506754 - ensure cache metadata are applied to avoid cache issue...
smustgrave’s picture

Status: Needs work » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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

loopy1492’s picture

This seems to have worked for us, however it doesn't seem to be in the most recent release. This seems to be a pretty important issue and warrants a new release. We have, for now, patched it.