Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 
9 
16 class Config
17 {
21  protected $_assetRepo;
22 
26  protected $_url;
27 
31  private $collectionFactory;
32 
36  private $storesVariables;
37 
41  private $encoder;
42 
51  public function __construct(
52  \Magento\Framework\View\Asset\Repository $assetRepo,
53  \Magento\Backend\Model\UrlInterface $url,
54  \Magento\Variable\Model\ResourceModel\Variable\CollectionFactory $collectionFactory = null,
55  \Magento\Variable\Model\Source\Variables $storesVariables = null,
56  \Magento\Framework\Serialize\Serializer\Json $encoder = null
57  ) {
58  $this->collectionFactory = $collectionFactory ?: ObjectManager::getInstance()
59  ->get(\Magento\Variable\Model\ResourceModel\Variable\CollectionFactory::class);
60  $this->storesVariables = $storesVariables ?: ObjectManager::getInstance()
61  ->get(\Magento\Variable\Model\Source\Variables::class);
62  $this->encoder = $encoder ?: ObjectManager::getInstance()
63  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
64  $this->_url = $url;
65  $this->_assetRepo = $assetRepo;
66  }
67 
75  {
76  $variableConfig = [];
77  $onclickParts = [
78  'search' => ['html_id'],
79  'subject' => 'MagentovariablePlugin.loadChooser(\'' .
81  '\', \'{{html_id}}\');',
82  ];
83  $variableWysiwyg = [
84  [
85  'name' => 'magentovariable',
86  'src' => $this->getWysiwygJsPluginSrc(),
87  'options' => [
88  'title' => __('Insert Variable...'),
89  'url' => $this->getVariablesWysiwygActionUrl(),
90  'onclick' => $onclickParts,
91  'class' => 'add-variable plugin',
92  'placeholders' => $this->getVariablesWysiwygData()
93  ],
94  ],
95  ];
96  $configPlugins = (array) $config->getData('plugins');
97  $variableConfig['plugins'] = array_merge($configPlugins, $variableWysiwyg);
98  return $variableConfig;
99  }
100 
107  public function getWysiwygJsPluginSrc()
108  {
109  $editorPluginJs = 'mage/adminhtml/wysiwyg/tiny_mce/plugins/magentovariable/editor_plugin.js';
110  return $this->_assetRepo->getUrl($editorPluginJs);
111  }
112 
120  {
121  return $this->_url->getUrl('mui/index/render', ['namespace' => 'variables_modal']);
122  }
123 
129  private function getDefaultVariables()
130  {
131  $variables = [];
132  foreach ($this->storesVariables->getData() as $variable) {
133  $variables[$variable['value']] = [
134  'code' => $variable['value'],
135  'variable_name' => $variable['label'],
137  ];
138  }
139 
140  return $variables;
141  }
142 
148  private function getCustomVariables()
149  {
150  $customVariables = $this->collectionFactory->create();
151 
152  $variables = [];
153  foreach ($customVariables->getData() as $variable) {
154  $variables[$variable['code']] = [
155  'code' => $variable['code'],
156  'variable_name' => $variable['name'],
157  'variable_type' => 'custom'
158  ];
159  }
160 
161  return $variables;
162  }
163 
169  private function getVariablesWysiwygData()
170  {
171  $variablesData = array_merge(
172  $this->getCustomVariables(),
173  $this->getDefaultVariables()
174  );
175  return $this->encoder->serialize($variablesData);
176  }
177 }
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$variable
Definition: variable.php:7
__construct(\Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Backend\Model\UrlInterface $url, \Magento\Variable\Model\ResourceModel\Variable\CollectionFactory $collectionFactory=null, \Magento\Variable\Model\Source\Variables $storesVariables=null, \Magento\Framework\Serialize\Serializer\Json $encoder=null)
Definition: Config.php:51