Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Variables.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  const DEFAULT_VARIABLE_TYPE = "default";
17  const CUSTOM_VARIABLE_TYPE = "custom";
18 
24  private $configVariables = [];
25 
29  private $configPaths = [];
30 
34  private $configStructure;
35 
42  public function __construct(
43  \Magento\Config\Model\Config\Structure\SearchInterface $configStructure,
44  array $configPaths = []
45  ) {
46  $this->configStructure = $configStructure;
47  $this->configPaths = $configPaths;
48  }
49 
56  public function toOptionArray($withGroup = false)
57  {
58  $optionArray = [];
59  if ($withGroup) {
60  foreach ($this->getConfigVariables() as $configVariableGroup) {
61  $group = [
62  'label' => $configVariableGroup['label']
63  ];
64  $groupElements = [];
65  foreach ($configVariableGroup['elements'] as $element) {
66  $groupElements[] = [
67  'value' => '{{config path="' . $element['value'] . '"}}',
68  'label' => $element['label'],
69  ];
70  }
71  $group['value'] = $groupElements;
72  $optionArray[] = $group;
73  }
74  } else {
75  foreach ($this->getConfigVariables() as $configVariableGroup) {
76  foreach ($configVariableGroup['elements'] as $element) {
77  $optionArray[] = [
78  'value' => '{{config path="' . $element['value'] . '"}}',
79  'label' => $element['label'],
80  ];
81  }
82  }
83  }
84  return $optionArray;
85  }
86 
93  public function getData()
94  {
95  return $this->getFlatConfigVars();
96  }
97 
103  private function getFlatConfigVars()
104  {
105  $result = [];
106  foreach ($this->getConfigVariables() as $configVariableGroup) {
107  foreach ($configVariableGroup['elements'] as $element) {
108  $element['group_label'] = $configVariableGroup['label'];
109  $result[] = $element;
110  }
111  }
112  return $result;
113  }
114 
120  private function getConfigVariables()
121  {
122  if (empty($this->configVariables)) {
123  foreach ($this->configPaths as $groupPath => $groupElements) {
124  $groupPathElements = explode('/', $groupPath);
125  $path = [];
126  $labels = [];
127  foreach ($groupPathElements as $groupPathElement) {
128  $path[] = $groupPathElement;
129  $labels[] = __(
130  $this->configStructure->getElementByConfigPath(implode('/', $path))->getLabel()
131  );
132  }
133  $this->configVariables[$groupPath]['label'] = implode(' / ', $labels);
134  foreach (array_keys($groupElements) as $elementPath) {
135  $this->configVariables[$groupPath]['elements'][] = [
136  'value' => $elementPath,
137  'label' => __($this->configStructure->getElementByConfigPath($elementPath)->getLabel()),
138  ];
139  }
140  }
141  }
142 
143  return $this->configVariables;
144  }
145 }
__construct(\Magento\Config\Model\Config\Structure\SearchInterface $configStructure, array $configPaths=[])
Definition: Variables.php:42
$group
Definition: sections.phtml:16
__()
Definition: __.php:13
$element
Definition: element.phtml:12