Tutorial for Converting a Module from Drupal 7 to Drupal 8
With Drupal 8 now in Beta (as of this blog post), it’s time to start digging in and getting familiar with the new structure. As a way to start teaching myself D8, I decided to convert a pretty simple module I created in D7 into a D8 module and I was surprised at how many basic D8 concepts it touched on. I wanted to share my work with everyone so that we can all get up to speed together.
Above is the directory structure of my Twitter Pull module. The module uses the Twitter API PHP library (https://github.com/J7mbo/twitter-api-php) as a way of connecting to Twitter and creating a tweet feed. It will contain a tweet block, a page, and an admin page for setting all the Twitter settings.
As you can see, it is not a flat module directory and is broken into many files. We will take a look at the code for each file to see how it all works.
twitter_pull.info.yml
name: Twitter pull description: Pulls twitter tweets and display them in a feed type: module core: 8.x
Drupal 8 now makes use of yml syntax for many of its files. As we can see, the syntax is different than the Drupal 7 .info file, but shouldn’t take too long to get used to.
twitter_pull.module
<?php /** * Implements hook_theme(). */ function twitter_pull_theme($existing, $type, $theme, $path) { return array( 'twitter_pull_tweet_listing' = array( 'variables' => array( 'params' => null, ), 'template' => 'twitter_pull_tweet_listing', ), ); } }
In my .module file, I specify my theme which will be used to display my tweets. One difference I found between Drupal 7 and Drupal 8 here: In Drupal 7, since I always stored my themes in moduleName/templates/template.name.tpl.php, I needed to specify this path in the template ‘template’ => ‘templates/twitter_pull_tweet_listing’.
In Drupal 8, you shouldn’t add in the folder name as it looks for it automatically, and if you do, it won’t be able to pick it up.
twitter_pull.credentials.yml
oauth_access_token: "" oauth_access_token_secret: "" consumer_key: "" consumer_secret: "" screen_name: "" tweet_count: ""
In Drupal 8, we no longer store site variables in the variables database table. Instead, they are stored in a config file, in [module-name]/config/install. The file name should be [module-name].someUsefulDescription.yml. Drupal 8 no longer uses variable_get() and variable_set() for retrieving and setting the variable values, and we’ll see the new way in the code following.
One thing to note here: These configuration variables are tracked in Drupal from the time the module is installed. Once installed, adding new variables will not be picked up. You’ll either have to uninstall and reinstall (which obviously won’t be practical in all cases) or run a module update, similar when you run a module update when updating a schema (I haven’t actually tried this yet, so I don’t know how different it is to Drupal 7).
twitter_pull.routing.tml
twitter_pull.tweets: path: '/tweets' defaults: _controller: '\Drupal\twitter_pull\Controller\Twitter_PullController::content' _title: 'Tweets' requirements: _permission: 'access content' twitter_pull.settings: path: 'admin/config/services/twitter-settings' defaults: _form: '\Drupal\twitter_pull\Form\TwitterSettingsForm' requirements: _permission: 'administer site configuration'
Jumping down to the last file in the list, the routing.yml file. Drupal 8 now uses Symfony’s Routing component. This replaces Drupal 7’s hook_menu(). In my file, am registering a regular page with the path /tweets whose callback is a controller. I also registered an admin path so that I could create an admin form for setting the Twitter settings. The appropriate permission settings are set here as well.
Read More 2501
Read More 2502
Read More 2503
Read More 2504
Read More 2505
Read More 2506
Read More 2507
Read More 2508
Read More 2509
Read More 2510
Read More 2511
Read More 2512
Read More 2513
Read More 2514
Read More 2515
Read More 2516
Read More 2517
Read More 2518
Read More 2519
Read More 2520
Read More 2521
Read More 2522
Read More 2523
Read More 2524
Read More 2525
Read More 2526
Read More 2527
Read More 2528
Read More 2529
Read More 2530
Read More 2531
Read More 2532
Read More 2533
Read More 2534
Read More 2535
Read More 2536
Read More 2537
Read More 2538
Read More 2539
Read More 2540
Read More 2541
Read More 2542
Read More 2543
Read More 2544
Read More 2545
Read More 2546
Read More 2547
Read More 2548
Read More 2549
Read More 2550
Read More 2551
Read More 2552
Read More 2553
Read More 2554
Read More 2555
Read More 2556
Read More 2557
Read More 2558
Read More 2559
Read More 2560
Read More 2561
Read More 2562
Read More 2563
Read More 2564
Read More 2565
Read More 2566
Read More 2567
Read More 2568
Read More 2569
Read More 2570
Read More 2571
Read More 2572
Read More 2573
Read More 2574
Read More 2575
Read More 2576
Read More 2577
Read More 2578
Read More 2579
Read More 2580
Read More 2581
Read More 2582
Read More 2583
Read More 2584
Read More 2585
Read More 2586
Read More 2587
Read More 2588
Read More 2589
Read More 2590
Read More 2591
Read More 2592
Read More 2593
Read More 2594
Read More 2595
Read More 2596
Read More 2597
Read More 2598
Read More 2599
Read More 2600
Read More 2601
Read More 2602
Read More 2603
Read More 2604
Read More 2605
Read More 2606
Read More 2607
Read More 2608
Read More 2609
Read More 2610
Read More 2611
Read More 2612
Read More 2613
Read More 2614
Read More 2615
Read More 2616
Read More 2617
Read More 2618
Read More 2619
Read More 2620
Read More 2621
Read More 2622
Read More 2623
Read More 2624
Read More 2625
Read More 2626
Read More 2627
Read More 2628
Read More 2629
Read More 2630
Read More 2631
Read More 2632
Read More 2633
Read More 2634
Read More 2635
Read More 2636
Read More 2637
Read More 2638
Read More 2639
Read More 2640
Read More 2641
Read More 2642
Read More 2643
Read More 2644
Read More 2645
Read More 2646
Read More 2647
Read More 2648
Read More 2649
Read More 2650
Read More 2651
Read More 2652
Read More 2653
Read More 2654
Read More 2655
Read More 2656
Read More 2657
Read More 2658
Read More 2659
Read More 2660
Read More 2661
Read More 2662
Read More 2663
Read More 2664
Read More 2665
Read More 2666
Read More 2667
Read More 2668
Read More 2669
Read More 2670
Read More 2671
Read More 2672
Read More 2673
Read More 2674
Read More 2675
Read More 2676
Read More 2677
Read More 2678
Read More 2679
Read More 2680
Read More 2681
Read More 2682
Read More 2683
Read More 2684
Read More 2685
Read More 2686
Read More 2687
Read More 2688
Read More 2689
Read More 2690
Read More 2691
Read More 2692
Read More 2693
Read More 2694
Read More 2695
Read More 2696
Read More 2697
Read More 2698
Read More 2699
Read More 2700
Read More 2701
Read More 2702
Read More 2703
Read More 2704
Read More 2705
Read More 2706
Read More 2707
Read More 2708
Read More 2709
Read More 2710
Read More 2711
Read More 2712
Read More 2713
Read More 2714
Read More 2715
Read More 2716
Read More 2717
Read More 2718
Read More 2719
Read More 2720
Read More 2721
Read More 2722
Read More 2723
Read More 2724
Read More 2725
Read More 2726
Read More 2727
Read More 2728
Read More 2729
Read More 2730
Read More 2731
Read More 2732
Read More 2733
Read More 2734
Read More 2735
Read More 2736
Read More 2737
Read More 2738
Read More 2739
Read More 2740
Read More 2741
Read More 2742
Read More 2743
Read More 2744
Read More 2745
Read More 2746
Read More 2747
Read More 2748
Read More 2749
Read More 2750
Read More 2751
Read More 2752
Read More 2753
Read More 2754
Read More 2755
Read More 2756
Read More 2757
Read More 2758
Read More 2759
Read More 2760
Read More 2761
Read More 2762
Read More 2763
Read More 2764
Read More 2765
Read More 2766
Read More 2767
Read More 2768
Read More 2769
Read More 2770
Read More 2771
Read More 2772
Read More 2773
Read More 2774
Read More 2775
Read More 2776
Read More 2777
Read More 2778
Read More 2779
Read More 2780
Read More 2781
Read More 2782
Read More 2783
Read More 2784
Read More 2785
Read More 2786
Read More 2787
Read More 2788
Read More 2789
Read More 2790
Read More 2791
Read More 2792
Read More 2793
Read More 2794
Read More 2795
Read More 2796
Read More 2797
Read More 2798
Read More 2799
Read More 2800
Read More 2801
Read More 2802
Read More 2803
Read More 2804
Read More 2805
Read More 2806
Read More 2807
Read More 2808
Read More 2809
Read More 2810
Read More 2811
Read More 2812
Read More 2813
Read More 2814
Read More 2815
Read More 2816
Read More 2817
Read More 2818
Read More 2819
Read More 2820
Read More 2821
Read More 2822
Read More 2823
Read More 2824
Read More 2825
Read More 2826
Read More 2827
Read More 2828
Read More 2829
Read More 2830
Read More 2831
Read More 2832
Read More 2833
Read More 2834
Read More 2835
Read More 2836
Read More 2837
Read More 2838
Read More 2839
Read More 2840
Read More 2841
Read More 2842
Read More 2843
Read More 2844
Read More 2845
Read More 2846
Read More 2847
Read More 2848
Read More 2849
Read More 2850
Read More 2851
Read More 2852
Read More 2853
Read More 2854
Read More 2855
Read More 2856
Read More 2857
Read More 2858
Read More 2859
Read More 2860
Read More 2861
Read More 2862
Read More 2863
Read More 2864
Read More 2865
Read More 2866
Read More 2867
Read More 2868
Read More 2869
Read More 2870
Read More 2871
Read More 2872
Read More 2873
Read More 2874
Read More 2875
Read More 2876
Read More 2877
Read More 2878
Read More 2879
Read More 2880
Read More 2881
Read More 2882
Read More 2883
Read More 2884
Read More 2885
Read More 2886
Read More 2887
Read More 2888
Read More 2889
Read More 2890
Read More 2891
Read More 2892
Read More 2893
Read More 2894
Read More 2895
Read More 2896
Read More 2897
Read More 2898
Read More 2899
Read More 2900
Read More 2901
Read More 2902
Read More 2903
Read More 2904
Read More 2905
Read More 2906
Read More 2907
Read More 2908
Read More 2909
Read More 2910
Read More 2911
Read More 2912
Read More 2913
Read More 2914
Read More 2915
Read More 2916
Read More 2917
Read More 2918
Read More 2919
Read More 2920
Read More 2921
Read More 2922
Read More 2923
Read More 2924
Read More 2925
Read More 2926
Read More 2927
Read More 2928
Read More 2929
Read More 2930
Read More 2931
Read More 2932
Read More 2933
Read More 2934
Read More 2935
Read More 2936
Read More 2937
Read More 2938
Read More 2939
Read More 2940
Read More 2941
Read More 2942
Read More 2943
Read More 2944
Read More 2945
Read More 2946
Read More 2947
Read More 2948
Read More 2949
Read More 2950
Read More 2951
Read More 2952
Read More 2953
Read More 2954
Read More 2955
Read More 2956
Read More 2957
Read More 2958
Read More 2959
Read More 2960
Read More 2961
Read More 2962
Read More 2963
Read More 2964
Read More 2965
Read More 2966
Read More 2967
Read More 2968
Read More 2969
Read More 2970
Read More 2971
Read More 2972
Read More 2973
Read More 2974
Read More 2975
Read More 2976
Read More 2977
Read More 2978
Read More 2979
Read More 2980
Read More 2981
Read More 2982
Read More 2983
Read More 2984
Read More 2985
Read More 2986
Read More 2987
Read More 2988
Read More 2989
Read More 2990
Read More 2991
Read More 2992
Read More 2993
Read More 2994
Read More 2995
Read More 2996
Read More 2997
Read More 2998
Read More 2999
Read More 3000