Skip to content

Theming

Themes can show announcements in their own markup, such as their alert component, by overriding the module's two templates and extending its library.

Templates

platform-announcements.html.twig

The list of announcements, for the Announcements page, the toolbar dialog and the block.

Variable Contents
items The announcements, each rendered with platform-announcement.html.twig. Empty when there are none.
feed_link The URL of the page that lists every announcement, or empty.

The default template wraps each item in an element with the platform-announcements__item class. Dismissing an announcement hides that element, or the announcement itself when there's none, so keep the class if a wrapper should disappear with the announcement.

platform-announcement.html.twig

One announcement.

Variable Contents
attributes HTML attributes, including data-platform-announcement-id and data-platform-announcement-modified.
title The title, as plain text.
content The description, as filtered HTML.
url The link to the full announcement.
published When it was published, as a timestamp, or 0.
featured TRUE if the announcement is featured.
announcement The Drupal\platform_announcements\Announcement object.

Print attributes on the outermost element: the Dismiss button finds announcements by their data-platform-announcement-id attribute. The button reads the announcement's title from the element with the platform-announcement__title class, for its accessible label.

For example, a theme with an alert component could use:

<div{{ attributes.addClass('alert', featured ? 'alert--warning' : 'alert--info') }}>
  <h2 class="alert__title platform-announcement__title"><a href="{{ url }}">{{ title }}</a></h2>
  {{ content }}
</div>

The Dismiss button

The platform_announcements/announcements library adds a Dismiss button to the end of each announcement, with the platform-announcement__dismiss class. Dismissals are kept in the browser's localStorage (Drupal.platformAnnouncements.dismissed), keyed by announcement ID with the updated date, so an announcement comes back when it's updated.

To change the button's markup, override its theme function in the theme's JavaScript:

Drupal.theme.platformAnnouncementDismiss = (title) =>
  `<button type="button" class="alert__close platform-announcement__dismiss" aria-label="${Drupal.checkPlain(
    Drupal.t('Dismiss @title', { '@title': title }),
  )}"></button>`;

To style it, add the theme's CSS to the module's library in the theme's .info.yml:

libraries-extend:
  platform_announcements/announcements:
    - my_theme/platform-announcements