In this post, you will learn how to theme Drupal 8 views by overriding default views templates and use our own markup to build an accordion (based on bootstrap 3 accordion). We will begin by identifying a custom theme for our newly installed Drupal 8 site and add bootstrap 3 css and javascript files.
This is all we need to create a new theme.
├── demotheme.info.yml ├── demotheme.libraries.yml ├── css │ ├── bootstrap-theme.min.css │ └── bootstrap.min.css └── js └── bootstrap.min.js
Then we will create a new view from “admin/structure/views/add” to show latest added articles.
- View basic information: We will name our view “Articles Accordion” (machine name: “articles_accordion”).
- View settings: Choose (Show: Content), (of type: Article) and (sorted by: Newly first).
- Page settings: Check “Create a page” and set “Page title” and “Path” to your liking. Under “Page display settings” choose (Display format: Unformatted list), (of: fields).
- Click “Save and edit”
Here is a screenshot for our view initial settings:
After saving the view we will click on “Content: Title” and uncheck “Link to the Content”, then add the “Body” field.
Here is a screenshot for the view final settings:
Understanding Views Templates
Views default templates are located under “/core/modules/views/templates/” folder.
Each view uses minimum of two templates:
- The first template is “views-view.html.twig”. This template is used for all views and contains the layout for the view. (view content, header, footer, exposed form and attachments)
- The second template is the style template. The default used template will vary based on the applied view style (grid, table, html list or unformatted).
- Grid: views-view-grid.html.twig
- Table: views-view-table.html.twig
- HTML List: views-view-list.html.twig
- Unformatted: views-view-unformatted.html.twig
- The third template is “views-view-fields.html.twig”. This template is used only if the view row style is set to “Fields”. This template is responsible for looping through available fields and print fields wrappers, labels and markup.
- The fourth template is “views-view-field.html.twig”. This template is used only if the view row style is set to “Fields”. This is the last template and is responsible for printing each field markup.
Naming Views Templates
Each type of the view templates above can be overridden with a variety of names. The template name is a concatenation of (base template name, view machine name, view display type and view display id – separated by 2 hyphens “–“).
The following are the possible template names sorted by precedence:
- [base template name]–[view machine name]–[view display id].html.twig
- [base template name]–[view machine name]–[view display type].html.twig
- [base template name]–[view display type].html.twig
- [base template name]–[view machine name].html.twig
- [base template name].html.twig
For example; If we want to override “views-view.html.twig” template for our view, the following template names are valid:
- [base template name]–[view machine name].html.twig
- views-view–articles-accordion–page.html.twig
- views-view–page.html.twig
- views-view–articles-accordion.html.twig
- views-view.html.twig
Building Our Bootstrap Accordion
To build our version of bootstrap accordion, we will need to override “views-view-unformatted.html.twig” (because this is the style in use) and “views-view-fields.html.twig”.
The easiest way to get started is to copy the templates from /core/modules/views/templates folder into our theme folder and then rename “views-view-unformatted.html.twig” to “views-view-unformatted–articles-accordion–page-1.html.twig” and “views-view-fields.html.twig” to “views-view-fields–articles-accordion–page.html.twig”
{% for row in rows %} {% set row_classes = [ default_row_class ? ‘views-row’, ‘panel’, ‘panel-default’, ] %}
{% endfor %}
This is how how our accordion looks like.
Visit website 3001
Visit website 3002
Visit website 3003
Visit website 3004
Visit website 3005
Visit website 3006
Visit website 3007
Visit website 3008
Visit website 3009
Visit website 3010
Visit website 3011
Visit website 3012
Visit website 3013
Visit website 3014
Visit website 3015
Visit website 3016
Visit website 3017
Visit website 3018
Visit website 3019
Visit website 3020
Visit website 3021
Visit website 3022
Visit website 3023
Visit website 3024
Visit website 3025
Visit website 3026
Visit website 3027
Visit website 3028
Visit website 3029
Visit website 3030
Visit website 3031
Visit website 3032
Visit website 3033
Visit website 3034
Visit website 3035
Visit website 3036
Visit website 3037
Visit website 3038
Visit website 3039
Visit website 3040
Visit website 3041
Visit website 3042
Visit website 3043
Visit website 3044
Visit website 3045
Visit website 3046
Visit website 3047
Visit website 3048
Visit website 3049
Visit website 3050
Visit website 3051
Visit website 3052
Visit website 3053
Visit website 3054
Visit website 3055
Visit website 3056
Visit website 3057
Visit website 3058
Visit website 3059
Visit website 3060
Visit website 3061
Visit website 3062
Visit website 3063
Visit website 3064
Visit website 3065
Visit website 3066
Visit website 3067
Visit website 3068
Visit website 3069
Visit website 3070
Visit website 3071
Visit website 3072
Visit website 3073
Visit website 3074
Visit website 3075
Visit website 3076
Visit website 3077
Visit website 3078
Visit website 3079
Visit website 3080
Visit website 3081
Visit website 3082
Visit website 3083
Visit website 3084
Visit website 3085
Visit website 3086
Visit website 3087
Visit website 3088
Visit website 3089
Visit website 3090
Visit website 3091
Visit website 3092
Visit website 3093
Visit website 3094
Visit website 3095
Visit website 3096
Visit website 3097
Visit website 3098
Visit website 3099
Visit website 3100
Visit website 3101
Visit website 3102
Visit website 3103
Visit website 3104
Visit website 3105
Visit website 3106
Visit website 3107
Visit website 3108
Visit website 3109
Visit website 3110
Visit website 3111
Visit website 3112
Visit website 3113
Visit website 3114
Visit website 3115
Visit website 3116
Visit website 3117
Visit website 3118
Visit website 3119
Visit website 3120
Visit website 3121
Visit website 3122
Visit website 3123
Visit website 3124
Visit website 3125
Visit website 3126
Visit website 3127
Visit website 3128
Visit website 3129
Visit website 3130
Visit website 3131
Visit website 3132
Visit website 3133
Visit website 3134
Visit website 3135
Visit website 3136
Visit website 3137
Visit website 3138
Visit website 3139
Visit website 3140
Visit website 3141
Visit website 3142
Visit website 3143
Visit website 3144
Visit website 3145
Visit website 3146
Visit website 3147
Visit website 3148
Visit website 3149
Visit website 3150
Visit website 3151
Visit website 3152
Visit website 3153
Visit website 3154
Visit website 3155
Visit website 3156
Visit website 3157
Visit website 3158
Visit website 3159
Visit website 3160
Visit website 3161
Visit website 3162
Visit website 3163
Visit website 3164
Visit website 3165
Visit website 3166
Visit website 3167
Visit website 3168
Visit website 3169
Visit website 3170
Visit website 3171
Visit website 3172
Visit website 3173
Visit website 3174
Visit website 3175
Visit website 3176
Visit website 3177
Visit website 3178
Visit website 3179
Visit website 3180
Visit website 3181
Visit website 3182
Visit website 3183
Visit website 3184
Visit website 3185
Visit website 3186
Visit website 3187
Visit website 3188
Visit website 3189
Visit website 3190
Visit website 3191
Visit website 3192
Visit website 3193
Visit website 3194
Visit website 3195
Visit website 3196
Visit website 3197
Visit website 3198
Visit website 3199
Visit website 3200
Visit website 3201
Visit website 3202
Visit website 3203
Visit website 3204
Visit website 3205
Visit website 3206
Visit website 3207
Visit website 3208
Visit website 3209
Visit website 3210
Visit website 3211
Visit website 3212
Visit website 3213
Visit website 3214
Visit website 3215
Visit website 3216
Visit website 3217
Visit website 3218
Visit website 3219
Visit website 3220
Visit website 3221
Visit website 3222
Visit website 3223
Visit website 3224
Visit website 3225
Visit website 3226
Visit website 3227
Visit website 3228
Visit website 3229
Visit website 3230
Visit website 3231
Visit website 3232
Visit website 3233
Visit website 3234
Visit website 3235
Visit website 3236
Visit website 3237
Visit website 3238
Visit website 3239
Visit website 3240
Visit website 3241
Visit website 3242
Visit website 3243
Visit website 3244
Visit website 3245
Visit website 3246
Visit website 3247
Visit website 3248
Visit website 3249
Visit website 3250
Visit website 3251
Visit website 3252
Visit website 3253
Visit website 3254
Visit website 3255
Visit website 3256
Visit website 3257
Visit website 3258
Visit website 3259
Visit website 3260
Visit website 3261
Visit website 3262
Visit website 3263
Visit website 3264
Visit website 3265
Visit website 3266
Visit website 3267
Visit website 3268
Visit website 3269
Visit website 3270
Visit website 3271
Visit website 3272
Visit website 3273
Visit website 3274
Visit website 3275
Visit website 3276
Visit website 3277
Visit website 3278
Visit website 3279
Visit website 3280
Visit website 3281
Visit website 3282
Visit website 3283
Visit website 3284
Visit website 3285
Visit website 3286
Visit website 3287
Visit website 3288
Visit website 3289
Visit website 3290
Visit website 3291
Visit website 3292
Visit website 3293
Visit website 3294
Visit website 3295
Visit website 3296
Visit website 3297
Visit website 3298
Visit website 3299
Visit website 3300
Visit website 3301
Visit website 3302
Visit website 3303
Visit website 3304
Visit website 3305
Visit website 3306
Visit website 3307
Visit website 3308
Visit website 3309
Visit website 3310
Visit website 3311
Visit website 3312
Visit website 3313
Visit website 3314
Visit website 3315
Visit website 3316
Visit website 3317
Visit website 3318
Visit website 3319
Visit website 3320
Visit website 3321
Visit website 3322
Visit website 3323
Visit website 3324
Visit website 3325
Visit website 3326
Visit website 3327
Visit website 3328
Visit website 3329
Visit website 3330
Visit website 3331
Visit website 3332
Visit website 3333
Visit website 3334
Visit website 3335
Visit website 3336
Visit website 3337
Visit website 3338
Visit website 3339
Visit website 3340
Visit website 3341
Visit website 3342
Visit website 3343
Visit website 3344
Visit website 3345
Visit website 3346
Visit website 3347
Visit website 3348
Visit website 3349
Visit website 3350
Visit website 3351
Visit website 3352
Visit website 3353
Visit website 3354
Visit website 3355
Visit website 3356
Visit website 3357
Visit website 3358
Visit website 3359
Visit website 3360
Visit website 3361
Visit website 3362
Visit website 3363
Visit website 3364
Visit website 3365
Visit website 3366
Visit website 3367
Visit website 3368
Visit website 3369
Visit website 3370
Visit website 3371
Visit website 3372
Visit website 3373
Visit website 3374
Visit website 3375
Visit website 3376
Visit website 3377
Visit website 3378
Visit website 3379
Visit website 3380
Visit website 3381
Visit website 3382
Visit website 3383
Visit website 3384
Visit website 3385
Visit website 3386
Visit website 3387
Visit website 3388
Visit website 3389
Visit website 3390
Visit website 3391
Visit website 3392
Visit website 3393
Visit website 3394
Visit website 3395
Visit website 3396
Visit website 3397
Visit website 3398
Visit website 3399
Visit website 3400
Visit website 3401
Visit website 3402
Visit website 3403
Visit website 3404
Visit website 3405
Visit website 3406
Visit website 3407
Visit website 3408
Visit website 3409
Visit website 3410
Visit website 3411
Visit website 3412
Visit website 3413
Visit website 3414
Visit website 3415
Visit website 3416
Visit website 3417
Visit website 3418
Visit website 3419
Visit website 3420
Visit website 3421
Visit website 3422
Visit website 3423
Visit website 3424
Visit website 3425
Visit website 3426
Visit website 3427
Visit website 3428
Visit website 3429
Visit website 3430
Visit website 3431
Visit website 3432
Visit website 3433
Visit website 3434
Visit website 3435
Visit website 3436
Visit website 3437
Visit website 3438
Visit website 3439
Visit website 3440
Visit website 3441
Visit website 3442
Visit website 3443
Visit website 3444
Visit website 3445
Visit website 3446
Visit website 3447
Visit website 3448
Visit website 3449
Visit website 3450
Visit website 3451
Visit website 3452
Visit website 3453
Visit website 3454
Visit website 3455
Visit website 3456
Visit website 3457
Visit website 3458
Visit website 3459
Visit website 3460
Visit website 3461
Visit website 3462
Visit website 3463
Visit website 3464
Visit website 3465
Visit website 3466
Visit website 3467
Visit website 3468
Visit website 3469
Visit website 3470
Visit website 3471
Visit website 3472
Visit website 3473
Visit website 3474
Visit website 3475
Visit website 3476
Visit website 3477
Visit website 3478
Visit website 3479
Visit website 3480
Visit website 3481
Visit website 3482
Visit website 3483
Visit website 3484
Visit website 3485
Visit website 3486
Visit website 3487
Visit website 3488
Visit website 3489
Visit website 3490
Visit website 3491
Visit website 3492
Visit website 3493
Visit website 3494
Visit website 3495
Visit website 3496
Visit website 3497
Visit website 3498
Visit website 3499
Visit website 3500