Skip to content

Instantly share code, notes, and snippets.

@xperseguers
Created May 6, 2017 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xperseguers/cdd7531a856c3cf45e69b544fb189d73 to your computer and use it in GitHub Desktop.
Save xperseguers/cdd7531a856c3cf45e69b544fb189d73 to your computer and use it in GitHub Desktop.
XCLASS to change style of IRRE blocks in FormEngine
<?php
// This is file EXT:your_ext/ext_localconf.php
// snip
// Add support for changing the IRRE div background color
// See https://twitter.com/xperseguers/status/859327351028690944 for result
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Backend\Form\Container\InlineRecordContainer::class] = [
'className' => \VENDOR\YourExt\Xclass\Backend\Form\Container\InlineRecordContainer::class,
];
<?php
// This is file EXT:your_ext/Classes/Xclass/Backend/Form/Container/InlineRecordContainer.php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace VENDOR\YourExt\Xclass\Backend\Form\Container;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Extends \TYPO3\CMS\Backend\Form\Container\InlineRecordContainer to allow changing the color of the whole inline record
* block color.
*
* @author Xavier Perseguers <xavier@causal.ch>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
*/
class InlineRecordContainer extends \TYPO3\CMS\Backend\Form\Container\InlineRecordContainer
{
/**
* Entry method
*
* @return array As defined in initializeResultArray() of AbstractNode
* @throws AccessDeniedContentEditException
*/
public function render()
{
$data = $this->data;
$this->inlineData = $data['inlineData'];
$inlineStackProcessor = $this->inlineStackProcessor;
$inlineStackProcessor->initializeByGivenStructure($data['inlineStructure']);
$record = $data['databaseRow'];
$inlineConfig = $data['inlineParentConfig'];
$foreignTable = $inlineConfig['foreign_table'];
$resultArray = $this->initializeResultArray();
// Send a mapping information to the browser via JSON:
// e.g. data[<curTable>][<curId>][<curField>] => data-<pid>-<parentTable>-<parentId>-<parentField>-<curTable>-<curId>-<curField>
$formPrefix = $inlineStackProcessor->getCurrentStructureFormPrefix();
$domObjectId = $inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($data['inlineFirstPid']);
$this->inlineData['map'][$formPrefix] = $domObjectId;
$resultArray['inlineData'] = $this->inlineData;
// If there is a selector field, normalize it:
if (!empty($inlineConfig['foreign_selector'])) {
$foreign_selector = $inlineConfig['foreign_selector'];
$valueToNormalize = $record[$foreign_selector];
if (is_array($record[$foreign_selector])) {
// @todo: this can be kicked again if always prepared rows are handled here
$valueToNormalize = implode(',', $record[$foreign_selector]);
}
$record[$foreign_selector] = $this->normalizeUid($valueToNormalize);
}
// Get the current naming scheme for DOM name/id attributes:
$appendFormFieldNames = '[' . $foreignTable . '][' . $record['uid'] . ']';
$objectId = $domObjectId . '-' . $foreignTable . '-' . $record['uid'];
$class = '';
$html = '';
$combinationHtml = '';
$isNewRecord = $data['command'] === 'new';
$hiddenField = '';
if (isset($data['processedTca']['ctrl']['enablecolumns']['disabled'])) {
$hiddenField = $data['processedTca']['ctrl']['enablecolumns']['disabled'];
}
if (!$data['isInlineDefaultLanguageRecordInLocalizedParentContext']) {
if ($isNewRecord || $data['isInlineChildExpanded']) {
// Render full content ONLY IF this is an AJAX request, a new record, or the record is not collapsed
$combinationHtml = '';
if (isset($data['combinationChild'])) {
$combinationChild = $this->renderCombinationChild($data, $appendFormFieldNames);
$combinationHtml = $combinationChild['html'];
$combinationChild['html'] = '';
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $combinationChild);
}
$childArray = $this->renderChild($data);
$html = $childArray['html'];
$childArray['html'] = '';
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childArray);
} else {
// This string is the marker for the JS-function to check if the full content has already been loaded
$html = '<!--notloaded-->';
}
if ($isNewRecord) {
// Add pid of record as hidden field
$html .= '<input type="hidden" name="data' . htmlspecialchars($appendFormFieldNames)
. '[pid]" value="' . htmlspecialchars($record['pid']) . '"/>';
// Tell DataHandler this record is expanded
$ucFieldName = 'uc[inlineView]'
. '[' . $data['inlineTopMostParentTableName'] . ']'
. '[' . $data['inlineTopMostParentUid'] . ']'
. $appendFormFieldNames;
$html .= '<input type="hidden" name="' . htmlspecialchars($ucFieldName)
. '" value="' . (int)$data['isInlineChildExpanded'] . '" />';
} else {
// Set additional field for processing for saving
$html .= '<input type="hidden" name="cmd' . htmlspecialchars($appendFormFieldNames)
. '[delete]" value="1" disabled="disabled" />';
if (!$data['isInlineChildExpanded'] && !empty($hiddenField)) {
$checked = !empty($record[$hiddenField]) ? ' checked="checked"' : '';
$html .= '<input type="checkbox" data-formengine-input-name="data'
. htmlspecialchars($appendFormFieldNames)
. '[' . htmlspecialchars($hiddenField) . ']" value="1"' . $checked . ' />';
$html .= '<input type="input" name="data' . htmlspecialchars($appendFormFieldNames)
. '[' . htmlspecialchars($hiddenField) . ']" value="' . htmlspecialchars($record[$hiddenField]) . '" />';
}
}
// If this record should be shown collapsed
$class = $data['isInlineChildExpanded'] ? 'panel-visible' : 'panel-collapsed';
}
if ($inlineConfig['renderFieldsOnly']) {
// Render "body" part only
$html = $html . $combinationHtml;
} else {
// Render header row and content (if expanded)
if ($data['isInlineDefaultLanguageRecordInLocalizedParentContext']) {
$class .= ' t3-form-field-container-inline-placeHolder';
}
if (!empty($hiddenField) && isset($record[$hiddenField]) && (int)$record[$hiddenField]) {
$class .= ' t3-form-field-container-inline-hidden';
}
$class .= ($isNewRecord ? ' inlineIsNewRecord' : '');
// XCLASS [begin]
$parameters = [
'tableName' => $data['tableName'],
'row' => $data['databaseRow'],
'style' => '',
];
// callUserFunction requires a third parameter, but we don't want to give $this as reference!
$null = null;
if (!empty($data['processedTca']['ctrl']['irreHeaderStyle_userFunc'])) {
GeneralUtility::callUserFunction($data['processedTca']['ctrl']['irreHeaderStyle_userFunc'], $parameters, $null);
}
$html = '
<div class="panel panel-default panel-condensed ' . trim($class) . '" id="' . htmlspecialchars($objectId) . '_div">
<div class="panel-heading" data-toggle="formengine-inline" id="' . htmlspecialchars($objectId) . '_header" data-expandSingle="' . ($inlineConfig['appearance']['expandSingle'] ? 1 : 0) . '">
<div class="form-irre-header" style="' . trim($parameters['style']) . '">
<div class="form-irre-header-cell form-irre-header-icon">
<span class="caret"></span>
</div>
' . $this->renderForeignRecordHeader($data) . '
</div>
</div>
<div class="panel-collapse" id="' . htmlspecialchars($objectId) . '_fields" data-expandSingle="' . ($inlineConfig['appearance']['expandSingle'] ? 1 : 0) . '" data-returnURL="' . htmlspecialchars(GeneralUtility::getIndpEnv('REQUEST_URI')) . '">' . $html . $combinationHtml . '</div>
</div>';
// XCLASS [end]
}
$resultArray['html'] = $html;
return $resultArray;
}
}
<?php
// This is file EXT:your_ext/Classes/Tca/SomeTable.php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace VENDOR\YourExt\Tca;
class SomeTable
{
/**
* Returns the styles to be used for an IRRE block.
*
* @param array $params
* @param null $pObj
* @return void
*/
public function getIrreHeaderStyle(array &$params, $pObj = null)
{
if (true) { // TODO: own business logic of course
$params['style'] = 'background-color:#ff6265';
}
}
}
<?php
// This is file EXT:your_ext/Configuration/TCA/tx_yourext_sometable.php
return [
'ctrl' => [
'title' => 'Whatever',
'irreHeaderStyle_userFunc' => \VENDOR\YourExt\Tca\SomeTable::class . '->getIrreHeaderStyle',
/// snip
],
// snip
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment