Variables.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Variable\Model\Source;
  7. /**
  8. * Store Contact Information source model.
  9. */
  10. class Variables implements \Magento\Framework\Option\ArrayInterface
  11. {
  12. /**
  13. * Variable types
  14. */
  15. const DEFAULT_VARIABLE_TYPE = "default";
  16. const CUSTOM_VARIABLE_TYPE = "custom";
  17. /**
  18. * Assoc array of configuration variables.
  19. *
  20. * @var array
  21. */
  22. private $configVariables = [];
  23. /**
  24. * @var array
  25. */
  26. private $configPaths = [];
  27. /**
  28. * @var \Magento\Config\Model\Config\Structure\SearchInterface
  29. */
  30. private $configStructure;
  31. /**
  32. * Constructor.
  33. *
  34. * @param \Magento\Config\Model\Config\Structure\SearchInterface $configStructure
  35. * @param array $configPaths
  36. */
  37. public function __construct(
  38. \Magento\Config\Model\Config\Structure\SearchInterface $configStructure,
  39. array $configPaths = []
  40. ) {
  41. $this->configStructure = $configStructure;
  42. $this->configPaths = $configPaths;
  43. }
  44. /**
  45. * Retrieve option array of store contact variables.
  46. *
  47. * @param bool $withGroup
  48. * @return array
  49. */
  50. public function toOptionArray($withGroup = false)
  51. {
  52. $optionArray = [];
  53. if ($withGroup) {
  54. foreach ($this->getConfigVariables() as $configVariableGroup) {
  55. $group = [
  56. 'label' => $configVariableGroup['label']
  57. ];
  58. $groupElements = [];
  59. foreach ($configVariableGroup['elements'] as $element) {
  60. $groupElements[] = [
  61. 'value' => '{{config path="' . $element['value'] . '"}}',
  62. 'label' => $element['label'],
  63. ];
  64. }
  65. $group['value'] = $groupElements;
  66. $optionArray[] = $group;
  67. }
  68. } else {
  69. foreach ($this->getConfigVariables() as $configVariableGroup) {
  70. foreach ($configVariableGroup['elements'] as $element) {
  71. $optionArray[] = [
  72. 'value' => '{{config path="' . $element['value'] . '"}}',
  73. 'label' => $element['label'],
  74. ];
  75. }
  76. }
  77. }
  78. return $optionArray;
  79. }
  80. /**
  81. * Return available config variables.
  82. *
  83. * @return array
  84. * @codeCoverageIgnore
  85. */
  86. public function getData()
  87. {
  88. return $this->getFlatConfigVars();
  89. }
  90. /**
  91. * Get flattened config variables.
  92. *
  93. * @return array
  94. */
  95. private function getFlatConfigVars()
  96. {
  97. $result = [];
  98. foreach ($this->getConfigVariables() as $configVariableGroup) {
  99. foreach ($configVariableGroup['elements'] as $element) {
  100. $element['group_label'] = $configVariableGroup['label'];
  101. $result[] = $element;
  102. }
  103. }
  104. return $result;
  105. }
  106. /**
  107. * Merge config with user defined data
  108. *
  109. * @return array
  110. */
  111. private function getConfigVariables()
  112. {
  113. if (empty($this->configVariables)) {
  114. foreach ($this->configPaths as $groupPath => $groupElements) {
  115. $groupPathElements = explode('/', $groupPath);
  116. $path = [];
  117. $labels = [];
  118. foreach ($groupPathElements as $groupPathElement) {
  119. $path[] = $groupPathElement;
  120. $labels[] = __(
  121. $this->configStructure->getElementByConfigPath(implode('/', $path))->getLabel()
  122. );
  123. }
  124. $this->configVariables[$groupPath]['label'] = implode(' / ', $labels);
  125. foreach (array_keys($groupElements) as $elementPath) {
  126. $this->configVariables[$groupPath]['elements'][] = [
  127. 'value' => $elementPath,
  128. 'label' => __($this->configStructure->getElementByConfigPath($elementPath)->getLabel()),
  129. ];
  130. }
  131. }
  132. }
  133. return $this->configVariables;
  134. }
  135. }