Uncategorized

What Are Asset Libraries?

An asset library is a bundle of CSS and/or JavaScript files that work together to provide a style and functionality for a specific component. They are frequently used to isolate the functionality and styling of a specific component, like the tabs displayed at the top of each node, into a reusable library. If you want to include CSS and/or JavaScript in your Drupal theme or module you’ll need to declare an asset library that tells Drupal about the existence, and location, of those files. And then attach that library to a page, or specific element, so that it gets loaded when needed.

In this tutorial we’ll:

  • Define what an asset library is.
  • Explain why asset libraries are used to include JavaScript and CSS files.
  • Look at some example asset library definitions.

By the end of this tutorial you should be able to define what asset libraries are, and when you’ll need to create one.

Define an Asset Library

New asset libraries can be defined by either modules or themes. In order to define a new asset library you need to create the requisite CSS and JavaScript files, and a new THEMENAME.libraries.yml, or MODULENAME.libraries.yml file that aggregates them together and provides metadata about the library itself and any dependencies.

In this tutorial we’ll:

  • Look at the structure of a *.libraries.yml file and demonstrate how to combine a couple of CSS and JS files together into an asset library that can be used in a theme or a module
  • Look at how one asset library can declare that it is dependent on another in order to ensure the assets from the dependency are loaded as well

By the end of this tutorial you should know how to define a new asset library in either a module or a theme.

Attach an Asset Library

Once you’ve defined an asset library you’ll need to tell Drupal when you want to add the CSS and JavaScript that it includes to the page. Ideally you’ll do so in a way that allows Drupal to only add the corresponding assets on pages where they are needed.

You can attach a library to all pages, a subset of pages, or to elements in a render array. This allows you to have some assets that are global, and others that get loaded on an as-needed basis. To attach a library you’ll need to know both its name and prefix, and then use one of the techniques outlined below to let Drupal know when to include it.

In this tutorial, we’ll look at attaching asset libraries:

  • Globally, via your THEMENAME.info.yml file
  • Conditionally, via a preprocess function using the #attached render array property
  • Inside of a Twig template file

By the end of this tutorial you should be able to attach asset libraries in various different ways depending on your use case.

Exercise: Add an Asset Library

You can accomplish a lot without needing to change any markup by adding CSS and JavaScript files to your theme. In this exercise you’ll create 2 asset libraries and add them to your theme. Specifically, we want to pull in the CSS and JavaScript from the popular Bootstrap framework so that we can make use of its layout utility classes later on. We’ll also add a custom CSS file that contains global styles for our site, like setting the page background color.

