Uncategorized

DIGGING INTO DRUPAL 8: CODE SNIPPETS FOR SITE BUILDERS

The more I work with Drupal 8, the more I realize how much has changed for developers in the Drupal community. While the transition to a modern, object-oriented system is what’s best for the longevity of the platform, it certainly doesn’t come without challenges. As someone who doesn’t come from an OOP background, I’ve found the transition difficult at times. In many cases, I know exactly what I want to do, just not how to do it the “Drupal 8 way”. On top of this, tutorials and blog posts on D8 are all over the map in terms of accuracy. Many posts written during D8’s development cycle are no longer applicable because of API changes, etc.

Below is a list of snippets that might be helpful to site builders or developers more familiar with D7 hooks and procedural. It might also be useful to OOP folks who are new to Drupal in general. My goal below is to add to and update these snippets over time.

ROUTES & LINKS

DETERMINE THE CURRENT DRUPAL ROUTE

Need to know what the current Drupal route is or need to run some logic against the current route? You can get the current route like so:

$route = \Drupal::routeMatch()->getRouteName();

To some, the \Drupal::routeMatch() syntax might look foreign (it did to me). Here’s a rundown of what’s happening here:

First, \Drupal. This is calling the global Drupal class, which, in Drupal 8, is a bridge between procedural and OO methods of writing Drupal code. The following comes from the documentation:

This class acts as a unified global accessor to arbitrary services within the system in order to ease the transition from procedural code to injected OO code.

Right. Moving on to ::routeMatch(). Here we’re using the routeMatch() method which “Retrieves the currently active route match object.” Simple enough. But what is “::” all about? This StackOverflow answer helped me to understand what that’s all about.

From there, the getRouteName() method returns the current route name as a string. Here are some example routes: entity.node.canonicalview.frontpage and node.type_add.

IS THIS THE FRONT PAGE ROUTE?

Need to check if the current route is the front page route? There’s a service and method for that:

// Is the current route/path the front page?
if ($is_front = \Drupal::service('path.matcher')->isFrontPage()) {}

Here we’re calling the path.matcher service (defined in /core/core.services.yml) and using the isFrontPage() method. For more on services, check out the “Services and Dependency Injection Container” documentation on api.drupal.org which helped me understand how all of these bits work together and the why of their structure.

GET THE REQUESTED PATH

Need to know what the current page’s requested path was, as opposed to the route? You can do this:

$current_uri = \Drupal::request()->getRequestUri();

REDIRECT TO A SPECIFIC ROUTE

Need to redirect to a specific page? In Drupal 7, you would likely handle this with drupal_goto() in your page callback function. In Drupal 8, you can use RedirectResponse() for that. Here is the relevant changelog.

Here are some examples, borrowed heavily from said changelog. First, in procedural PHP:

use Symfony\Component\HttpFoundation\RedirectResponse;

function my_redirect() {
  return new RedirectResponse(\Drupal::url('user.page'));
}

Here is how you would use a Drupal 8 controller to accomplish the same thing:

use Drupal\Core\Controller\ControllerBase;

class MyControllerClass extends ControllerBase {

  public function foo() {
    //...
    return $this->redirect('user.page');
  }
}

 

 

