Uncategorized

CREATING LINKS WITHIN TWIG TEMPLATES USING PATH() AND URL()

Drupal 8 comes packed with loads of great new features, APIs and developer tools. There are sweeping changes aplenty. Not the least of which is a brand new templating system called Twig. Twig is the bee’s knees and a welcome improvement over the much maligned PHPTemplate. However, many front-end developers who’ve grown accustomed to the templating ways of old might feel a bit lost as they enter the strange, new world of Drupal 8: Twig and render arrays. Furthermore, the Drupal community is still learning, documentation is still being written, etc.

As we approach the completion of our first official Drupal 8 project here at Chromatic, I thought it would be helpful to start sharing a bit of what I’ve learned along the way, starting with some examples on linking from within Twig templates.

If you want to just see how the hell you link to things from within Twig templates, skip these next bits on context.

SOME CONTEXT

In Drupal 7 and versions prior, we used paths to define destinations for our content, APIs, etc. In Drupal 8, these are abstracted into routes. Where in Drupal 7 you would define a custom page via hook_menu(), like so:

<?php
function chromatic_contact_menu() {
  $items['contact'] = array(
    'title' => 'Chromatic Contact',
    'page callback' => 'contact_page',
    'access arguments' => array('access content'),
    'type' => MENU_SUGGESTED_ITEM,
  );

  return $items;
}
?>

In Drupal 8, you define this in a special routing file that follows this naming convention: module_name.routing.yml. Here’s a similar example, where chromatic_contact_contact is our route name and contact is the internal path where it can be accessed.

chromatic_contact_contact:
  path: 'contact'
  defaults:
    _form: '\Drupal\chromatic_contact\Form\ChromaticContactForm'
    _title: 'Contact Us!'
  requirements:
    _permission: 'access content'

Here’s how Drupal 8 sets up the route for node type creation:

node.type_add:
  path: '/admin/structure/types/add'
  defaults:
    _entity_form: 'node_type.add'
    _title: 'Add content type'
  requirements:
    _permission: 'administer content types'

GOT IT. ROUTES ARE THE NEW HOTNESS IN D8. NOW WHY DOES THIS MATTER TO ME? I’M A FRONT-ENDER.

Glad you asked. Routes matter because if you want to generate URLs to custom pages from your templates and you want to do it properly, you need to understand routes. The old methods of using paths are “dead in Drupal 8”.

The url() and path() functions are how we handle this type of linking in D8. These functions don’t expect an internal path like you’re probably familiar with in prior versions of Drupal. Instead, they expect proper system routes. The type of thing you now see in module_name.routing.yml.

SO WHAT’S THE DIFFERENCE?

  • path() – Generates a [relative] URL path given a route name and parameters.
  • url() – Generates an absolute URL given a route name and parameters.

JOIN OUR TEAM!

Are you an talented developer or project manager looking for your next challenge? We’re hiring and we’d love to hear from you!

Check out the current openings on our Careers page.

EXAMPLES

// Link to the default frontpage content listing view:
<a href="{{ path('view.frontpage') }}">{{ 'View all content'|t }}</a>

// Link to a specific node page:
<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">{{ 'Read more'|t }}</a>

// Link to a specific user profile page:
<a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a>

// Link to a view, and throw in some additional query string parameters:
<a href="{{ path('view.articles.page_1', {'page': 2}) }}">{{ 'Go to page 2'|t }}</a>

// Link to a view and pass in some arguments to the view:
<a href="{{ path('view.recent_articles_by_author.page_1', {'arg_0': user.field_display_name.value|drupal_escape }) }}">{{ 'See all the articles written by'|t }} {{ user.field_display_name.value }}</a>

The source code for the path() and url() Twig extensions can be found here within Drupal 8 core: /core/lib/Drupal/Core/Template/TwigExtension.php.

 

 

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