Skip to content

Instantly share code, notes, and snippets.

@a-r-m-i-n
Created November 5, 2014 15:55
Show Gist options
  • Save a-r-m-i-n/6a4b4cd8c0e73747d1f2 to your computer and use it in GitHub Desktop.
Save a-r-m-i-n/6a4b4cd8c0e73747d1f2 to your computer and use it in GitHub Desktop.
Example of how to provide a BackendLayoutFileProvider in TYPO3 6.2
backend_layout {
colCount = 1
rowCount = 1
rows {
1 {
columns {
1 {
name = Disabled
}
}
}
}
}
<?php
namespace sunzinet\Typo3szBase\Provider;
/***************************************************************
* Copyright notice
*
* (c) 2014 Armin Rüdiger Vieweg <[email protected]>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayout;
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayoutCollection;
use TYPO3\CMS\Backend\View\BackendLayout\DataProviderContext;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class FileProvider
*/
class BackendLayoutFileProvider implements \TYPO3\CMS\Backend\View\BackendLayout\DataProviderInterface {
/**
* Adds backend layouts to the given backend layout collection.
*
* @param DataProviderContext $dataProviderContext
* @param BackendLayoutCollection $backendLayoutCollection
* @return void
*/
public function addBackendLayouts(DataProviderContext $dataProviderContext, BackendLayoutCollection $backendLayoutCollection) {
$files = $this->getLayoutFiles();
foreach ($files as $file) {
$backendLayout = $this->createBackendLayout($file);
$backendLayoutCollection->add($backendLayout);
}
}
/**
* Gets a backend layout by (regular) identifier.
*
* @param string $identifier
* @param integer $pageId
* @return BackendLayout|NULL test
*/
public function getBackendLayout($identifier, $pageId) {
$files = $this->getLayoutFiles();
foreach ($files as $file) {
$fileInformation = pathinfo($file);
if ($identifier === array_pop(explode(DIRECTORY_SEPARATOR, $fileInformation['dirname']))) {
return $this->createBackendLayout($file);
}
}
return NULL;
}
/**
* Returns backend layout paths
*
* @return array
*/
protected function getLayoutFiles() {
$fileCollection = array();
if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutFileProvider'])
|| !is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutFileProvider'])) {
return $fileCollection;
}
if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutFileProvider']['dir'])) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutFileProvider']['dir'] as $directory) {
$directory = GeneralUtility::getFileAbsFileName($directory);
$subDirectories = GeneralUtility::get_dirs($directory);
if (is_array($subDirectories)) {
foreach ($subDirectories as $subDirectory) {
$layoutFile = $directory . DIRECTORY_SEPARATOR . $subDirectory . DIRECTORY_SEPARATOR . 'backendlayout.ts';
if (file_exists($layoutFile)) {
$this->addFileToCollection($layoutFile, $fileCollection);
}
}
}
}
}
return $fileCollection;
}
/**
* Creates a new backend layout using the given record data.
*
* @param string $file
* @return BackendLayout
*/
protected function createBackendLayout($file) {
$fileInformation = pathinfo($file);
$backendLayout = BackendLayout::create(
array_pop(explode(DIRECTORY_SEPARATOR, $fileInformation['dirname'])),
$this->getTitle($fileInformation),
GeneralUtility::getUrl($file)
);
return $backendLayout;
}
/**
* @param $file
* @param $fileCollection
* @return void
* @throws \UnexpectedValueException
*/
protected function addFileToCollection($file, array &$fileCollection) {
$key = sha1($file);
if (isset($fileCollection[$key])) {
throw new \UnexpectedValueException(sprintf('The file "%s" exists already, see "%s"', $file, $fileCollection[$key]));
}
$fileCollection[$key] = $file;
}
/**
* Returns title of backend_layout located in locallang.xml. If key or file
* is not existing, the directory name of backend_layout will be returned
*
* @param array $fileInformation pathinfo() information of the given file
* @return string
*/
protected function getTitle(array $fileInformation) {
$locallangFile = $fileInformation['dirname'] . DIRECTORY_SEPARATOR . 'locallang.xml';
if (file_exists($locallangFile)) {
$title = $GLOBALS['LANG']->sL('LLL:' . $locallangFile . ':title');
if (!empty($title)) {
return $title;
}
}
return array_pop(explode(DIRECTORY_SEPARATOR, $locallangFile));
}
}
<?php
if (TYPO3_MODE === 'BE') {
// Provide file based backend layouts
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutDataProvider']['typo3sz_base']
= 'sunzinet\Typo3szBase\Provider\BackendLayoutFileProvider';
// Here you set the path of backend layouts
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['BackendLayoutFileProvider']['dir'][]
= 'fileadmin/templates/data/backend_layouts';
}
// Folder structure of previously set path of backend layouts:
//
// fileadmin/templates/data/backend_layouts/
// - main
// - backendlayout.ts
// - locallang.xml
// - main-right
// - backendlayout.ts
// - locallang.xml
// "main" and "main-right" are the names of the provided backend layouts.
// The backendlayout.ts contains the configuration of the layout.
// The locallang.xml some one translation for the title of the layout in backend.
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<T3locallang>
<meta type="array">
<type>database</type>
<description>Language labels for backend layouts</description>
</meta>
<data type="array">
<languageKey index="default" type="array">
<label index="title">Main-Template</label>
</languageKey>
<languageKey index="de" type="array">
<label index="title">Haupt-Template</label>
</languageKey>
</data>
</T3locallang>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment