TYPO3 CMS  TYPO3_7-6
PageTsBackendLayoutDataProvider.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
23 
67 {
73  protected $backendLayouts = [];
74 
80  protected $pageTsConfig = [];
81 
87  protected $pageId = 0;
88 
95  protected function setPageTsConfig(array $pageTsConfig)
96  {
97  $this->pageTsConfig = $pageTsConfig;
98  }
99 
105  protected function getPageTsConfig()
106  {
107  return $this->pageTsConfig;
108  }
109 
116  protected function setPageId($pageId)
117  {
118  $this->pageId = (int)$pageId;
119  }
120 
126  protected function getPageId()
127  {
128  return (int)$this->pageId;
129  }
130 
138  protected function generatePageTsConfig($dataProviderContext = null)
139  {
140  if ($dataProviderContext === null) {
141  $pageId = $this->getPageId();
142  $pageId = $pageId > 0 ? $pageId : (int)GeneralUtility::_GP('id');
144  } else {
145  $pageTsConfig = $dataProviderContext->getPageTsConfig();
146  }
148  }
149 
156  protected function generateBackendLayouts($dataProviderContext = null)
157  {
158  $this->generatePageTsConfig($dataProviderContext);
159  $pageTsConfig = $this->getPageTsConfig();
160  if (!empty($pageTsConfig['mod.']['web_layout.']['BackendLayouts.'])) {
161  $backendLayouts = (array)$pageTsConfig['mod.']['web_layout.']['BackendLayouts.'];
162  foreach ($backendLayouts as $identifier => $data) {
163  $backendLayout = $this->generateBackendLayoutFromTsConfig($identifier, $data);
164  $this->attachBackendLayout($backendLayout);
165  }
166  }
167  }
168 
174  protected function generateBackendLayoutFromTsConfig($identifier, $data)
175  {
176  if (!empty($data['config.']['backend_layout.']) && is_array($data['config.']['backend_layout.'])) {
177  $backendLayout['uid'] = substr($identifier, 0, -1);
178  $backendLayout['title'] = ($data['title']) ? $data['title'] : $backendLayout['uid'];
179  $backendLayout['icon'] = ($data['icon']) ? $data['icon'] : '';
180  // Convert PHP array back to plain TypoScript so it can be procecced
181  $config = \TYPO3\CMS\Core\Utility\ArrayUtility::flatten($data['config.']);
182  $backendLayout['config'] = '';
183  foreach ($config as $row => $value) {
184  $backendLayout['config'] .= $row . ' = ' . $value . "\r\n";
185  }
186  return $backendLayout;
187  }
188  return null;
189  }
190 
196  protected function attachBackendLayout($backendLayout = null)
197  {
198  if ($backendLayout) {
199  $this->backendLayouts[$backendLayout['uid']] = $backendLayout;
200  }
201  }
202 
208  public function addBackendLayouts(DataProviderContext $dataProviderContext, BackendLayoutCollection $backendLayoutCollection)
209  {
210  $this->generateBackendLayouts($dataProviderContext);
211  foreach ($this->backendLayouts as $backendLayoutConfig) {
212  $backendLayout = $this->createBackendLayout($backendLayoutConfig);
213  $backendLayoutCollection->add($backendLayout);
214  }
215  }
216 
224  public function getBackendLayout($identifier, $pageId)
225  {
226  $this->setPageId($pageId);
227  $this->generateBackendLayouts();
228  $backendLayout = null;
229  if (array_key_exists($identifier, $this->backendLayouts)) {
230  return $this->createBackendLayout($this->backendLayouts[$identifier]);
231  }
232  return $backendLayout;
233  }
234 
241  protected function createBackendLayout(array $data)
242  {
243  $backendLayout = BackendLayout::create($data['uid'], $data['title'], $data['config']);
244  $backendLayout->setIconPath($this->getIconPath($data['icon']));
245  $backendLayout->setData($data);
246  return $backendLayout;
247  }
248 
255  protected function getIconPath($icon)
256  {
257  $iconPath = '';
258  if (!empty($icon)) {
259  $iconPath = $icon;
260  }
261  return $iconPath;
262  }
263 }
static getPagesTSconfig($id, $rootLine=null, $returnPartArray=false)
static flatten(array $array, $prefix='')
static create($identifier, $title, $configuration)
addBackendLayouts(DataProviderContext $dataProviderContext, BackendLayoutCollection $backendLayoutCollection)