Replace all instances of the global function drupal_set_message() with MessengerInterface::addMessage()
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | 2974051-20.patch | 2.43 KB | mahtab_alam |
| #18 | 2974051-18.patch | 1.53 KB | mahtab_alam |
| #17 | marketing_cloud-messages_dep_inj-2974051-17.patch | 3.1 KB | john_a |
| #10 | marketing_cloud-messages_dep_inj-2974051-10.patch | 22.49 KB | john_a |
| #5 | marketing_cloud-drupal_set_message-2974051-5.patch | 367 bytes | john_a |
Comments
Comment #2
john_a commentedComment #3
john_a commentedComment #4
john_a commentedI want to throw this out there, to see what others think. I've been reviewing how other contributors have been addressing this issue, considering we do not want to release this until at least 8.5.0 is released.
My idea at the moment, is to temporarily place the following in the main info.yml file:
Postponing this patch, and as soon as 8.5 is released, begin work on the drupal_set_messages patch, that will include:
If possible, it would be preferable to not have to support multiple branches for legacy core systems, especially since this is a new module.
Any thoughts? We have a few months until crunch time on this, anyway
Comment #5
john_a commentedPatch created to ensure initial safeguard for core version and deprecated drupal_set_message().
Comment #6
john_a commentedComment #7
john_a commentedIn retrospect, I am actually going to make a start on this patch. Since 8.5 is already out (so messenger service is already in core) and we should be ready for full release very soon, it makes sense that we 8.5.0 should be our starting point. This relieves pressure from having separate releases for < 8.4.
@banhu, I know you're keen and want to learn about this. So reviewing the patch may be a good introduction to it.
Comment #8
john_a commentedComment #9
john_a commentedComment #10
john_a commentedPatch created, can someone please review.
Comment #11
john_a commentedComment #12
john_a commentedreviewed and tested by self. patch applies, all tests pass.
accidentally committed this with the incorrect message, re. the previous commit (forgot to add the tests for marketing_cloud_example)
Comment #14
john_a commentedRe-opening this issue, found a couple of drupal_set_message in MarketingCloudExample->sdubmit_form().
Comment #15
john_a commentedPatch created, please review.
Also added more explanation comments to the response handling.
Comment #16
bhanuprakashnani commentedHi John. Sorry I was busy with GSoC. And unfortunately, I failed the phase 1. I will be able to work on this module. I will review the patch and learn how the changes were made. Please tell me what is the next step to be done now. The patch is not seen in comment #15. Please send it again. I will see it.
Comment #17
john_a commentedI swear I had uploaded the patch. Oh well, here it is...
The main steps to take here, are validate that the patch installs, that unit tests still work, are there any unit tests that we can add that will validate the changes in this patch (I don't think there are), and code quality validation - is there a more elegant way rather than using MessengerTrait (I don't think there is unless we consider overriding the constructor, which seems a little overkill for this).
Comment #18
mahtab_alam commentedComment #19
john_a commentedHi @mahtab_alam, many thanks for your patch. Unfortunately, procedural code like yours (and my code that I'm replacing) is being discouraged in D8. So I can't use this (even though your code is good, terse and easy to read). See:
From www.drupal.org/node/2774931:
Many thanks for your input!
Comment #20
mahtab_alam commentedComment #21
john_a commentedHi @mahtab_alam,
Patch failed QA. It is unable to the class extending WebformHandlerBase, which has a large constructor, and then does not call the parent constructor:
In addition, the constructor is sent the large number of injected dependencies that WebformHandlerBase expects, but only MessengerInterface is defined in the contract for MarketingCloudExample:
I had a play with this, and got as far as:
Which gave me:
At that point, I stopped. Looking into other webform handler extensions such EmailWebformHandler, things get quite confusing and will likely require a complete refactoring of the class and defining new parent classes and/or interfaces.
The simplest method may be to stick to patch #17 that just uses a trait to inject the messenger service.
Thoughts, anyone?
Comment #22
mahtab_alam commentedYes patch #17 is fine. I have read somewhere.
A decision tree for when to work with traits
1) If it is a service you are creating then avoid the trait and directly inject into the constructor
2) If it is a small controller method, and it is appropriate to extend ControllerBase, then look for the trait in the base class
3) If what you are writing can be considered application code, then use traits
4) Testing can become complicated. so if you need 100% coverage then consider converting a trait into DI logic.
Hope this help.
Comment #23
mahtab_alam commentedFor patch #17 how can we call $this->logger without calling use LoggerTrait;
Comment #24
john_a commentedHi @mahtab_alam,
Yes I had read that somewhere too, quite a good decision tree!
If you look at WebformHandlerBase, they already have a logger injected, which was why I was able to use $this->logger. However they have not injected the messenger service, and are still using drupal_set_message() in the extra handlers like the email webform handler that they provide out of the box.
So using $this->logger fixes the same trap I had fallen into, using \Drupal::logger.
IMO, since this is just an example module to show how to use the services, it should be as simple as possible so that devs can read the code and see exactly what they have to do without having to wade through convoluted code just so that we can inject the messenger service.
Many thanks!
Comment #25
john_a commentedSo decided then, applying patch #17. Many thanks for you input and suggestions @mahtab_alam!
Comment #27
john_a commented