4

Now when we are almost there using purely YML files to include JavaScript, is it possible to include javascripts everywhere except for in the Drupal administration? Beta 4 does not allow the #attached property anymore, so this solution would be best suited in a YML setup.

1 Answer 1

5

Sure. Drupal 8 still allows #attached, the difference is just that you specify a library instead of a specific javascript file, but that is irrelevant for your question.

You can implement hook_page_attachments() and ask the route context if this is an admin route.

/**
 * Implements hook_page_attachments().
 */
function yourmodule_page_attachments(&$page) {
  // This returns TRUE for admin paths.
  if (!\Drupal::service('router.admin_context')->isAdminRoute()) {
    $page['#attached']['library'][] = 'yourmodule/yourlibrary';
  }
}
1
  • Did the trick, cheers!
    – Undrium
    Commented Jan 20, 2015 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.