This is part 3 in my series of articles about creating a custom field. I recommend reading Part 1: Field type and Part 2: Field widget first, if you have not done so already.
After creating the field type and field widget it is now time to complete the set by creating the field formatter.
a) Create the file
The field type must be located as follows:
<module_name>/lib/Drupal/<module_name>/Plugin/field/formatter/<field_formatter_name>.php
N.B. The field formatter name should be in CamelCase.
b) Add Contains
, namespace
and use
In the newly created field type file add a brief comment to explain what it consists of:
/**
* @file
* Contains \Drupal\module_name\Plugin\field\formatter\
*/
N.B. The “Contains…” line should match the location and name of this file.
Then add the namespace
as follows:
namespace Drupal\<module_name>\Plugin\field\formatter;
N.B. Again I must emphasise that it is vital for the namespace to match the location of the file otherwise it will not work.
Then add the following use
s:
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
This provides the class that the field widget will extend.
use Drupal\Core\Entity\Field\FieldItemListInterface;
This provides a variable type required within the field formatter class.
c) Add formatter details annotation
The annotation should appear as follows:
/**
* Plugin implementation of the '<field_formatter_id>' formatter.
*
* @FieldFormatter(
* id = "<field_formatter_id>",
* label = @Translation("<field_formatter_label>"),
* field_types = {
* "<field_type_id>"
* }
* )
*/
N.B. All text represented by a <placeholder> should be appropriately replaced according to requirements. The field_type_id
must match the id of a field type and the field_formatter_id
should match the default formatter specified in the field type (see Part 1 of this article).
d) Add field formatter class
Create the field formatter class as follows:
class <field_formatter_name> extends FormatterBase {
}
N.B. The <field_formatter_name> must match the name of this file (case-sensitive).
The field formatter class needs to contain the viewElements()
function that defines how the field will be output:
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#theme' => 'person_default',
'#forename' => check_plain($item->forename),
'#surname' => check_plain($item->surname),
'#age' => check_plain($item->age),
);
}
return $elements;
}
When writing a viewElements()
function for a field with multiple columns I recommend using a custom theme (e.g. person_default
) to avoid including markup within the code.
Here is a simple example, similar to that discussed above.
Once you have created a field type, a field widget and field formatter you now have a custom field type!
After you have saved the files (and cleared caches, of course!) you will find:
- the field type in the Field Type drop-down when adding a new field, under the Manage Fields tab.
- the field widget in the Widget drop-down under the Manage Form Display tab.
- the field formatter Format drop-down under the Manage Display tab.
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