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
9 
12 
14 {
18  protected $_dataStorage;
19 
23  protected $_moduleReader;
24 
28  protected $fileSystem;
29 
33  protected $viewFileSystem;
34 
38  private $readDirFactory;
39 
43  private $themePackages;
44 
52  public function __construct(
53  \Magento\Email\Model\Template\Config\Data $dataStorage,
54  \Magento\Framework\Module\Dir\Reader $moduleReader,
55  \Magento\Framework\View\FileSystem $viewFileSystem,
56  ThemePackageList $themePackages,
57  ReadFactory $readDirFactory
58  ) {
59  $this->_dataStorage = $dataStorage;
60  $this->_moduleReader = $moduleReader;
61  $this->viewFileSystem = $viewFileSystem;
62  $this->themePackages = $themePackages;
63  $this->readDirFactory = $readDirFactory;
64  }
65 
71  public function getAvailableTemplates()
72  {
73  $templates = [];
74  foreach (array_keys($this->_dataStorage->get()) as $templateId) {
75  $templates[] = [
76  'value' => $templateId,
77  'label' => $this->getTemplateLabel($templateId),
78  'group' => $this->getTemplateModule($templateId),
79  ];
80  $themeTemplates = $this->getThemeTemplates($templateId);
81  $templates = array_merge($templates, $themeTemplates);
82  }
83  return $templates;
84  }
85 
92  public function getThemeTemplates($templateId)
93  {
94  $templates = [];
95 
96  $area = $this->getTemplateArea($templateId);
97  $module = $this->getTemplateModule($templateId);
98  $filename = $this->_getInfo($templateId, 'file');
99 
100  foreach ($this->themePackages->getThemes() as $theme) {
101  if ($theme->getArea() == $area) {
102  $themeDir = $this->readDirFactory->create($theme->getPath());
103  $file = "$module/email/$filename";
104  if ($themeDir->isExist($file)) {
105  $templates[] = [
106  'value' => sprintf(
107  '%s/%s/%s',
108  $templateId,
109  $theme->getVendor(),
110  $theme->getName()
111  ),
112  'label' => sprintf(
113  '%s (%s/%s)',
115  $theme->getVendor(),
116  $theme->getName()
117  ),
118  'group' => $this->getTemplateModule($templateId),
119  ];
120  }
121  }
122  }
123 
124  return $templates;
125  }
126 
134  {
135  $parts = [
136  'templateId' => $templateId,
137  'theme' => null
138  ];
139  $pattern = "#^(?<templateId>[^/]+)/(?<themeVendor>[^/]+)/(?<themeName>[^/]+)#i";
140  if (preg_match($pattern, $templateId, $matches)) {
141  $parts['templateId'] = $matches['templateId'];
142  $parts['theme'] = $matches['themeVendor'] . '/' . $matches['themeName'];
143  }
144  return $parts;
145  }
146 
153  public function getTemplateLabel($templateId)
154  {
155  return __($this->_getInfo($templateId, 'label'));
156  }
157 
164  public function getTemplateType($templateId)
165  {
166  return $this->_getInfo($templateId, 'type');
167  }
168 
176  {
177  return $this->_getInfo($templateId, 'module');
178  }
179 
186  public function getTemplateArea($templateId)
187  {
188  return $this->_getInfo($templateId, 'area');
189  }
190 
198  public function getTemplateFilename($templateId, $designParams = [])
199  {
200  // If design params aren't passed, then use area/module defined in email_templates.xml
201  if (!isset($designParams['area'])) {
202  $designParams['area'] = $this->getTemplateArea($templateId);
203  }
204  $module = $this->getTemplateModule($templateId);
205  $designParams['module'] = $module;
206 
207  $file = $this->_getInfo($templateId, 'file');
208  $filename = $this->getFilename($file, $designParams, $module);
209 
210  return $filename;
211  }
212 
221  protected function _getInfo($templateId, $fieldName)
222  {
223  $data = $this->_dataStorage->get();
224  if (!isset($data[$templateId])) {
225  throw new \UnexpectedValueException("Email template '{$templateId}' is not defined.");
226  }
227  if (!isset($data[$templateId][$fieldName])) {
228  throw new \UnexpectedValueException(
229  "Field '{$fieldName}' is not defined for email template '{$templateId}'."
230  );
231  }
232  return $data[$templateId][$fieldName];
233  }
234 
246  private function getFilename(string $file, array $designParams, string $module): string
247  {
248  $filename = $this->viewFileSystem->getEmailTemplateFileName($file, $designParams, $module);
249 
250  if ($filename === false) {
251  throw new \UnexpectedValueException("Template file '{$file}' is not found.");
252  }
253 
254  return $filename;
255  }
256 }
$pattern
Definition: website.php:22
getTemplateFilename($templateId, $designParams=[])
Definition: Config.php:198
__()
Definition: __.php:13
$templateId
Definition: queue.php:15
$theme
_getInfo($templateId, $fieldName)
Definition: Config.php:221
__construct(\Magento\Email\Model\Template\Config\Data $dataStorage, \Magento\Framework\Module\Dir\Reader $moduleReader, \Magento\Framework\View\FileSystem $viewFileSystem, ThemePackageList $themePackages, ReadFactory $readDirFactory)
Definition: Config.php:52