- Issue created by @Anybody
- 🇩🇪Germany Anybody Porta Westfalica
(PS: I think this is the only case where this is needed and makes sense in commerce currently.)
- 🇮🇱Israel jsacksick
hm... I'm unclear on how you'd be passing the context array? Do you have code for embedding the block?
What is preventing you from overridding CartBlock::build() with your own logic?
Or can't a hook_preprocess_HOOK() help here? Not sure exactly why a change in Commerce core would be required for this? Since you have your own custom logic / usecase to support, it kind of makes sense to override the logic you need from your own codebase? - 🇩🇪Germany Anybody Porta Westfalica
Thanks for the quick reply @jsacksick!
So you suggest for example adding a custom cart block that extends the Commerce CartBlock and overrides
build()
?
That's not a bad strategy, thank you! I didn't think about that!hook_preprocess_HOOK()
doesn't help in this case, I think, if we're both talking about preprocessing commerce_cart_block, e.g.hook_preprocess_commerce_cart_block($variables) { ... }
as there's no information about the context / block in there.
But creating a simple custom extending block is a genius idea! We could still proceed here, providing context information, if the core issue is fixed, but I think it's not worth to wait for it. The benefit then would be having the context information available in twig without writing such custom code.
Thank you!!
- 🇮🇱Israel jsacksick
I don't think you have to define a custom block, that's one way to do it but you can also swap the block class with your own.
Using a hook_block_alter().
function hook_block_alter(&$definitions) {
You can set your own class like the following:
$definitions['commerce_cart']['class'] = MyCartBlock::class;
- 🇩🇪Germany Anybody Porta Westfalica
Thanks @jsacksick! Extending the class with a custom block seems even more flexible and clearer!
Your advice helped a lot!