Uncategorized

Adding CSS Classes to Blocks in Drupal 8

block.html.twig

<div class="block block--fancy">
  {{ title_prefix }}
  {% if label %}
    <h2  class="block__title block__title--fancy">{{ label }}</h2>
  {% endif %}
  {{ title_suffix }}
  {% block content %}
    {{ content }}
  {% endblock %}
</div>

This works in a lot of cases, but may not be flexible enough. The second approach utilizes the new attributes object – the successor to Drupal 7’s attributes array. The attribute object encapsulates all the attributes for a given tag. It also includes a number of methods which enable you to add, remove and alter those attributes before printing. For now we’ll just focus on the attributes.addClass() method. You can learn more about available methods in the official Drupal 8 documentation.

block.html.twig

{%
  set classes = [
    'block',
    'block--fancy'
  ]
%}
 
{%
  set title_classes = [
    'block__title',
    'block__title--fancy'
  ]
%}
 
<div{{ attributes.addClass(classes) }}>
  {{ title_prefix }}
  {% if label %}
    <h2{{ title_attributes.addClass(title_classes) }}>{{ label }}</h2>
  {% endif %}
  {{ title_suffix }}
  {% block content %}
    {{ content }}
  {% endblock %}
</div>

Alternatively, we can add our class directly to the class attribute with the existing classes from the attribute.class then print the remaining attributes. To prevent the class attribute from printing twice, we exclude it using the without Twig filter. Either way works.

block.html.twig

<div class="block--fancy {{ attributes.class }}"{{attributes|without('class') }}>
  {{ title_prefix }}
  {% if label %}
    <h2  class="block--fancy {{ title_attributes.class }}" {{title_attributes|without('class') }}>{{ label }}</h2>
  {% endif %}
  {{ title_suffix }}
  {% block content %}
    {{ content }}
  {% endblock %}
</div>

In any case, all our blocks on the site now look fancy as hell (assuming we’ve styled .block--fancy as such)

Template Suggestions

The above examples work. In reality if all our blocks look fancy, no blocks will look fancy. We need to apply this class only to our special blocks that truly deserve to be fancy. This introduces my second favorite DX improvement to Drupal 8 – hook_theme_suggestions_HOOK_alter.

If you wanted to make a custom template available for use to a certain block In Drupal 7, you had to do so in a preprocess function. Altering theme hook suggestions (the list of possible templates) in the Drupal 8 is delegated to its very own hook. The concept is pretty straight forward. Before Drupal renders an element, it looks at an array of possible template file names (a.k.a. suggestions) one-by-one. For each template file, it looks in the file system to see if that file exists in our theme, its base theme or core themes. Once it finds a match, it stops looking and renders the element using the matching template.

We’ll use this hook to add our new template file to the list of suggestions. In the case of blocks, the function we’ll define is hook_theme_suggestions_block_alter. It takes two arguments, the first is the array of suggestions which are passed by reference (by prefixing the parameter with a & so we can alter them directly. The second is the variables from our element that we can use to determine which templates we want to include.

Lets assume we renamed one of our templates above to block--fancy.html.twig and saved it to our theme. We then add the following function to my_theme.theme where “my_theme” is the name of our theme.

my_theme.theme

<?php
 
/**
 * Implements hook_theme_suggestions_HOOK_alter() for block templates.
 */
function my_theme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  $block_id = $variables['elements']['#id'];
 
  /* Uncomment the line below to see variables you can use to target a block */
  // print $block_id . '<br/>';
 
  /* Add classes based on the block id. */
  switch ($block_id) {
    /* Account Menu block */
    case 'account_menu':
      $suggestions[] = 'block__fancy';
      break;
  }
}

Now the account menu block on our site will use block--fancy.html.twig as we can see from the output of twig debug

This is just one example of the improvements in D8 theming. I’m really excited for the clarity that the new Twig templates bring to Drupal 8 and the simplicity of managing template suggestions through hook_theme_suggestions_HOOK_alter.

Code Drupal Drupal 8 Drupal Planet

 

 

 

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

Related Articles

Leave a Reply

Back to top button