Skip to content

Platform announcements block

The Announcements page and toolbar tab only show announcements to people who open them. The Platform announcements block (platform_announcements) puts them in front of people on every page.

Placing the block

  1. Go to Administration » Structure » Block layout.
  2. Place the Platform announcements block in a region near the top of the page, such as one for site alerts or above the content.
  3. Grant the "Access platform announcements" permission to the roles that should see it. The block doesn't show to anyone else, so there's no need to set the block's own role visibility.

On platforms that share configuration, export the placed block with the rest of the configuration, so every site shows it in the same place.

Configuration

Besides the standard block settings, such as its title, the block has Hide the block when there are no announcements, checked by default. Uncheck it to show "No announcements available" instead, e.g. in a sidebar where an empty space would look broken. The block always shows nothing when the feed can't be loaded: visitors can't do anything about it, and the error is logged. The Announcements page shows the error instead.

What the block shows comes from the module's settings, at Administration » Configuration » Web services » Platform announcements, which also apply to the Announcements page and the toolbar's dialog:

  • Feed URL: where the announcements come from.
  • Announcements page URL: the "View all announcements" link, left out when empty.
  • Number of announcements: the most to show, featured ones first.

How long the fetched feed is kept, max_age (86400 seconds, a day, by default), isn't on the form. Set it in settings.php:

$config['platform_announcements.settings']['max_age'] = 3600;

Caching

The block's render array carries:

  • the config:platform_announcements.settings cache tag, so changing the settings, including the feed URL, shows the new announcements straight away;
  • a max-age of max_age, so the block is rebuilt once the fetched feed expires;
  • the user.permissions cache context, from the block's access check.

When the feed can't be loaded, the empty block is cached for at most five minutes (PlatformAnnouncements::ERROR_MAX_AGE), so the announcements come back soon after the feed recovers.

The fetched feed itself is kept in the platform_announcements key-value store for max_age seconds, and fetched again on cron once it expires, so page requests rarely wait for it.

Drupal's Internal Page Cache, which serves pages to anonymous visitors, ignores max-age. If anonymous users have the "Access platform announcements" permission, cached pages keep showing the old announcements until the page cache is cleared or the settings change.

Theming

The block renders the same list as the Announcements page, with the module's theme hooks:

  • platform_announcements (platform-announcements.html.twig): the list, with the "View all announcements" link, or "No announcements available" when there are none;
  • platform_announcement (platform-announcement.html.twig): each announcement.

It attaches the platform_announcements/announcements library, which adds the Dismiss buttons and the module's CSS. Themes override the templates and extend the library to show the announcements as alerts: see Theming.

Dismissing announcements

Each announcement in the block has a Dismiss button. A dismissed announcement is hidden in that browser, in the block, on the Announcements page and in the toolbar's dialog, until it's updated on the source site. Dismissals are kept in the browser's localStorage, so they don't follow people to another browser or device.