Bitcoin Blogs 2501
Bitcoin Blogs 2502
Bitcoin Blogs 2503
Bitcoin Blogs 2504
Bitcoin Blogs 2505
Bitcoin Blogs 2506
Bitcoin Blogs 2507
Bitcoin Blogs 2508
Bitcoin Blogs 2509
Bitcoin Blogs 2510
Bitcoin Blogs 2511
Bitcoin Blogs 2512
Bitcoin Blogs 2513
Bitcoin Blogs 2514
Bitcoin Blogs 2515
Bitcoin Blogs 2516
Bitcoin Blogs 2517
Bitcoin Blogs 2518
Bitcoin Blogs 2519
Bitcoin Blogs 2520
Bitcoin Blogs 2521
Bitcoin Blogs 2522
Bitcoin Blogs 2523
Bitcoin Blogs 2524
Bitcoin Blogs 2525
Bitcoin Blogs 2526
Bitcoin Blogs 2527
Bitcoin Blogs 2528
Bitcoin Blogs 2529
Bitcoin Blogs 2530
Bitcoin Blogs 2531
Bitcoin Blogs 2532
Bitcoin Blogs 2533
Bitcoin Blogs 2534
Bitcoin Blogs 2535
Bitcoin Blogs 2536
Bitcoin Blogs 2537
Bitcoin Blogs 2538
Bitcoin Blogs 2539
Bitcoin Blogs 2540
Bitcoin Blogs 2541
Bitcoin Blogs 2542
Bitcoin Blogs 2543
Bitcoin Blogs 2544
Bitcoin Blogs 2545
Bitcoin Blogs 2546
Bitcoin Blogs 2547
Bitcoin Blogs 2548
Bitcoin Blogs 2549
Bitcoin Blogs 2550
Bitcoin Blogs 2551
Bitcoin Blogs 2552
Bitcoin Blogs 2553
Bitcoin Blogs 2554
Bitcoin Blogs 2555
Bitcoin Blogs 2556
Bitcoin Blogs 2557
Bitcoin Blogs 2558
Bitcoin Blogs 2559
Bitcoin Blogs 2560
Bitcoin Blogs 2561
Bitcoin Blogs 2562
Bitcoin Blogs 2563
Bitcoin Blogs 2564
Bitcoin Blogs 2565
Bitcoin Blogs 2566
Bitcoin Blogs 2567
Bitcoin Blogs 2568
Bitcoin Blogs 2569
Bitcoin Blogs 2570
Bitcoin Blogs 2571
Bitcoin Blogs 2572
Bitcoin Blogs 2573
Bitcoin Blogs 2574
Bitcoin Blogs 2575
Bitcoin Blogs 2576
Bitcoin Blogs 2577
Bitcoin Blogs 2578
Bitcoin Blogs 2579
Bitcoin Blogs 2580
Bitcoin Blogs 2581
Bitcoin Blogs 2582
Bitcoin Blogs 2583
Bitcoin Blogs 2584
Bitcoin Blogs 2585
Bitcoin Blogs 2586
Bitcoin Blogs 2587
Bitcoin Blogs 2588
Bitcoin Blogs 2589
Bitcoin Blogs 2590
Bitcoin Blogs 2591
Bitcoin Blogs 2592
Bitcoin Blogs 2593
Bitcoin Blogs 2594
Bitcoin Blogs 2595
Bitcoin Blogs 2596
Bitcoin Blogs 2597
Bitcoin Blogs 2598
Bitcoin Blogs 2599
Bitcoin Blogs 2600
Bitcoin Blogs 2601
Bitcoin Blogs 2602
Bitcoin Blogs 2603
Bitcoin Blogs 2604
Bitcoin Blogs 2605
Bitcoin Blogs 2606
Bitcoin Blogs 2607
Bitcoin Blogs 2608
Bitcoin Blogs 2609
Bitcoin Blogs 2610
Bitcoin Blogs 2611
Bitcoin Blogs 2612
Bitcoin Blogs 2613
Bitcoin Blogs 2614
Bitcoin Blogs 2615
Bitcoin Blogs 2616
Bitcoin Blogs 2617
Bitcoin Blogs 2618
Bitcoin Blogs 2619
Bitcoin Blogs 2620
Bitcoin Blogs 2621
Bitcoin Blogs 2622
Bitcoin Blogs 2623
Bitcoin Blogs 2624
Bitcoin Blogs 2625
Bitcoin Blogs 2626
Bitcoin Blogs 2627
Bitcoin Blogs 2628
Bitcoin Blogs 2629
Bitcoin Blogs 2630
Bitcoin Blogs 2631
Bitcoin Blogs 2632
Bitcoin Blogs 2633
Bitcoin Blogs 2634
Bitcoin Blogs 2635
Bitcoin Blogs 2636
Bitcoin Blogs 2637
Bitcoin Blogs 2638
Bitcoin Blogs 2639
Bitcoin Blogs 2640
Bitcoin Blogs 2641
Bitcoin Blogs 2642
Bitcoin Blogs 2643
Bitcoin Blogs 2644
Bitcoin Blogs 2645
Bitcoin Blogs 2646
Bitcoin Blogs 2647
Bitcoin Blogs 2648
Bitcoin Blogs 2649
Bitcoin Blogs 2650
Bitcoin Blogs 2651
Bitcoin Blogs 2652
Bitcoin Blogs 2653
Bitcoin Blogs 2654
Bitcoin Blogs 2655
Bitcoin Blogs 2656
Bitcoin Blogs 2657
Bitcoin Blogs 2658
Bitcoin Blogs 2659
Bitcoin Blogs 2660
Bitcoin Blogs 2661
Bitcoin Blogs 2662
Bitcoin Blogs 2663
Bitcoin Blogs 2664
Bitcoin Blogs 2665
Bitcoin Blogs 2666
Bitcoin Blogs 2667
Bitcoin Blogs 2668
Bitcoin Blogs 2669
Bitcoin Blogs 2670
Bitcoin Blogs 2671
Bitcoin Blogs 2672
Bitcoin Blogs 2673
Bitcoin Blogs 2674
Bitcoin Blogs 2675
Bitcoin Blogs 2676
Bitcoin Blogs 2677
Bitcoin Blogs 2678
Bitcoin Blogs 2679
Bitcoin Blogs 2680
Bitcoin Blogs 2681
Bitcoin Blogs 2682
Bitcoin Blogs 2683
Bitcoin Blogs 2684
Bitcoin Blogs 2685
Bitcoin Blogs 2686
Bitcoin Blogs 2687
Bitcoin Blogs 2688
Bitcoin Blogs 2689
Bitcoin Blogs 2690
Bitcoin Blogs 2691
Bitcoin Blogs 2692
Bitcoin Blogs 2693
Bitcoin Blogs 2694
Bitcoin Blogs 2695
Bitcoin Blogs 2696
Bitcoin Blogs 2697
Bitcoin Blogs 2698
Bitcoin Blogs 2699
Bitcoin Blogs 2700
Bitcoin Blogs 2701
Bitcoin Blogs 2702
Bitcoin Blogs 2703
Bitcoin Blogs 2704
Bitcoin Blogs 2705
Bitcoin Blogs 2706
Bitcoin Blogs 2707
Bitcoin Blogs 2708
Bitcoin Blogs 2709
Bitcoin Blogs 2710
Bitcoin Blogs 2711
Bitcoin Blogs 2712
Bitcoin Blogs 2713
Bitcoin Blogs 2714
Bitcoin Blogs 2715
Bitcoin Blogs 2716
Bitcoin Blogs 2717
Bitcoin Blogs 2718
Bitcoin Blogs 2719
Bitcoin Blogs 2720
Bitcoin Blogs 2721
Bitcoin Blogs 2722
Bitcoin Blogs 2723
Bitcoin Blogs 2724
Bitcoin Blogs 2725
Bitcoin Blogs 2726
Bitcoin Blogs 2727
Bitcoin Blogs 2728
Bitcoin Blogs 2729
Bitcoin Blogs 2730
Bitcoin Blogs 2731
Bitcoin Blogs 2732
Bitcoin Blogs 2733
Bitcoin Blogs 2734
Bitcoin Blogs 2735
Bitcoin Blogs 2736
Bitcoin Blogs 2737
Bitcoin Blogs 2738
Bitcoin Blogs 2739
Bitcoin Blogs 2740
Bitcoin Blogs 2741
Bitcoin Blogs 2742
Bitcoin Blogs 2743
Bitcoin Blogs 2744
Bitcoin Blogs 2745
Bitcoin Blogs 2746
Bitcoin Blogs 2747
Bitcoin Blogs 2748
Bitcoin Blogs 2749
Bitcoin Blogs 2750
Bitcoin Blogs 2751
Bitcoin Blogs 2752
Bitcoin Blogs 2753
Bitcoin Blogs 2754
Bitcoin Blogs 2755
Bitcoin Blogs 2756
Bitcoin Blogs 2757
Bitcoin Blogs 2758
Bitcoin Blogs 2759
Bitcoin Blogs 2760
Bitcoin Blogs 2761
Bitcoin Blogs 2762
Bitcoin Blogs 2763
Bitcoin Blogs 2764
Bitcoin Blogs 2765
Bitcoin Blogs 2766
Bitcoin Blogs 2767
Bitcoin Blogs 2768
Bitcoin Blogs 2769
Bitcoin Blogs 2770
Bitcoin Blogs 2771
Bitcoin Blogs 2772
Bitcoin Blogs 2773
Bitcoin Blogs 2774
Bitcoin Blogs 2775
Bitcoin Blogs 2776
Bitcoin Blogs 2777
Bitcoin Blogs 2778
Bitcoin Blogs 2779
Bitcoin Blogs 2780
Bitcoin Blogs 2781
Bitcoin Blogs 2782
Bitcoin Blogs 2783
Bitcoin Blogs 2784
Bitcoin Blogs 2785
Bitcoin Blogs 2786
Bitcoin Blogs 2787
Bitcoin Blogs 2788
Bitcoin Blogs 2789
Bitcoin Blogs 2790
Bitcoin Blogs 2791
Bitcoin Blogs 2792
Bitcoin Blogs 2793
Bitcoin Blogs 2794
Bitcoin Blogs 2795
Bitcoin Blogs 2796
Bitcoin Blogs 2797
Bitcoin Blogs 2798
Bitcoin Blogs 2799
Bitcoin Blogs 2800
Bitcoin Blogs 2801
Bitcoin Blogs 2802
Bitcoin Blogs 2803
Bitcoin Blogs 2804
Bitcoin Blogs 2805
Bitcoin Blogs 2806
Bitcoin Blogs 2807
Bitcoin Blogs 2808
Bitcoin Blogs 2809
Bitcoin Blogs 2810
Bitcoin Blogs 2811
Bitcoin Blogs 2812
Bitcoin Blogs 2813
Bitcoin Blogs 2814
Bitcoin Blogs 2815
Bitcoin Blogs 2816
Bitcoin Blogs 2817
Bitcoin Blogs 2818
Bitcoin Blogs 2819
Bitcoin Blogs 2820
Bitcoin Blogs 2821
Bitcoin Blogs 2822
Bitcoin Blogs 2823
Bitcoin Blogs 2824
Bitcoin Blogs 2825
Bitcoin Blogs 2826
Bitcoin Blogs 2827
Bitcoin Blogs 2828
Bitcoin Blogs 2829
Bitcoin Blogs 2830
Bitcoin Blogs 2831
Bitcoin Blogs 2832
Bitcoin Blogs 2833
Bitcoin Blogs 2834
Bitcoin Blogs 2835
Bitcoin Blogs 2836
Bitcoin Blogs 2837
Bitcoin Blogs 2838
Bitcoin Blogs 2839
Bitcoin Blogs 2840
Bitcoin Blogs 2841
Bitcoin Blogs 2842
Bitcoin Blogs 2843
Bitcoin Blogs 2844
Bitcoin Blogs 2845
Bitcoin Blogs 2846
Bitcoin Blogs 2847
Bitcoin Blogs 2848
Bitcoin Blogs 2849
Bitcoin Blogs 2850
Bitcoin Blogs 2851
Bitcoin Blogs 2852
Bitcoin Blogs 2853
Bitcoin Blogs 2854
Bitcoin Blogs 2855
Bitcoin Blogs 2856
Bitcoin Blogs 2857
Bitcoin Blogs 2858
Bitcoin Blogs 2859
Bitcoin Blogs 2860
Bitcoin Blogs 2861
Bitcoin Blogs 2862
Bitcoin Blogs 2863
Bitcoin Blogs 2864
Bitcoin Blogs 2865
Bitcoin Blogs 2866
Bitcoin Blogs 2867
Bitcoin Blogs 2868
Bitcoin Blogs 2869
Bitcoin Blogs 2870
Bitcoin Blogs 2871
Bitcoin Blogs 2872
Bitcoin Blogs 2873
Bitcoin Blogs 2874
Bitcoin Blogs 2875
Bitcoin Blogs 2876
Bitcoin Blogs 2877
Bitcoin Blogs 2878
Bitcoin Blogs 2879
Bitcoin Blogs 2880
Bitcoin Blogs 2881
Bitcoin Blogs 2882
Bitcoin Blogs 2883
Bitcoin Blogs 2884
Bitcoin Blogs 2885
Bitcoin Blogs 2886
Bitcoin Blogs 2887
Bitcoin Blogs 2888
Bitcoin Blogs 2889
Bitcoin Blogs 2890
Bitcoin Blogs 2891
Bitcoin Blogs 2892
Bitcoin Blogs 2893
Bitcoin Blogs 2894
Bitcoin Blogs 2895
Bitcoin Blogs 2896
Bitcoin Blogs 2897
Bitcoin Blogs 2898
Bitcoin Blogs 2899
Bitcoin Blogs 2900
Bitcoin Blogs 2901
Bitcoin Blogs 2902
Bitcoin Blogs 2903
Bitcoin Blogs 2904
Bitcoin Blogs 2905
Bitcoin Blogs 2906
Bitcoin Blogs 2907
Bitcoin Blogs 2908
Bitcoin Blogs 2909
Bitcoin Blogs 2910
Bitcoin Blogs 2911
Bitcoin Blogs 2912
Bitcoin Blogs 2913
Bitcoin Blogs 2914
Bitcoin Blogs 2915
Bitcoin Blogs 2916
Bitcoin Blogs 2917
Bitcoin Blogs 2918
Bitcoin Blogs 2919
Bitcoin Blogs 2920
Bitcoin Blogs 2921
Bitcoin Blogs 2922
Bitcoin Blogs 2923
Bitcoin Blogs 2924
Bitcoin Blogs 2925
Bitcoin Blogs 2926
Bitcoin Blogs 2927
Bitcoin Blogs 2928
Bitcoin Blogs 2929
Bitcoin Blogs 2930
Bitcoin Blogs 2931
Bitcoin Blogs 2932
Bitcoin Blogs 2933
Bitcoin Blogs 2934
Bitcoin Blogs 2935
Bitcoin Blogs 2936
Bitcoin Blogs 2937
Bitcoin Blogs 2938
Bitcoin Blogs 2939
Bitcoin Blogs 2940
Bitcoin Blogs 2941
Bitcoin Blogs 2942
Bitcoin Blogs 2943
Bitcoin Blogs 2944
Bitcoin Blogs 2945
Bitcoin Blogs 2946
Bitcoin Blogs 2947
Bitcoin Blogs 2948
Bitcoin Blogs 2949
Bitcoin Blogs 2950
Bitcoin Blogs 2951
Bitcoin Blogs 2952
Bitcoin Blogs 2953
Bitcoin Blogs 2954
Bitcoin Blogs 2955
Bitcoin Blogs 2956
Bitcoin Blogs 2957
Bitcoin Blogs 2958
Bitcoin Blogs 2959
Bitcoin Blogs 2960
Bitcoin Blogs 2961
Bitcoin Blogs 2962
Bitcoin Blogs 2963
Bitcoin Blogs 2964
Bitcoin Blogs 2965
Bitcoin Blogs 2966
Bitcoin Blogs 2967
Bitcoin Blogs 2968
Bitcoin Blogs 2969
Bitcoin Blogs 2970
Bitcoin Blogs 2971
Bitcoin Blogs 2972
Bitcoin Blogs 2973
Bitcoin Blogs 2974
Bitcoin Blogs 2975
Bitcoin Blogs 2976
Bitcoin Blogs 2977
Bitcoin Blogs 2978
Bitcoin Blogs 2979
Bitcoin Blogs 2980
Bitcoin Blogs 2981
Bitcoin Blogs 2982
Bitcoin Blogs 2983
Bitcoin Blogs 2984
Bitcoin Blogs 2985
Bitcoin Blogs 2986
Bitcoin Blogs 2987
Bitcoin Blogs 2988
Bitcoin Blogs 2989
Bitcoin Blogs 2990
Bitcoin Blogs 2991
Bitcoin Blogs 2992
Bitcoin Blogs 2993
Bitcoin Blogs 2994
Bitcoin Blogs 2995
Bitcoin Blogs 2996
Bitcoin Blogs 2997
Bitcoin Blogs 2998
Bitcoin Blogs 2999
Bitcoin Blogs 3000

Related Articles

Leave a Reply

Back to top button