Guest User

Untitled

a guest
Dec 11th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\mymodule\Plugin\Block;
  4.  
  5. use Drupal\commerce_cart\CartProviderInterface;
  6. use Drupal\commerce_checkout\CheckoutOrderManagerInterface;
  7. use Drupal\commerce_checkout\Plugin\Block\CheckoutProgressBlock as BaseBlock;
  8. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  9. use Drupal\Core\Routing\RouteMatchInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11.  
  12. /**
  13. * Provides a checkout progress block.
  14. *
  15. * @Block(
  16. * id = "mymodule_commerce_checkout_progress",
  17. * admin_label = @Translation("Desmazieres - Checkout progress"),
  18. * category = @Translation("Commerce")
  19. * )
  20. */
  21. class CheckoutProgressBlock extends BaseBlock implements ContainerFactoryPluginInterface {
  22.  
  23. /**
  24. * The cart provider.
  25. *
  26. * @var \Drupal\commerce_cart\CartProviderInterface
  27. */
  28. protected $cartProvider;
  29.  
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutOrderManagerInterface $checkout_order_manager, RouteMatchInterface $route_match, CartProviderInterface $cart_provider) {
  34. parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_order_manager, $route_match);
  35.  
  36. $this->cartProvider = $cart_provider;
  37. }
  38.  
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  43. return new static(
  44. $configuration,
  45. $plugin_id,
  46. $plugin_definition,
  47. $container->get('commerce_checkout.checkout_order_manager'),
  48. $container->get('current_route_match'),
  49. $container->get('commerce_cart.cart_provider')
  50. );
  51. }
  52.  
  53. /**
  54. * Builds the checkout progress block.
  55. *
  56. * @return array
  57. * A render array.
  58. *
  59. * @see \Drupal\commerce_cart\Plugin\Block\CartBlock::build();
  60. */
  61. public function build() {
  62. // Try to build regularly.
  63. $build = parent::build();
  64.  
  65. // If nothing found, try to get the current Cart and rebuild.
  66. if (empty($build) && $this->routeMatch->getRouteName() == 'commerce_cart.page') {
  67. $carts = $this->cartProvider->getCarts();
  68. $carts = array_filter($carts, function ($cart) {
  69. return $cart->hasItems() && $cart->cart->value;
  70. });
  71.  
  72. if (!empty($carts)) {
  73. $order = reset($carts);
  74. $build = parent::render($order);
  75. }
  76. }
  77.  
  78. return $build;
  79. }
  80. }
Add Comment
Please, Sign In to add comment