Uncategorized

D8 Development: Console, Menus, and Entity load/save basics

I learn best when I have a problem to solve, and with one of our D8 upgrade projects, we had a mess to clean up in the menu system. This provided an excellent oppportunity to get hands-on in Drupal core, learning some of the major differences from earlier versions, and three things in particular:

  1. How easy it is to use Drupal Console to create your own tools
  2. How much the menu system has changed
  3. How to load and save just about any entity

The problem

It seemed like an easy one: links on the menu were going to internal “node” paths instead of the aliased “friendly URL”. For example, a link on a submenu went to “/node/494” instead of “/about/accreditation”. In this particular project, there were several hundred links that got mangled in this way.

I’m not exactly sure how this happened, but it has something to do with starting the migration in Drupal 8.0, updating the migration to work with 8.1, and going back to clean up all the friendly URLs. Somewhere along the way, the menu system ended up with links to the node/[nid] path, and not the friendly URLs, even though the pages themselves worked fine with the URL aliases.

The Redirect module was already set up, and configured to redirect to the alias — but this does not seem to work correctly yet.

Bulk operations were of no help — and worse, the menu and parent items on the menu link edit form on most of the links did not show up correctly, even though the menu itself showed links in the correct place.

Creating a repair tool using Drupal Console

In earlier versions of Drupal, we’ve created dozens of little helper scripts using Drush. It’s relatively easy to create a drush script to fix up data, set up integrations with other systems, diagnose issues, etc. Drush was our go-to tool for creating little helper tools.

In Drupal 8, Drupal Console has become a compelling alternative — mainly due to its built-in code generation. Drush is still an important part of our kit, but Drupal Console has quickly become an important complement.

And it turns out, one of the things you can generate in Drupal Console is a Drupal Console command. First, create a module (if you don’t want to add the command to an existing module):

john@katmai >git/test (develop)> drupal generate:module

Enter the new module name:
> Fix Menus

 Enter the module machine name [fix_menus]:
>

 Enter the module Path [/modules/custom]:
>

 Enter module description [My Awesome Module]:
> Fix non-aliased menu link data

 Enter package name [Custom]:
>

 Enter Drupal Core version [8.x]:
>

 Do you want to generate a .module file (yes/no) [yes]:
> no

 Define module as feature (yes/no) [no]:
>

 Do you want to add a composer.json file to your module (yes/no) [yes]:
> no

 Would you like to add module dependencies (yes/no) [no]:
> no

 Do you confirm generation? (yes/no) [yes]:
>

Generated or updated files
Site path: /home/john/git/test
1 - modules/custom/fix_menus/fix_menus.info.yml
john@katmai >git/test (develop)>

Next, generate your custom command:

john@katmai >git/test (develop)> drupal generate:command
Enter the module name [ctools]:
> fix_menus

 Enter the Command name. [fix_menus:default]:
> fix_aliases

 Enter the Command Class. (Must end with the word 'Command'). [DefaultCommand]:
> FixAliasesCommand

 Is the command aware of the drupal site installation when executed?. (yes/no) [yes]:
>

 Do you confirm generation? (yes/no) [yes]:
>

Generated or updated files
Site path: /home/john/git/test
1 - modules/custom/fix_menus/src/Command/FixAliasesCommand.php
john@katmai >git/test (develop)>

 

 

