CountryCreditCard.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Block\Adminhtml\Form\Field;
  7. use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
  8. use Magento\Framework\DataObject;
  9. /**
  10. * Class CountryCreditCard
  11. */
  12. class CountryCreditCard extends AbstractFieldArray
  13. {
  14. /**
  15. * @var Countries
  16. */
  17. protected $countryRenderer = null;
  18. /**
  19. * @var CcTypes
  20. */
  21. protected $ccTypesRenderer = null;
  22. /**
  23. * Returns renderer for country element
  24. *
  25. * @return Countries
  26. */
  27. protected function getCountryRenderer()
  28. {
  29. if (!$this->countryRenderer) {
  30. $this->countryRenderer = $this->getLayout()->createBlock(
  31. Countries::class,
  32. '',
  33. ['data' => ['is_render_to_js_template' => true]]
  34. );
  35. }
  36. return $this->countryRenderer;
  37. }
  38. /**
  39. * Returns renderer for country element
  40. *
  41. * @return CcTypes
  42. */
  43. protected function getCcTypesRenderer()
  44. {
  45. if (!$this->ccTypesRenderer) {
  46. $this->ccTypesRenderer = $this->getLayout()->createBlock(
  47. CcTypes::class,
  48. '',
  49. ['data' => ['is_render_to_js_template' => true]]
  50. );
  51. }
  52. return $this->ccTypesRenderer;
  53. }
  54. /**
  55. * Prepare to render
  56. * @return void
  57. */
  58. protected function _prepareToRender()
  59. {
  60. $this->addColumn(
  61. 'country_id',
  62. [
  63. 'label' => __('Country'),
  64. 'renderer' => $this->getCountryRenderer(),
  65. ]
  66. );
  67. $this->addColumn(
  68. 'cc_types',
  69. [
  70. 'label' => __('Allowed Credit Card Types'),
  71. 'renderer' => $this->getCcTypesRenderer(),
  72. ]
  73. );
  74. $this->_addAfter = false;
  75. $this->_addButtonLabel = __('Add Rule');
  76. }
  77. /**
  78. * Prepare existing row data object
  79. *
  80. * @param DataObject $row
  81. * @return void
  82. */
  83. protected function _prepareArrayRow(DataObject $row)
  84. {
  85. $country = $row->getCountryId();
  86. $options = [];
  87. if ($country) {
  88. $options['option_' . $this->getCountryRenderer()->calcOptionHash($country)]
  89. = 'selected="selected"';
  90. $ccTypes = $row->getCcTypes();
  91. foreach ($ccTypes as $cardType) {
  92. $options['option_' . $this->getCcTypesRenderer()->calcOptionHash($cardType)]
  93. = 'selected="selected"';
  94. }
  95. }
  96. $row->setData('option_extra_attrs', $options);
  97. }
  98. }