Add a custom script to subtheme

Created on 3 December 2019, over 4 years ago
Updated 29 March 2023, about 1 year ago

I just want to know if this is the correct way to handle this with AT.

1.) Put custom script in /scripts
2.) Add definitions to themename.libraries.yaml
Example, for loading it global, on every page

global-scripts:
version: 8.x-1.0
js:
scripts/custom.js: {}
dependencies:
- core/jquery

3.)Add this

libraries:
- dist_pd_/global-scripts

To themename.info.yml

πŸ’¬ Support request
Status

Active

Version

3.1

Component

Documentation

Created by

πŸ‡©πŸ‡ͺGermany marcoka

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • @harish1688 opened merge request.
  • Hi @drupalfan2 β†’
    step 1 - In your sub-theme folder in custom_theme.libraries.yml file paste below code, this create a link of main.js: in scripts folder JS file .

    #global custom
    global:
      version: 8.x-3.1
      js:
        scripts/main.js: {}

    step 2 - In your sub-theme folder in custom_theme.info.yml file paste below code, that will connect global libraries with your theme.

    # custom library 
    libraries:
      - custom_theme/global

    step 3 - In your sub-theme folder in scripts folder create a file with name main.js.
    you may testing by pasting the code.

    /**
     * @file main.js 
     */
    
    // Attach all behaviors.
    (function($, Drupal, once, drupalSettings) {
      "use strict";
      Drupal.behaviors.global = {
        attach: function(context, settings) {
          // Attach first behaviors - you may bind the behavior on any element like 'html/body'
          once('myCustomBehavior', 'html', context).forEach(function(element) {
            console.log('first behavior ready');
          });
          // Attach second behaviors - you may bind the behavior on any element like 'html/body'
          once('myCustomBehavior_2', 'body', context).forEach(function(element) {
            console.log('second behavior ready');
          });
        }
      };
    })(jQuery, Drupal, once, drupalSettings);

    Note :
    change custom_theme name with sub-theme name.

Production build 0.69.0 2024