[FEATURE] IRRE: Provide default values for created records 15/18815/9
authorSebastian Michaelsen <michaelsen@t3seo.de>
Fri, 8 Mar 2013 12:08:54 +0000 (13:08 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 5 Jul 2013 15:14:42 +0000 (17:14 +0200)
This change introduces a new TCA option to configure 'inline' fields:
'foreign_record_defaults' gives you the possibility to set values
in new created records.

Usage example:
config => array(
  'type' => 'inline',
  'foreign_table' => 'tt_content',
  'foreign_record_defaults' => array(
    'CType' => 'image'
  ),
)

That means tt_content elements that are created for this IRRE field
will be image content elements by default (and can be set to another
type by the editor before saving).

The following fields can't be set by this feature because they must
stay under system control:
uid, pid and t3ver_*
and the fields that are configured with the following TCA-ctrl options:
crdate, cruser_id, delete, origUid, transOrigDiffSourceField,
transOrigPointerField and tstamp

Change-Id: I539f9941cc69506590b55a3520f95e38d5e61dd1
Documentation: #46440
Resolves: #46124
Releases: 6.2
Reviewed-on: https://review.typo3.org/18815
Reviewed-by: Oliver Hader
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/backend/Classes/Form/Element/InlineElement.php

index c4dd9b3..fb8806c 100644 (file)
@@ -1080,11 +1080,11 @@ class InlineElement {
                $current = $this->inlineStructure['unstable'];
                // The parent table - this table embeds the current table
                $parent = $this->getStructureLevel(-1);
+               $config = $parent['config'];
                // Get TCA 'config' of the parent table
-               if (!$this->checkConfiguration($parent['config'])) {
+               if (!$this->checkConfiguration($config)) {
                        return $this->getErrorMessageForAJAX('Wrong configuration in table ' . $parent['table']);
                }
-               $config = $parent['config'];
                $collapseAll = isset($config['appearance']['collapseAll']) && $config['appearance']['collapseAll'];
                $expandSingle = isset($config['appearance']['expandSingle']) && $config['appearance']['expandSingle'];
                // Put the current level also to the dynNestedStack of TCEforms:
@@ -1092,6 +1092,29 @@ class InlineElement {
                // Dynamically create a new record using \TYPO3\CMS\Backend\Form\DataPreprocessor
                if (!$foreignUid || !MathUtility::canBeInterpretedAsInteger($foreignUid) || $config['foreign_selector']) {
                        $record = $this->getNewRecord($this->inlineFirstPid, $current['table']);
+                       // Set default values for new created records
+                       if (isset($config['foreign_record_defaults']) && is_array($config['foreign_record_defaults'])) {
+                               $foreignTableConfig = $GLOBALS['TCA'][$current['table']];
+                               // the following system relevant fields can't be set by foreign_record_defaults
+                               $notSettableFields = array(
+                                       'uid', 'pid', 't3ver_oid', 't3ver_id', 't3ver_label', 't3ver_wsid', 't3ver_state', 't3ver_stage',
+                                       't3ver_count', 't3ver_tstamp', 't3ver_move_id'
+                               );
+                               $configurationKeysForNotSettableFields = array(
+                                       'crdate', 'cruser_id', 'delete', 'origUid', 'transOrigDiffSourceField', 'transOrigPointerField',
+                                       'tstamp'
+                               );
+                               foreach ($configurationKeysForNotSettableFields as $configurationKey) {
+                                       if (isset($foreignTableConfig['ctrl'][$configurationKey])) {
+                                               $notSettableFields[] = $foreignTableConfig['ctrl'][$configurationKey];
+                                       }
+                               }
+                               foreach ($config['foreign_record_defaults'] as $fieldName => $defaultValue) {
+                                       if (isset($foreignTableConfig['columns'][$fieldName]) && !in_array($fieldName, $notSettableFields)) {
+                                               $record[$fieldName] = $defaultValue;
+                                       }
+                               }
+                       }
                        // Set language of new child record to the language of the parent record:
                        if ($config['localizationMode'] == 'select') {
                                $parentRecord = $this->getRecord(0, $parent['table'], $parent['uid']);