Visit website 4001
Visit website 4002
Visit website 4003
Visit website 4004
Visit website 4005
Visit website 4006
Visit website 4007
Visit website 4008
Visit website 4009
Visit website 4010
Visit website 4011
Visit website 4012
Visit website 4013
Visit website 4014
Visit website 4015
Visit website 4016
Visit website 4017
Visit website 4018
Visit website 4019
Visit website 4020
Visit website 4021
Visit website 4022
Visit website 4023
Visit website 4024
Visit website 4025
Visit website 4026
Visit website 4027
Visit website 4028
Visit website 4029
Visit website 4030
Visit website 4031
Visit website 4032
Visit website 4033
Visit website 4034
Visit website 4035
Visit website 4036
Visit website 4037
Visit website 4038
Visit website 4039
Visit website 4040
Visit website 4041
Visit website 4042
Visit website 4043
Visit website 4044
Visit website 4045
Visit website 4046
Visit website 4047
Visit website 4048
Visit website 4049
Visit website 4050
Visit website 4051
Visit website 4052
Visit website 4053
Visit website 4054
Visit website 4055
Visit website 4056
Visit website 4057
Visit website 4058
Visit website 4059
Visit website 4060
Visit website 4061
Visit website 4062
Visit website 4063
Visit website 4064
Visit website 4065
Visit website 4066
Visit website 4067
Visit website 4068
Visit website 4069
Visit website 4070
Visit website 4071
Visit website 4072
Visit website 4073
Visit website 4074
Visit website 4075
Visit website 4076
Visit website 4077
Visit website 4078
Visit website 4079
Visit website 4080
Visit website 4081
Visit website 4082
Visit website 4083
Visit website 4084
Visit website 4085
Visit website 4086
Visit website 4087
Visit website 4088
Visit website 4089
Visit website 4090
Visit website 4091
Visit website 4092
Visit website 4093
Visit website 4094
Visit website 4095
Visit website 4096
Visit website 4097
Visit website 4098
Visit website 4099
Visit website 4100
Visit website 4101
Visit website 4102
Visit website 4103
Visit website 4104
Visit website 4105
Visit website 4106
Visit website 4107
Visit website 4108
Visit website 4109
Visit website 4110
Visit website 4111
Visit website 4112
Visit website 4113
Visit website 4114
Visit website 4115
Visit website 4116
Visit website 4117
Visit website 4118
Visit website 4119
Visit website 4120
Visit website 4121
Visit website 4122
Visit website 4123
Visit website 4124
Visit website 4125
Visit website 4126
Visit website 4127
Visit website 4128
Visit website 4129
Visit website 4130
Visit website 4131
Visit website 4132
Visit website 4133
Visit website 4134
Visit website 4135
Visit website 4136
Visit website 4137
Visit website 4138
Visit website 4139
Visit website 4140
Visit website 4141
Visit website 4142
Visit website 4143
Visit website 4144
Visit website 4145
Visit website 4146
Visit website 4147
Visit website 4148
Visit website 4149
Visit website 4150
Visit website 4151
Visit website 4152
Visit website 4153
Visit website 4154
Visit website 4155
Visit website 4156
Visit website 4157
Visit website 4158
Visit website 4159
Visit website 4160
Visit website 4161
Visit website 4162
Visit website 4163
Visit website 4164
Visit website 4165
Visit website 4166
Visit website 4167
Visit website 4168
Visit website 4169
Visit website 4170
Visit website 4171
Visit website 4172
Visit website 4173
Visit website 4174
Visit website 4175
Visit website 4176
Visit website 4177
Visit website 4178
Visit website 4179
Visit website 4180
Visit website 4181
Visit website 4182
Visit website 4183
Visit website 4184
Visit website 4185
Visit website 4186
Visit website 4187
Visit website 4188
Visit website 4189
Visit website 4190
Visit website 4191
Visit website 4192
Visit website 4193
Visit website 4194
Visit website 4195
Visit website 4196
Visit website 4197
Visit website 4198
Visit website 4199
Visit website 4200
Visit website 4201
Visit website 4202
Visit website 4203
Visit website 4204
Visit website 4205
Visit website 4206
Visit website 4207
Visit website 4208
Visit website 4209
Visit website 4210
Visit website 4211
Visit website 4212
Visit website 4213
Visit website 4214
Visit website 4215
Visit website 4216
Visit website 4217
Visit website 4218
Visit website 4219
Visit website 4220
Visit website 4221
Visit website 4222
Visit website 4223
Visit website 4224
Visit website 4225
Visit website 4226
Visit website 4227
Visit website 4228
Visit website 4229
Visit website 4230
Visit website 4231
Visit website 4232
Visit website 4233
Visit website 4234
Visit website 4235
Visit website 4236
Visit website 4237
Visit website 4238
Visit website 4239
Visit website 4240
Visit website 4241
Visit website 4242
Visit website 4243
Visit website 4244
Visit website 4245
Visit website 4246
Visit website 4247
Visit website 4248
Visit website 4249
Visit website 4250
Visit website 4251
Visit website 4252
Visit website 4253
Visit website 4254
Visit website 4255
Visit website 4256
Visit website 4257
Visit website 4258
Visit website 4259
Visit website 4260
Visit website 4261
Visit website 4262
Visit website 4263
Visit website 4264
Visit website 4265
Visit website 4266
Visit website 4267
Visit website 4268
Visit website 4269
Visit website 4270
Visit website 4271
Visit website 4272
Visit website 4273
Visit website 4274
Visit website 4275
Visit website 4276
Visit website 4277
Visit website 4278
Visit website 4279
Visit website 4280
Visit website 4281
Visit website 4282
Visit website 4283
Visit website 4284
Visit website 4285
Visit website 4286
Visit website 4287
Visit website 4288
Visit website 4289
Visit website 4290
Visit website 4291
Visit website 4292
Visit website 4293
Visit website 4294
Visit website 4295
Visit website 4296
Visit website 4297
Visit website 4298
Visit website 4299
Visit website 4300
Visit website 4301
Visit website 4302
Visit website 4303
Visit website 4304
Visit website 4305
Visit website 4306
Visit website 4307
Visit website 4308
Visit website 4309
Visit website 4310
Visit website 4311
Visit website 4312
Visit website 4313
Visit website 4314
Visit website 4315
Visit website 4316
Visit website 4317
Visit website 4318
Visit website 4319
Visit website 4320
Visit website 4321
Visit website 4322
Visit website 4323
Visit website 4324
Visit website 4325
Visit website 4326
Visit website 4327
Visit website 4328
Visit website 4329
Visit website 4330
Visit website 4331
Visit website 4332
Visit website 4333
Visit website 4334
Visit website 4335
Visit website 4336
Visit website 4337
Visit website 4338
Visit website 4339
Visit website 4340
Visit website 4341
Visit website 4342
Visit website 4343
Visit website 4344
Visit website 4345
Visit website 4346
Visit website 4347
Visit website 4348
Visit website 4349
Visit website 4350
Visit website 4351
Visit website 4352
Visit website 4353
Visit website 4354
Visit website 4355
Visit website 4356
Visit website 4357
Visit website 4358
Visit website 4359
Visit website 4360
Visit website 4361
Visit website 4362
Visit website 4363
Visit website 4364
Visit website 4365
Visit website 4366
Visit website 4367
Visit website 4368
Visit website 4369
Visit website 4370
Visit website 4371
Visit website 4372
Visit website 4373
Visit website 4374
Visit website 4375
Visit website 4376
Visit website 4377
Visit website 4378
Visit website 4379
Visit website 4380
Visit website 4381
Visit website 4382
Visit website 4383
Visit website 4384
Visit website 4385
Visit website 4386
Visit website 4387
Visit website 4388
Visit website 4389
Visit website 4390
Visit website 4391
Visit website 4392
Visit website 4393
Visit website 4394
Visit website 4395
Visit website 4396
Visit website 4397
Visit website 4398
Visit website 4399
Visit website 4400
Visit website 4401
Visit website 4402
Visit website 4403
Visit website 4404
Visit website 4405
Visit website 4406
Visit website 4407
Visit website 4408
Visit website 4409
Visit website 4410
Visit website 4411
Visit website 4412
Visit website 4413
Visit website 4414
Visit website 4415
Visit website 4416
Visit website 4417
Visit website 4418
Visit website 4419
Visit website 4420
Visit website 4421
Visit website 4422
Visit website 4423
Visit website 4424
Visit website 4425
Visit website 4426
Visit website 4427
Visit website 4428
Visit website 4429
Visit website 4430
Visit website 4431
Visit website 4432
Visit website 4433
Visit website 4434
Visit website 4435
Visit website 4436
Visit website 4437
Visit website 4438
Visit website 4439
Visit website 4440
Visit website 4441
Visit website 4442
Visit website 4443
Visit website 4444
Visit website 4445
Visit website 4446
Visit website 4447
Visit website 4448
Visit website 4449
Visit website 4450
Visit website 4451
Visit website 4452
Visit website 4453
Visit website 4454
Visit website 4455
Visit website 4456
Visit website 4457
Visit website 4458
Visit website 4459
Visit website 4460
Visit website 4461
Visit website 4462
Visit website 4463
Visit website 4464
Visit website 4465
Visit website 4466
Visit website 4467
Visit website 4468
Visit website 4469
Visit website 4470
Visit website 4471
Visit website 4472
Visit website 4473
Visit website 4474
Visit website 4475
Visit website 4476
Visit website 4477
Visit website 4478
Visit website 4479
Visit website 4480
Visit website 4481
Visit website 4482
Visit website 4483
Visit website 4484
Visit website 4485
Visit website 4486
Visit website 4487
Visit website 4488
Visit website 4489
Visit website 4490
Visit website 4491
Visit website 4492
Visit website 4493
Visit website 4494
Visit website 4495
Visit website 4496
Visit website 4497
Visit website 4498
Visit website 4499
Visit website 4500

Related Articles

Leave a Reply

Back to top button