Closed (fixed)
Project:
Examples for Developers
Version:
8.x-1.x-dev
Component:
Other
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
3 Oct 2016 at 19:02 UTC
Updated:
17 Dec 2016 at 02:34 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
eojthebraveHere's a first pass at updating this example. This contains the following:
Comment #3
blakehall commentedI think these changes make a lot of sense. This patch gets a hearty +1 from me.
Comment #4
eojthebraveI just used this code as the basis for some tutorials for Drupalize.Me and made a few more tweaks to this in the process. Cleaning up some coding standards violations and, and adding a little more clarity to a few comments. Here's the updated patch.
Comment #5
mile23A lot of good changes. Some changes that add to complexity, but I think it's probably worth it.
Thanks!
Some things I found:
Clearly we need to insert annotation classname here. -)
The class name should be on the next line if it doesn't fit in 80 chars.
Either this is commented code, which is bad, or it's example code which should be properly indented and have a @code/@endcode tag.
Wrap the URL, add a @see annotation as well.
Indentation and an incomplete sentence.
To require that the method be implemented by subclasses, we'd declare it abstract on the abstract base class.
The way this is set up, it doesn't actually implement the interface it says it does, which breaks for the wrong reason.
So add
abstract public function order()with {@inheritdoc} in the docblock.We don't do @file for 'provides' any more. This docblock should comment on the interface declaration, not the file.
Document what this method does.
Use ::class for these class names, rather than string literals.
Comment #6
eojthebraveThanks for the review. I can make all those changes no problem except the last one. I guess just don't understand what you're asking for. Could you explain what you mean with this?
Use ::class for these class names, rather than string literals.
Comment #7
mile23In PHP 5.5+ you can do the following, and the output will be the fully-qualified class name:
This is better because it means you have a more useful error if the interface is removed or gets moved around. It also helps IDEs.
Thanks.
Comment #8
eojthebraveThanks for the explanation. I had no idea you could do that. I've updated the patch to hopefully help address all of your feedback.
Comment #9
eojthebraveOops. Forgot one things. This now gets rid of the @file docblock in SandwichInterface.php as well.
Comment #10
mile23Seems like out of scope changes... You might need to pull the repo again.
Comment #11
eojthebraveBah. Thanks for catching that. Here's an update. Same patch as above, just without all the extra changes to things outside the plugin_type_example/ directory.
Comment #12
eojthebraveComment #13
mile23Here's a patch with some more cleanup for CS.
Also, I just noticed that meatball sandwiches are different on Sundays, so I added a test, which breaks. :-) There's no t() on the plugin class, so either we have to demonstrate injecting services to the plugin, or we have to not translate the string. I'm not sure if the plugin system will do the translation on the annotation values, the way we have it set up.
Comment #15
eojthebraveThanks for helping with the cleanup. I feel like I'm getting to the point with this code where I'm missing things because I've been over it so many times.
This patch adds
StringTranslationTraitto theExampleHamSandwichplugin. Which fixes the use of $this->t() in the order() method. And the tests, including your fancy new Sunday test, are passing for me.If you're referring to the use of @Translation("string") in the plugin annotations, then yes the plugin system will do the translation of whatever the value is inside of the @Translation() block in the annotation. See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Annotatio... for documentation, and https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Annotatio... for implementation.
Should we maybe add an
@see plugin_translatablesomewhere if this is a potential source of confusion?Comment #16
mile23That works because
\Drupalworks, but it's a bit of a pet peeve of mine... #2733703: [plan] Service traits should require IoC injectionHowever, it turns out we can use a proper IoC dependency injection pattern with plugin objects: https://www.drupal.org/node/2012118
So here's a version with IoC injection.
Comment #17
eojthebraveThat works for me. I actually like that as a result this also ends up demonstrating how you can inject not just translation but also any service you might need. Which is probably something that's likely to come up for a lot of people.
Comment #19
mile23OK, since we're basically RTBCing each other.... I'll just commit and mark fixed. :-)
Add a new issue for follow-ups.
Thanks!