Group.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Structure\Element;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Group extends AbstractComposite
  12. {
  13. /**
  14. * Group clone model factory
  15. *
  16. * @var \Magento\Config\Model\Config\BackendClone\Factory
  17. */
  18. protected $_cloneModelFactory;
  19. /**
  20. *
  21. * @var \Magento\Config\Model\Config\Structure\Element\Dependency\Mapper
  22. */
  23. protected $_dependencyMapper;
  24. /**
  25. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  26. * @param \Magento\Framework\Module\Manager $moduleManager
  27. * @param Iterator\Field $childrenIterator
  28. * @param \Magento\Config\Model\Config\BackendClone\Factory $cloneModelFactory
  29. * @param Dependency\Mapper $dependencyMapper
  30. */
  31. public function __construct(
  32. \Magento\Store\Model\StoreManagerInterface $storeManager,
  33. \Magento\Framework\Module\Manager $moduleManager,
  34. \Magento\Config\Model\Config\Structure\Element\Iterator\Field $childrenIterator,
  35. \Magento\Config\Model\Config\BackendClone\Factory $cloneModelFactory,
  36. \Magento\Config\Model\Config\Structure\Element\Dependency\Mapper $dependencyMapper
  37. ) {
  38. parent::__construct($storeManager, $moduleManager, $childrenIterator);
  39. $this->_cloneModelFactory = $cloneModelFactory;
  40. $this->_dependencyMapper = $dependencyMapper;
  41. }
  42. /**
  43. * Should group fields be cloned
  44. *
  45. * @return bool
  46. */
  47. public function shouldCloneFields()
  48. {
  49. return isset($this->_data['clone_fields']) && !empty($this->_data['clone_fields']);
  50. }
  51. /**
  52. * Retrieve clone model
  53. *
  54. * @return \Magento\Framework\Model\AbstractModel
  55. * @throws \Magento\Framework\Exception\LocalizedException
  56. */
  57. public function getCloneModel()
  58. {
  59. if (!isset($this->_data['clone_model']) || !$this->_data['clone_model']) {
  60. throw new \Magento\Framework\Exception\LocalizedException(
  61. __('Config form fieldset clone model required to be able to clone fields')
  62. );
  63. }
  64. return $this->_cloneModelFactory->create($this->_data['clone_model']);
  65. }
  66. /**
  67. * Populate form fieldset with group data
  68. *
  69. * @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset
  70. * @return void
  71. */
  72. public function populateFieldset(\Magento\Framework\Data\Form\Element\Fieldset $fieldset)
  73. {
  74. $originalData = [];
  75. foreach ($this->_data as $key => $value) {
  76. if (!is_array($value)) {
  77. $originalData[$key] = $value;
  78. }
  79. }
  80. $fieldset->setOriginalData($originalData);
  81. }
  82. /**
  83. * Check whether group should be expanded
  84. *
  85. * @return bool
  86. */
  87. public function isExpanded()
  88. {
  89. return (bool)(isset($this->_data['expanded']) ? (int)$this->_data['expanded'] : false);
  90. }
  91. /**
  92. * Retrieve group fieldset css
  93. *
  94. * @return string
  95. */
  96. public function getFieldsetCss()
  97. {
  98. return array_key_exists('fieldset_css', $this->_data) ? $this->_data['fieldset_css'] : '';
  99. }
  100. /**
  101. * Retrieve field dependencies
  102. *
  103. * @param string $storeCode
  104. * @return array
  105. */
  106. public function getDependencies($storeCode)
  107. {
  108. $dependencies = [];
  109. if (false == isset($this->_data['depends']['fields'])) {
  110. return $dependencies;
  111. }
  112. $dependencies = $this->_dependencyMapper->getDependencies($this->_data['depends']['fields'], $storeCode);
  113. return $dependencies;
  114. }
  115. }