ShortcutButtons.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Block;
  7. use Magento\Framework\View\Element\Template;
  8. /**
  9. * Shortcuts container
  10. *
  11. * Accepts shortcuts on shortcut_buttons_container event and render shortcuts using custom order
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class ShortcutButtons extends Template
  17. {
  18. /**#@+
  19. * Position of "OR" label against shortcut
  20. */
  21. const POSITION_BEFORE = 'before';
  22. const POSITION_AFTER = 'after';
  23. /**#@-*/
  24. /**#@-*/
  25. protected $_shortcuts = [];
  26. /**
  27. * @var bool
  28. */
  29. protected $_isCatalogProduct;
  30. /**
  31. * @var null|string
  32. */
  33. protected $_orPosition;
  34. /**
  35. * @param Template\Context $context
  36. * @param bool $isCatalogProduct
  37. * @param null|string $orPosition
  38. * @param array $data
  39. */
  40. public function __construct(
  41. Template\Context $context,
  42. $isCatalogProduct = false,
  43. $orPosition = null,
  44. array $data = []
  45. ) {
  46. parent::__construct($context, $data);
  47. $this->_isCatalogProduct = $isCatalogProduct;
  48. $this->_orPosition = $orPosition ?: ($isCatalogProduct ? self::POSITION_BEFORE : self::POSITION_AFTER);
  49. }
  50. /**
  51. * Add shortcut button
  52. *
  53. * @param Template $block
  54. * @return void
  55. */
  56. public function addShortcut(Template $block)
  57. {
  58. if ($block instanceof ShortcutInterface) {
  59. $this->_shortcuts[] = $block;
  60. }
  61. }
  62. /**
  63. * Dispatch shortcuts container event
  64. * @return $this
  65. */
  66. protected function _beforeToHtml()
  67. {
  68. $this->_eventManager->dispatch(
  69. 'shortcut_buttons_container',
  70. [
  71. 'container' => $this,
  72. 'is_catalog_product' => $this->_isCatalogProduct,
  73. 'or_position' => $this->_orPosition
  74. ]
  75. );
  76. return $this;
  77. }
  78. /**
  79. * Render all shortcuts
  80. *
  81. * @return string
  82. */
  83. protected function _toHtml()
  84. {
  85. /** @var ShortcutInterface $shortcut */
  86. foreach ($this->_shortcuts as $shortcut) {
  87. $this->setChild($shortcut->getAlias(), $shortcut);
  88. }
  89. return $this->getChildHtml();
  90. }
  91. }