TYPO3 CMS  TYPO3_8-7
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 
66 {
72  protected $backendLayouts = [];
73 
79  protected $pageTsConfig = [];
80 
86  protected $pageId = 0;
87 
93  protected function setPageTsConfig(array $pageTsConfig)
94  {
95  $this->pageTsConfig = $pageTsConfig;
96  }
97 
103  protected function getPageTsConfig()
104  {
105  return $this->pageTsConfig;
106  }
107 
113  protected function setPageId($pageId)
114  {
115  $this->pageId = (int)$pageId;
116  }
117 
123  protected function getPageId()
124  {
125  return (int)$this->pageId;
126  }
127 
134  protected function generatePageTsConfig($dataProviderContext = null)
135  {
136  if ($dataProviderContext === null) {
137  $pageId = $this->getPageId();
138  $pageId = $pageId > 0 ? $pageId : (int)GeneralUtility::_GP('id');
140  } else {
141  $pageTsConfig = $dataProviderContext->getPageTsConfig();
142  }
144  }
145 
151  protected function generateBackendLayouts($dataProviderContext = null)
152  {
153  $this->generatePageTsConfig($dataProviderContext);
154  $pageTsConfig = $this->getPageTsConfig();
155  if (!empty($pageTsConfig['mod.']['web_layout.']['BackendLayouts.'])) {
156  $backendLayouts = (array)$pageTsConfig['mod.']['web_layout.']['BackendLayouts.'];
157  foreach ($backendLayouts as $identifier => $data) {
158  $backendLayout = $this->generateBackendLayoutFromTsConfig($identifier, $data);
159  $this->attachBackendLayout($backendLayout);
160  }
161  }
162  }
163 
169  protected function generateBackendLayoutFromTsConfig($identifier, $data)
170  {
171  if (!empty($data['config.']['backend_layout.']) && is_array($data['config.']['backend_layout.'])) {
172  $backendLayout['uid'] = substr($identifier, 0, -1);
173  $backendLayout['title'] = ($data['title']) ? $data['title'] : $backendLayout['uid'];
174  $backendLayout['icon'] = ($data['icon']) ? $data['icon'] : '';
175  // Convert PHP array back to plain TypoScript so it can be procecced
176  $config = \TYPO3\CMS\Core\Utility\ArrayUtility::flatten($data['config.']);
177  $backendLayout['config'] = '';
178  foreach ($config as $row => $value) {
179  $backendLayout['config'] .= $row . ' = ' . $value . "\r\n";
180  }
181  return $backendLayout;
182  }
183  return null;
184  }
185 
191  protected function attachBackendLayout($backendLayout = null)
192  {
193  if ($backendLayout) {
194  $this->backendLayouts[$backendLayout['uid']] = $backendLayout;
195  }
196  }
197 
202  public function addBackendLayouts(DataProviderContext $dataProviderContext, BackendLayoutCollection $backendLayoutCollection)
203  {
204  $this->generateBackendLayouts($dataProviderContext);
205  foreach ($this->backendLayouts as $backendLayoutConfig) {
206  $backendLayout = $this->createBackendLayout($backendLayoutConfig);
207  $backendLayoutCollection->add($backendLayout);
208  }
209  }
210 
218  public function getBackendLayout($identifier, $pageId)
219  {
220  $this->setPageId($pageId);
221  $this->generateBackendLayouts();
222  $backendLayout = null;
223  if (array_key_exists($identifier, $this->backendLayouts)) {
224  return $this->createBackendLayout($this->backendLayouts[$identifier]);
225  }
226  return $backendLayout;
227  }
228 
235  protected function createBackendLayout(array $data)
236  {
237  $backendLayout = BackendLayout::create($data['uid'], $data['title'], $data['config']);
238  $backendLayout->setIconPath($this->getIconPath($data['icon']));
239  $backendLayout->setData($data);
240  return $backendLayout;
241  }
242 
249  protected function getIconPath($icon)
250  {
251  $iconPath = '';
252  if (!empty($icon)) {
253  $iconPath = $icon;
254  }
255  return $iconPath;
256  }
257 }
static getPagesTSconfig($id, $rootLine=null, $returnPartArray=false)
static flatten(array $array, $prefix='')
static create($identifier, $title, $configuration)
addBackendLayouts(DataProviderContext $dataProviderContext, BackendLayoutCollection $backendLayoutCollection)