Problem/Motivation

Attaching Custom JavaScript to HTML added via a Twig Template override.

Steps to reproduce

Copy sitewide-alert.html.twig from module's template directory to a custom theme.
Add HTML to create a button.
While not necessary to demonstrate my problem, my motivation is to have two HTML divs for each alert in the template override. One is an alert summary, the other is the full alert details. The desire is to show a summary and allow a user to expand/collapse the alert. This is done with a JavaScript function to show/hide each div when a button is clicked.

Problem 1 Since the alerts are loaded to a page by the module's init.js, I was unable to find an efficient way to add my event listener to my custom button(s). I ended up using a setTimeout function. This could be my own ignorance but setTimeout has been the only way I've been able to ensure the custom button(s) are present when I try to create an event listener.

Problem 2 If the option in the module settings for periodic refreshing the alerts is enabled, I need to recreate my event listener on the custom button(s). I tried using another setTimeout based on the drupalSettings.sitewideAlert.refreshInterval value but this hasn't really solved this problem.

Proposed resolution

I can achieve exactly the desired behaviors I want if I edit the module's init.js but obviously don't want to do that. I have a feeling there is a better, saner, Drupal-way of attaching my custom JavaScript without hacking the modules init.js code. Any suggestions are appreciated.

Remaining tasks

unsure?

User interface changes

none

API changes

none?

Data model changes

none

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

sadman created an issue. See original summary.

chrissnyder’s picture

Status: Active » Needs work
chrissnyder’s picture

Category: Support request » Feature request
Status: Needs work » Active
chrissnyder’s picture

Version: 2.0.0-alpha5 » 2.x-dev

chrissnyder’s picture

  • ChrisSnyder committed fd61b31 on 2.x
    Issue #3267185 by ChrisSnyder, sadman: Custom JS Event Listener
    
chrissnyder’s picture

Status: Active » Fixed
chrissnyder’s picture

The following JS events have been added:

  • sitewide-alert-rendered
  • sitewide-alert-dismissed
  • sitewide-alert-removed

Status: Fixed » Closed (fixed)

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