Even though there was somewhat of a delay since my last post in this series, it seems no-one else has really covered any of the advanced use cases of Drupal 8 in tutorials yet. So, here is the next installment in my series. I initially wanted to cover creating a new plugin type, but since that already requires creating a new servive, I thought I’d cover that smaller part first and then move on to plugin types in the next post.
I realize that now already a lot more people have started on their Drupal 8 modules, but perhaps this will make this series all the more useful.
Services in Drupal 8
First, a short overview of what a service even is. Basically it is a component (represented as a class) providing a certain, limited range of functionality. The database is a service, the entity manager (which is what you now use for loading entities) is a service, translation, configuration – everything handled by services. Getting the current user – also a service now, ridding us of the highly unclean global variable.
In general, a lot of what was previously a file in includes/
containing some functions with a common prefix is now a service (or split into multiple services).
The upsides of this is that the implementation and logic is cleanly bundled and properly encapsulated, that all these components can easily be switched out by contrib or later core updates, and that these systems can also be very well tested with unit tests. Even more, since services can be used with dependency injection, it also makes it much easier to test all other classes that use any of these services (if they can use dependency injection and do it properly).
(For reference, here is the official documentation on services.)
Dependency injection
This has been covered already in a lot of other blog posts, probably since it is both a rather central concept in Drupal 8, and a bit complicated when you first encounter it. However, before using it, I should still at least skim over the topic. Feel free to skip to the next heading if you feel you already know what dependency injection is and how it roughly works in Drupal 8.
Dependency injection is a programming technique where a class with external dependencies (e.g., a mechanism for translating) explicitly defines these dependencies (in some form) and makes the class which constructs it responsible for supplying those dependencies. That way, the class itself can be self-contained and doesn’t need to know about where it can get those dependencies, or use any global functions or anything to achieve that.
Consider for example the following class:
<?php
class ExampleClass {
public function getDefinition() {
return array(
'label' => t('example class'),
'type' => 'foo',
);
}
}
?>
For translating the definition label, this explicitly uses the global t()
function. Now, what’s bad about this I, hear you ask, it worked well enough in Drupal 7, right?
The problem is that it becomes almost impossible to properly unit-test that method without bootstrapping Drupal to the point where the t()
function becomes available and functional. It’s also more or less impossible to switch out Drupal’s translation mechanism without hacking core, since there is no way to redirect the call to t()
.
But if translation is done by a class with a defined interface (in other words, a service), it ‘s possible to do this much cleaner:
<?php
class ExampleClass {
public function __construct(TranslationServiceInterface $translation) {
$this->translation = $translation;
}
public function getDefinition() {
return array(
'label' => $this->translation->translate('example class'),
'type' => 'foo',
);
}
}
?>
Read More 3001
Read More 3002
Read More 3003
Read More 3004
Read More 3005
Read More 3006
Read More 3007
Read More 3008
Read More 3009
Read More 3010
Read More 3011
Read More 3012
Read More 3013
Read More 3014
Read More 3015
Read More 3016
Read More 3017
Read More 3018
Read More 3019
Read More 3020
Read More 3021
Read More 3022
Read More 3023
Read More 3024
Read More 3025
Read More 3026
Read More 3027
Read More 3028
Read More 3029
Read More 3030
Read More 3031
Read More 3032
Read More 3033
Read More 3034
Read More 3035
Read More 3036
Read More 3037
Read More 3038
Read More 3039
Read More 3040
Read More 3041
Read More 3042
Read More 3043
Read More 3044
Read More 3045
Read More 3046
Read More 3047
Read More 3048
Read More 3049
Read More 3050
Read More 3051
Read More 3052
Read More 3053
Read More 3054
Read More 3055
Read More 3056
Read More 3057
Read More 3058
Read More 3059
Read More 3060
Read More 3061
Read More 3062
Read More 3063
Read More 3064
Read More 3065
Read More 3066
Read More 3067
Read More 3068
Read More 3069
Read More 3070
Read More 3071
Read More 3072
Read More 3073
Read More 3074
Read More 3075
Read More 3076
Read More 3077
Read More 3078
Read More 3079
Read More 3080
Read More 3081
Read More 3082
Read More 3083
Read More 3084
Read More 3085
Read More 3086
Read More 3087
Read More 3088
Read More 3089
Read More 3090
Read More 3091
Read More 3092
Read More 3093
Read More 3094
Read More 3095
Read More 3096
Read More 3097
Read More 3098
Read More 3099
Read More 3100
Read More 3101
Read More 3102
Read More 3103
Read More 3104
Read More 3105
Read More 3106
Read More 3107
Read More 3108
Read More 3109
Read More 3110
Read More 3111
Read More 3112
Read More 3113
Read More 3114
Read More 3115
Read More 3116
Read More 3117
Read More 3118
Read More 3119
Read More 3120
Read More 3121
Read More 3122
Read More 3123
Read More 3124
Read More 3125
Read More 3126
Read More 3127
Read More 3128
Read More 3129
Read More 3130
Read More 3131
Read More 3132
Read More 3133
Read More 3134
Read More 3135
Read More 3136
Read More 3137
Read More 3138
Read More 3139
Read More 3140
Read More 3141
Read More 3142
Read More 3143
Read More 3144
Read More 3145
Read More 3146
Read More 3147
Read More 3148
Read More 3149
Read More 3150
Read More 3151
Read More 3152
Read More 3153
Read More 3154
Read More 3155
Read More 3156
Read More 3157
Read More 3158
Read More 3159
Read More 3160
Read More 3161
Read More 3162
Read More 3163
Read More 3164
Read More 3165
Read More 3166
Read More 3167
Read More 3168
Read More 3169
Read More 3170
Read More 3171
Read More 3172
Read More 3173
Read More 3174
Read More 3175
Read More 3176
Read More 3177
Read More 3178
Read More 3179
Read More 3180
Read More 3181
Read More 3182
Read More 3183
Read More 3184
Read More 3185
Read More 3186
Read More 3187
Read More 3188
Read More 3189
Read More 3190
Read More 3191
Read More 3192
Read More 3193
Read More 3194
Read More 3195
Read More 3196
Read More 3197
Read More 3198
Read More 3199
Read More 3200
Read More 3201
Read More 3202
Read More 3203
Read More 3204
Read More 3205
Read More 3206
Read More 3207
Read More 3208
Read More 3209
Read More 3210
Read More 3211
Read More 3212
Read More 3213
Read More 3214
Read More 3215
Read More 3216
Read More 3217
Read More 3218
Read More 3219
Read More 3220
Read More 3221
Read More 3222
Read More 3223
Read More 3224
Read More 3225
Read More 3226
Read More 3227
Read More 3228
Read More 3229
Read More 3230
Read More 3231
Read More 3232
Read More 3233
Read More 3234
Read More 3235
Read More 3236
Read More 3237
Read More 3238
Read More 3239
Read More 3240
Read More 3241
Read More 3242
Read More 3243
Read More 3244
Read More 3245
Read More 3246
Read More 3247
Read More 3248
Read More 3249
Read More 3250
Read More 3251
Read More 3252
Read More 3253
Read More 3254
Read More 3255
Read More 3256
Read More 3257
Read More 3258
Read More 3259
Read More 3260
Read More 3261
Read More 3262
Read More 3263
Read More 3264
Read More 3265
Read More 3266
Read More 3267
Read More 3268
Read More 3269
Read More 3270
Read More 3271
Read More 3272
Read More 3273
Read More 3274
Read More 3275
Read More 3276
Read More 3277
Read More 3278
Read More 3279
Read More 3280
Read More 3281
Read More 3282
Read More 3283
Read More 3284
Read More 3285
Read More 3286
Read More 3287
Read More 3288
Read More 3289
Read More 3290
Read More 3291
Read More 3292
Read More 3293
Read More 3294
Read More 3295
Read More 3296
Read More 3297
Read More 3298
Read More 3299
Read More 3300
Read More 3301
Read More 3302
Read More 3303
Read More 3304
Read More 3305
Read More 3306
Read More 3307
Read More 3308
Read More 3309
Read More 3310
Read More 3311
Read More 3312
Read More 3313
Read More 3314
Read More 3315
Read More 3316
Read More 3317
Read More 3318
Read More 3319
Read More 3320
Read More 3321
Read More 3322
Read More 3323
Read More 3324
Read More 3325
Read More 3326
Read More 3327
Read More 3328
Read More 3329
Read More 3330
Read More 3331
Read More 3332
Read More 3333
Read More 3334
Read More 3335
Read More 3336
Read More 3337
Read More 3338
Read More 3339
Read More 3340
Read More 3341
Read More 3342
Read More 3343
Read More 3344
Read More 3345
Read More 3346
Read More 3347
Read More 3348
Read More 3349
Read More 3350
Read More 3351
Read More 3352
Read More 3353
Read More 3354
Read More 3355
Read More 3356
Read More 3357
Read More 3358
Read More 3359
Read More 3360
Read More 3361
Read More 3362
Read More 3363
Read More 3364
Read More 3365
Read More 3366
Read More 3367
Read More 3368
Read More 3369
Read More 3370
Read More 3371
Read More 3372
Read More 3373
Read More 3374
Read More 3375
Read More 3376
Read More 3377
Read More 3378
Read More 3379
Read More 3380
Read More 3381
Read More 3382
Read More 3383
Read More 3384
Read More 3385
Read More 3386
Read More 3387
Read More 3388
Read More 3389
Read More 3390
Read More 3391
Read More 3392
Read More 3393
Read More 3394
Read More 3395
Read More 3396
Read More 3397
Read More 3398
Read More 3399
Read More 3400
Read More 3401
Read More 3402
Read More 3403
Read More 3404
Read More 3405
Read More 3406
Read More 3407
Read More 3408
Read More 3409
Read More 3410
Read More 3411
Read More 3412
Read More 3413
Read More 3414
Read More 3415
Read More 3416
Read More 3417
Read More 3418
Read More 3419
Read More 3420
Read More 3421
Read More 3422
Read More 3423
Read More 3424
Read More 3425
Read More 3426
Read More 3427
Read More 3428
Read More 3429
Read More 3430
Read More 3431
Read More 3432
Read More 3433
Read More 3434
Read More 3435
Read More 3436
Read More 3437
Read More 3438
Read More 3439
Read More 3440
Read More 3441
Read More 3442
Read More 3443
Read More 3444
Read More 3445
Read More 3446
Read More 3447
Read More 3448
Read More 3449
Read More 3450
Read More 3451
Read More 3452
Read More 3453
Read More 3454
Read More 3455
Read More 3456
Read More 3457
Read More 3458
Read More 3459
Read More 3460
Read More 3461
Read More 3462
Read More 3463
Read More 3464
Read More 3465
Read More 3466
Read More 3467
Read More 3468
Read More 3469
Read More 3470
Read More 3471
Read More 3472
Read More 3473
Read More 3474
Read More 3475
Read More 3476
Read More 3477
Read More 3478
Read More 3479
Read More 3480
Read More 3481
Read More 3482
Read More 3483
Read More 3484
Read More 3485
Read More 3486
Read More 3487
Read More 3488
Read More 3489
Read More 3490
Read More 3491
Read More 3492
Read More 3493
Read More 3494
Read More 3495
Read More 3496
Read More 3497
Read More 3498
Read More 3499
Read More 3500