Read Comfy 3501
Read Comfy 3502
Read Comfy 3503
Read Comfy 3504
Read Comfy 3505
Read Comfy 3506
Read Comfy 3507
Read Comfy 3508
Read Comfy 3509
Read Comfy 3510
Read Comfy 3511
Read Comfy 3512
Read Comfy 3513
Read Comfy 3514
Read Comfy 3515
Read Comfy 3516
Read Comfy 3517
Read Comfy 3518
Read Comfy 3519
Read Comfy 3520
Read Comfy 3521
Read Comfy 3522
Read Comfy 3523
Read Comfy 3524
Read Comfy 3525
Read Comfy 3526
Read Comfy 3527
Read Comfy 3528
Read Comfy 3529
Read Comfy 3530
Read Comfy 3531
Read Comfy 3532
Read Comfy 3533
Read Comfy 3534
Read Comfy 3535
Read Comfy 3536
Read Comfy 3537
Read Comfy 3538
Read Comfy 3539
Read Comfy 3540
Read Comfy 3541
Read Comfy 3542
Read Comfy 3543
Read Comfy 3544
Read Comfy 3545
Read Comfy 3546
Read Comfy 3547
Read Comfy 3548
Read Comfy 3549
Read Comfy 3550
Read Comfy 3551
Read Comfy 3552
Read Comfy 3553
Read Comfy 3554
Read Comfy 3555
Read Comfy 3556
Read Comfy 3557
Read Comfy 3558
Read Comfy 3559
Read Comfy 3560
Read Comfy 3561
Read Comfy 3562
Read Comfy 3563
Read Comfy 3564
Read Comfy 3565
Read Comfy 3566
Read Comfy 3567
Read Comfy 3568
Read Comfy 3569
Read Comfy 3570
Read Comfy 3571
Read Comfy 3572
Read Comfy 3573
Read Comfy 3574
Read Comfy 3575
Read Comfy 3576
Read Comfy 3577
Read Comfy 3578
Read Comfy 3579
Read Comfy 3580
Read Comfy 3581
Read Comfy 3582
Read Comfy 3583
Read Comfy 3584
Read Comfy 3585
Read Comfy 3586
Read Comfy 3587
Read Comfy 3588
Read Comfy 3589
Read Comfy 3590
Read Comfy 3591
Read Comfy 3592
Read Comfy 3593
Read Comfy 3594
Read Comfy 3595
Read Comfy 3596
Read Comfy 3597
Read Comfy 3598
Read Comfy 3599
Read Comfy 3600
Read Comfy 3601
Read Comfy 3602
Read Comfy 3603
Read Comfy 3604
Read Comfy 3605
Read Comfy 3606
Read Comfy 3607
Read Comfy 3608
Read Comfy 3609
Read Comfy 3610
Read Comfy 3611
Read Comfy 3612
Read Comfy 3613
Read Comfy 3614
Read Comfy 3615
Read Comfy 3616
Read Comfy 3617
Read Comfy 3618
Read Comfy 3619
Read Comfy 3620
Read Comfy 3621
Read Comfy 3622
Read Comfy 3623
Read Comfy 3624
Read Comfy 3625
Read Comfy 3626
Read Comfy 3627
Read Comfy 3628
Read Comfy 3629
Read Comfy 3630
Read Comfy 3631
Read Comfy 3632
Read Comfy 3633
Read Comfy 3634
Read Comfy 3635
Read Comfy 3636
Read Comfy 3637
Read Comfy 3638
Read Comfy 3639
Read Comfy 3640
Read Comfy 3641
Read Comfy 3642
Read Comfy 3643
Read Comfy 3644
Read Comfy 3645
Read Comfy 3646
Read Comfy 3647
Read Comfy 3648
Read Comfy 3649
Read Comfy 3650
Read Comfy 3651
Read Comfy 3652
Read Comfy 3653
Read Comfy 3654
Read Comfy 3655
Read Comfy 3656
Read Comfy 3657
Read Comfy 3658
Read Comfy 3659
Read Comfy 3660
Read Comfy 3661
Read Comfy 3662
Read Comfy 3663
Read Comfy 3664
Read Comfy 3665
Read Comfy 3666
Read Comfy 3667
Read Comfy 3668
Read Comfy 3669
Read Comfy 3670
Read Comfy 3671
Read Comfy 3672
Read Comfy 3673
Read Comfy 3674
Read Comfy 3675
Read Comfy 3676
Read Comfy 3677
Read Comfy 3678
Read Comfy 3679
Read Comfy 3680
Read Comfy 3681
Read Comfy 3682
Read Comfy 3683
Read Comfy 3684
Read Comfy 3685
Read Comfy 3686
Read Comfy 3687
Read Comfy 3688
Read Comfy 3689
Read Comfy 3690
Read Comfy 3691
Read Comfy 3692
Read Comfy 3693
Read Comfy 3694
Read Comfy 3695
Read Comfy 3696
Read Comfy 3697
Read Comfy 3698
Read Comfy 3699
Read Comfy 3700
Read Comfy 3701
Read Comfy 3702
Read Comfy 3703
Read Comfy 3704
Read Comfy 3705
Read Comfy 3706
Read Comfy 3707
Read Comfy 3708
Read Comfy 3709
Read Comfy 3710
Read Comfy 3711
Read Comfy 3712
Read Comfy 3713
Read Comfy 3714
Read Comfy 3715
Read Comfy 3716
Read Comfy 3717
Read Comfy 3718
Read Comfy 3719
Read Comfy 3720
Read Comfy 3721
Read Comfy 3722
Read Comfy 3723
Read Comfy 3724
Read Comfy 3725
Read Comfy 3726
Read Comfy 3727
Read Comfy 3728
Read Comfy 3729
Read Comfy 3730
Read Comfy 3731
Read Comfy 3732
Read Comfy 3733
Read Comfy 3734
Read Comfy 3735
Read Comfy 3736
Read Comfy 3737
Read Comfy 3738
Read Comfy 3739
Read Comfy 3740
Read Comfy 3741
Read Comfy 3742
Read Comfy 3743
Read Comfy 3744
Read Comfy 3745
Read Comfy 3746
Read Comfy 3747
Read Comfy 3748
Read Comfy 3749
Read Comfy 3750
Read Comfy 3751
Read Comfy 3752
Read Comfy 3753
Read Comfy 3754
Read Comfy 3755
Read Comfy 3756
Read Comfy 3757
Read Comfy 3758
Read Comfy 3759
Read Comfy 3760
Read Comfy 3761
Read Comfy 3762
Read Comfy 3763
Read Comfy 3764
Read Comfy 3765
Read Comfy 3766
Read Comfy 3767
Read Comfy 3768
Read Comfy 3769
Read Comfy 3770
Read Comfy 3771
Read Comfy 3772
Read Comfy 3773
Read Comfy 3774
Read Comfy 3775
Read Comfy 3776
Read Comfy 3777
Read Comfy 3778
Read Comfy 3779
Read Comfy 3780
Read Comfy 3781
Read Comfy 3782
Read Comfy 3783
Read Comfy 3784
Read Comfy 3785
Read Comfy 3786
Read Comfy 3787
Read Comfy 3788
Read Comfy 3789
Read Comfy 3790
Read Comfy 3791
Read Comfy 3792
Read Comfy 3793
Read Comfy 3794
Read Comfy 3795
Read Comfy 3796
Read Comfy 3797
Read Comfy 3798
Read Comfy 3799
Read Comfy 3800
Read Comfy 3801
Read Comfy 3802
Read Comfy 3803
Read Comfy 3804
Read Comfy 3805
Read Comfy 3806
Read Comfy 3807
Read Comfy 3808
Read Comfy 3809
Read Comfy 3810
Read Comfy 3811
Read Comfy 3812
Read Comfy 3813
Read Comfy 3814
Read Comfy 3815
Read Comfy 3816
Read Comfy 3817
Read Comfy 3818
Read Comfy 3819
Read Comfy 3820
Read Comfy 3821
Read Comfy 3822
Read Comfy 3823
Read Comfy 3824
Read Comfy 3825
Read Comfy 3826
Read Comfy 3827
Read Comfy 3828
Read Comfy 3829
Read Comfy 3830
Read Comfy 3831
Read Comfy 3832
Read Comfy 3833
Read Comfy 3834
Read Comfy 3835
Read Comfy 3836
Read Comfy 3837
Read Comfy 3838
Read Comfy 3839
Read Comfy 3840
Read Comfy 3841
Read Comfy 3842
Read Comfy 3843
Read Comfy 3844
Read Comfy 3845
Read Comfy 3846
Read Comfy 3847
Read Comfy 3848
Read Comfy 3849
Read Comfy 3850
Read Comfy 3851
Read Comfy 3852
Read Comfy 3853
Read Comfy 3854
Read Comfy 3855
Read Comfy 3856
Read Comfy 3857
Read Comfy 3858
Read Comfy 3859
Read Comfy 3860
Read Comfy 3861
Read Comfy 3862
Read Comfy 3863
Read Comfy 3864
Read Comfy 3865
Read Comfy 3866
Read Comfy 3867
Read Comfy 3868
Read Comfy 3869
Read Comfy 3870
Read Comfy 3871
Read Comfy 3872
Read Comfy 3873
Read Comfy 3874
Read Comfy 3875
Read Comfy 3876
Read Comfy 3877
Read Comfy 3878
Read Comfy 3879
Read Comfy 3880
Read Comfy 3881
Read Comfy 3882
Read Comfy 3883
Read Comfy 3884
Read Comfy 3885
Read Comfy 3886
Read Comfy 3887
Read Comfy 3888
Read Comfy 3889
Read Comfy 3890
Read Comfy 3891
Read Comfy 3892
Read Comfy 3893
Read Comfy 3894
Read Comfy 3895
Read Comfy 3896
Read Comfy 3897
Read Comfy 3898
Read Comfy 3899
Read Comfy 3900
Read Comfy 3901
Read Comfy 3902
Read Comfy 3903
Read Comfy 3904
Read Comfy 3905
Read Comfy 3906
Read Comfy 3907
Read Comfy 3908
Read Comfy 3909
Read Comfy 3910
Read Comfy 3911
Read Comfy 3912
Read Comfy 3913
Read Comfy 3914
Read Comfy 3915
Read Comfy 3916
Read Comfy 3917
Read Comfy 3918
Read Comfy 3919
Read Comfy 3920
Read Comfy 3921
Read Comfy 3922
Read Comfy 3923
Read Comfy 3924
Read Comfy 3925
Read Comfy 3926
Read Comfy 3927
Read Comfy 3928
Read Comfy 3929
Read Comfy 3930
Read Comfy 3931
Read Comfy 3932
Read Comfy 3933
Read Comfy 3934
Read Comfy 3935
Read Comfy 3936
Read Comfy 3937
Read Comfy 3938
Read Comfy 3939
Read Comfy 3940
Read Comfy 3941
Read Comfy 3942
Read Comfy 3943
Read Comfy 3944
Read Comfy 3945
Read Comfy 3946
Read Comfy 3947
Read Comfy 3948
Read Comfy 3949
Read Comfy 3950
Read Comfy 3951
Read Comfy 3952
Read Comfy 3953
Read Comfy 3954
Read Comfy 3955
Read Comfy 3956
Read Comfy 3957
Read Comfy 3958
Read Comfy 3959
Read Comfy 3960
Read Comfy 3961
Read Comfy 3962
Read Comfy 3963
Read Comfy 3964
Read Comfy 3965
Read Comfy 3966
Read Comfy 3967
Read Comfy 3968
Read Comfy 3969
Read Comfy 3970
Read Comfy 3971
Read Comfy 3972
Read Comfy 3973
Read Comfy 3974
Read Comfy 3975
Read Comfy 3976
Read Comfy 3977
Read Comfy 3978
Read Comfy 3979
Read Comfy 3980
Read Comfy 3981
Read Comfy 3982
Read Comfy 3983
Read Comfy 3984
Read Comfy 3985
Read Comfy 3986
Read Comfy 3987
Read Comfy 3988
Read Comfy 3989
Read Comfy 3990
Read Comfy 3991
Read Comfy 3992
Read Comfy 3993
Read Comfy 3994
Read Comfy 3995
Read Comfy 3996
Read Comfy 3997
Read Comfy 3998
Read Comfy 3999
Read Comfy 4000

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button