Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andyhausmann/6194046 to your computer and use it in GitHub Desktop.
Save andyhausmann/6194046 to your computer and use it in GitHub Desktop.
This is a Hook which is responsible for flexform manipulations depending on the chosen controller action (switchableControllerAction). Following Hook file is located in typo3conf/ext/your_ext/Classes/Hooks/T3libBefunc.php You need to register this Hook within your ext_localconf: $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_befunc…
<?php
/***************************************************************
* Copyright notice
*
* (c) 2012-2013 Andy Hausmann <ah@sota-studio.de>, sota studio
* 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 2 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!
***************************************************************/
/**
* Hook into t3lib_befunc to change Flexform behavior depending on detected action
*
* @package TYPO3
* @subpackage your_ext
*/
class Tx_YourExt_Hooks_T3libBefunc
{
/** @var string Extension key of this Extension */
static protected $extKey = 'your_ext';
/** @var array List of Plugins names to react to */
protected $pluginNames = array(
'core', 'widgets'
);
/**
* Mapping for Controller->Action pair and removable fields.
* The first one acts as default respectively fallback one.
*
* Convention for structure:
*
* Controller->action => array(
* flexformTreeNode => fields,to,remove
* )
*
* Mentioned fields are prefixed in Flexforms by 'settings.'
* Method 'deleteFromStructure' can easily be manipulated in order to allow definiting of non-prefixed fields.
* I've just chosen this kind of declaration, because i mostly want to make every Flexform setting available within the Fluid templates.
*
* @var array The Mapping
*/
protected $fieldMap = array(
'ControllerOne->index' => array(
'sDEF' => 'fieldOne,fieldTwo'
),
'ControllerOne->map' => array(
'sDEF' => 'fieldOne,fieldTwo',
'template' => 'fieldFive,fieldSix'
),
'ControllerOne->list' => array(
'sDEF' => 'fieldOne,fieldTwo,fieldThree'
),
'ControllerOne->search' => array(
'sDEF' => 'fieldFour'
),
'ControllerOne->detail' => array(
'sDEF' => ''
),
'ControllerTwo->index' => array(
'sDEF' => ''
)
);
/**
* Returns the Extension key.
*
* @return string The Extension key.
*/
static public function getExtKey() {
return self::$extKey;
}
/**
* Returns the Extension name, transformed into a Plugin signature prefix.
* E.g.: xt_name becomes ExtName, return value will be extname.
*
* @return string Plugin signature prefix
*/
protected function getPluginPrefix() {
$extName = t3lib_div::underscoredToUpperCamelCase(self::$extKey);
return strtolower($extName); //
}
/**
* Returns a list of Plugins to react to.
*
* @return array List of Plugins
*/
protected function getPlugins() {
$plugins = array();
foreach ($this->pluginNames as $k => $v) {
array_push($plugins, $this->getPluginPrefix() . '_' . $v);
}
return $plugins;
}
/**
* Returns the first defined Controller-Action pair configured.
* The first one also acts as default/fallback.
*
* @return string Default pair of Controller and Action.
*/
protected function getDefaultControllerAndAction() {
reset($this->fieldMap);
return key($this->fieldMap);
}
/**
* Returns the removable fields depending on the chosen Controller and Action.
*
* @param string $controllerActionPair
* @return array
*/
protected function getRemovableFields($controllerActionPair) {
return (is_array($this->fieldMap[$controllerActionPair]))
? $this->fieldMap[$controllerActionPair]
: array();
}
protected function allowedToAct($listType) {
return (in_array($listType, $this->getPlugins()))
? true
: false;
}
/**
* Hook function of t3lib_befunc
* It is used to change the flexform if it is about news
*
* @param array &$dataStructure Flexform structure
* @param array $conf some strange configuration
* @param array $row row of current record
* @param string $table table name
* @param string $fieldName some strange field name
* @return void
*/
public function getFlexFormDS_postProcessDS(&$dataStructure, $conf, $row, $table, $fieldName) {
if ($this->allowedToAct($row['list_type']) && $table === 'tt_content' && is_array($dataStructure)) {
$this->updateFlexforms($dataStructure, $row);
}
}
/**
* Update flexform configuration if a action is selected
*
* @param array|string &$dataStructure flexform structure
* @param array $row row of current record
* @return void
*/
protected function updateFlexforms(array &$dataStructure, array $row) {
$selectedView = '';
// Get selected action
$flexform = t3lib_div::xml2array($row['pi_flexform']);
if (is_array($flexform) && is_array($flexform['data'])) {
$selectedView = $flexform['data']['sDEF']['lDEF']['switchableControllerActions']['vDEF'];
if (!empty($selectedView)) {
$actionParts = t3lib_div::trimExplode(';', $selectedView, true);
$selectedView = $actionParts[0];
}
// If new plugin element
} elseif (t3lib_div::isFirstPartOfStr($row['uid'], 'NEW')) {
$selectedView = $this->getDefaultControllerAndAction();
} else {
$selectedView = $this->getDefaultControllerAndAction();
}
if (!empty($selectedView)) {
$this->deleteFromStructure($dataStructure, $this->getRemovableFields($selectedView));
if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXT'][$this->extKey]['Hooks/T3libBefunc.php']['updateFlexforms'])) {
$params = array(
'selectedView' => $selectedView,
'dataStructure' => &$dataStructure,
);
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXT'][$this->extKey]['Hooks/T3libBefunc.php']['updateFlexforms'] as $reference) {
t3lib_div::callUserFunction($reference, $params, $this);
}
}
}
}
/**
* Remove fields from flexform structure
*
* @param array &$dataStructure flexform structure
* @param array $fieldsToBeRemoved fields which need to be removed
* @return void
*/
protected function deleteFromStructure(array &$dataStructure, array $fieldsToBeRemoved) {
foreach ($fieldsToBeRemoved as $sheetName => $sheetFields) {
$fieldsInSheet = t3lib_div::trimExplode(',', $sheetFields, true);
foreach ($fieldsInSheet as $fieldName) {
unset($dataStructure['sheets'][$sheetName]['ROOT']['el']['settings.' . $fieldName]);
}
}
}
}
$extKey = Tx_YourExt_Hooks_T3libBefunc::getExtKey();
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/' . $extKey . '/Classes/Hooks/T3libBefunc.php']) {
require_once ($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/' . $extKey . '/Classes/Hooks/T3libBefunc.php']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment