You are here

function bugherd_page_attachments in BugHerd 8

Implements hook_attachments().

We must inline the JS so BugHerd can detect correct installation of the script.

File

./bugherd.module, line 33
BugHerd module functions.

Code

function bugherd_page_attachments(array &$attachments) {
  $config = \Drupal::config('bugherd.settings');
  $project_key = $config
    ->get('bugherd_project_key');
  if ($project_key && \Drupal::currentUser()
    ->hasPermission('access bugherd')) {
    $disable_on_admin = $config
      ->get('bugherd_disable_on_admin');
    $position = $config
      ->get('bugherd_widget_position');

    // Add to the head.
    if (!$disable_on_admin || !\Drupal::service('router.admin_context')
      ->isAdminRoute()) {
      $escaper = new Escaper();
      $attachments['#attached']['html_head'][] = [
        [
          '#type' => 'html_tag',
          '#tag' => 'script',
          '#value' => "\n            var BugHerdConfig = {\"feedback\":{\"tab_position\":\"" . $position . "\"}};\n\n            (function (d, t) {\n              var bh = d.createElement(t), s = d.getElementsByTagName(t)[0];\n              bh.type = 'text/javascript';\n              bh.src = 'https://www.bugherd.com/sidebarv2.js?apikey=" . $escaper
            ->escapeJs($project_key) . "';\n              s.parentNode.insertBefore(bh, s);\n            })(document, 'script');\n          ",
          '#cache' => [
            'contexts' => [
              'route',
              'user.permissions',
            ],
            'tags' => [
              'bugherd',
            ],
          ],
        ],
        'bugherd_embed',
      ];
    }
  }
}