-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make custom store address formatting possible #10850
Make custom store address formatting possible #10850
Conversation
* Interface RendererInterface | ||
* @package Magento\Store\Model\Address | ||
*/ | ||
interface RendererInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such interface should have been named something like AddressRendererInterface
or AddressFormatterInterface
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did considered this but because the interface is in the namespace Magento\Store\Model\Address it seemed unnecessary to add Address to the name. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rule of thumb is (imho) - name should be verbose enough for understanding without aliasing. We already have generic RendererInterface
which is fine as it just renders element. But here renderer is more specific.
Another example - Collection
lying inside Product
folder is not a good name, ProductCollection
is much better. But there is no need to duplicate context in method names, $address->getId()
is just fine, $address->getAddressId()
looks inelegantly.
Anyway, this is just a side note, I believe we don't need a new interface introduced here.
I don't see any difference between existing and proposed implementation from customization perspective. One can easily swap
In fact it's easy in more than one way, using custom However, what I really don't like in current implementation is template hardcoded in |
@orlangur, I tried a plugin to add my own template but I don't like that solution. I could use an around or after plugin but in both cases it's unnecessary to run the original code because I'll change the output anyway. I agree, the template should not be hard coded in the first place. It would be nice if you could define the template in configuration. This is possible for the billing and shipping address. |
To me it seems weird to introduce interfaces for such cases, as it means that we will introduce an interface for every single class injected.
It is not BC policy compliant in fact as it could break existing customizations, please check http://devdocs.magento.com/guides/v2.1/contributor-guide/backward-compatible-development/index.html#modifying-the-method-argument-type.
My suggestion is as simple as
I didn't have a chance to check billing/shipping implementation, please make address renderer consistent with them. |
@orlangur , I'll have a look or I can make a different PR with the configurable template. Thanks for all the feedback. |
You welcome 👍 For the same issue it's better to continue in this PR, just force push the necessary changes. |
Hi @jokeputs I had the same issue the other day when trying to replace another part of Magento that did not use an interface. I would love to see this updated like you have done to include an interface and thus make replacement easier. @orlangur maybe I am missing something but I cannot see how this would actually break BC as long as the interface defines all the public methods that are in the implementation it should be ok. If someone has added a preference to the implementation they would have to extend it anyway and so would automatically also implement the interface. I could easily be missing something here though. |
Ah now I see it @orlangur if someone has extended
Gives you: |
You can replace the whole class (a.k.a. class rewrite), you can inject another implementation as an argument for this particular class - I don't see how interface makes the life easier. |
For me at least having the interface makes it "easier" (maybe easier is the wrong word here but I am not sure what the right word is) as you can stick with composition rather than inheritance. If you do not have the interface then you new version must extend to original even if you do not need anything from the original. Some cases this will be no issue and might even be the desired option but there are others that it is not needed. The example I was working on the other day was to completely replace the totalscollector. Our system needed to work with totals in it's own way. But since the collector is called via the implementation rather than the interface my class needed to extend the original, even if the task meant that I needed 0% of the original class. Again not sure if this could be described as "easier" but for me it just sits better with the way I develop. Honestly both options work and are fine, I just prefer the interface approach. |
@dmanners thanks, I see your point. As to me super-abstract classes like With current implementation to make it more clear that you only use class interface and not implementation one can replace ALL parent methods with empty stubs. For the problem described approach I proposed looks much cleaner to me and does not require new interface introduction. For the general case, where do you think should be the line when we stop to convert each class into interface in constructor? Or more interfaces = the better and at some point we will rely only on interfaces? |
@jokeputs are you going to perform suggested changes? |
@orlangur, I will but I haven't had the time yet. |
@jokeputs sure thing, there is no hurry, sorry for bothering. |
@orlangur, I've updated my pull request. The address template now gets injected. |
@@ -41,7 +41,11 @@ protected function setUp() | |||
return implode("\n", $data['variables']); | |||
}); | |||
|
|||
$this->model = new Renderer($eventManager, $filterManager); | |||
$template = "{{var name}}\n{{var street_line1}}\n{{depend street_line2}}{{var street_line2}}\n{{/depend}}" . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have it as a DEFAULT_TEMPLATE
constant in original class maybe? And then just specify it as a default value of $template
argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the pull request to include the constant.
Hello @jokeputs, internal builds passed successfully, please remove the |
Thanks! Could you make branch history clear now? Something similar to #11117 (comment) should do the trick. |
@orlangur, I'm trying to follow your instruction but I'm not sure how I should get the squashed commit on my own branch. In #11117 the change is committed in the develop branch but this pull request is from my own branch |
@jokeputs just |
@orlangur,
https://github.com/magento/magento2/ is called upstream with me. When I run the command I get the error Any ideas? |
That's correct as
Yeah, that was what I was asking for. Not on Ideally it would be nice to change target of this PR to |
There is no need to close PR to change target branch. You can leave the one commit branch targeting |
My source branch Anyway, I have a clean branch now with the one commit. Should I just create a new pull request for that one? Or can I somehow change this pull request? |
Just push clean branch as
Hm, didn't know that as well, I usually use force push instead of branch deletion. |
Hm, that doesn't seem to work. I've got a "new" |
True, https://github.com/jokeputs/magento2/tree/store-address-renderer-interface is there but it does not satisfy GitHub. Please post a new PR targeting |
Ok, will do. Thanks for all the help! |
Description
It's not possible to change the format of the store address used in for example the footer of an e-mail template. At least not in a clean way. If the address renderer would be an interface it would make it possible to create a custom renderer and inject it using a preference.
This is not a bug in Magento but it would make customisation easier.