AbstractContainer.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Block\Info;
  7. /**
  8. * Payment information container block
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. abstract class AbstractContainer extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Payment data
  17. *
  18. * @var \Magento\Payment\Helper\Data
  19. */
  20. protected $_paymentData = null;
  21. /**
  22. * @param \Magento\Framework\View\Element\Template\Context $context
  23. * @param \Magento\Payment\Helper\Data $paymentData
  24. * @param array $data
  25. */
  26. public function __construct(
  27. \Magento\Framework\View\Element\Template\Context $context,
  28. \Magento\Payment\Helper\Data $paymentData,
  29. array $data = []
  30. ) {
  31. $this->_paymentData = $paymentData;
  32. parent::__construct($context, $data);
  33. }
  34. /**
  35. * Add payment info block to layout
  36. *
  37. * @return $this
  38. */
  39. protected function _prepareLayout()
  40. {
  41. if ($info = $this->getPaymentInfo()) {
  42. $this->setChild($this->_getInfoBlockName(), $this->_paymentData->getInfoBlock($info, $this->getLayout()));
  43. }
  44. return parent::_prepareLayout();
  45. }
  46. /**
  47. * Retrieve info block name
  48. *
  49. * @return string|false
  50. */
  51. protected function _getInfoBlockName()
  52. {
  53. if ($info = $this->getPaymentInfo()) {
  54. return 'payment.info.' . $info->getMethodInstance()->getCode();
  55. }
  56. return false;
  57. }
  58. /**
  59. * Retrieve payment info model
  60. *
  61. * @return \Magento\Payment\Model\Info|false
  62. */
  63. abstract public function getPaymentInfo();
  64. /**
  65. * Declare info block template
  66. *
  67. * @param string $method
  68. * @param string $template
  69. * @return $this
  70. */
  71. public function setInfoTemplate($method = '', $template = '')
  72. {
  73. if ($info = $this->getPaymentInfo()) {
  74. if ($info->getMethodInstance()->getCode() == $method) {
  75. $this->getChildBlock($this->_getInfoBlockName())->setTemplate($template);
  76. }
  77. }
  78. return $this;
  79. }
  80. }