如何配置femanager从first_name/last_name自动填充fe_users的name字段?
Absolutely! You can auto-populate the fe_users.name field using values from first_name and last_name in femanager—perfect for meeting that mandatory field requirement from the seminars extension. Here are two reliable approaches to set this up:
1. Use Femanager's Pre-Save Hook (Recommended)
Femanager has dedicated hooks for modifying user data before it’s saved, which is the cleanest way to handle this since it’s tightly integrated with femanager’s workflow.
Step 1: Register the hook
Add this to your custom extension’sext_localconf.php(create one if you don’t have a site-specific extension yet):$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['femanager']['hooks']['processData'][] = \YourVendor\YourExtension\Hooks\FemanagerDataProcessor::class;Step 2: Implement the hook class
CreateClasses/Hooks/FemanagerDataProcessor.phpin your extension with this logic:<?php namespace YourVendor\YourExtension\Hooks; use In2code\Femanager\Domain\Model\User; use In2code\Femanager\Event\ProcessDataEvent; class FemanagerDataProcessor { public function processData(ProcessDataEvent $event): void { /** @var User $user */ $user = $event->getUser(); // Combine first and last name, trim whitespace $firstName = trim($user->getFirstName() ?? ''); $lastName = trim($user->getLastName() ?? ''); $fullName = trim("$firstName $lastName"); // Set the name field, handle empty values to meet seminars' mandatory requirement if (!empty($fullName)) { $user->setName($fullName); } else { // Fallback: Use username or email prefix if both names are empty $fallbackName = $user->getUsername() ?? substr($user->getEmail(), 0, strpos($user->getEmail(), '@')); $user->setName($fallbackName ?? 'Unknown User'); } } }This hook triggers every time a user is saved via femanager—whether that’s front-end registration, front-end profile edits, or femanager’s backend user management.
2. Use TYPO3 DataHandler Hook (Global Effect)
If you want this auto-population to work for all fe_users changes (not just those via femanager), use TYPO3’s core DataHandler hook:
Step 1: Register the hook
Add this to your extension’sext_localconf.php:$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = \YourVendor\YourExtension\Hooks\DataHandlerProcessor::class;Step 2: Implement the hook class
CreateClasses/Hooks/DataHandlerProcessor.php:<?php namespace YourVendor\YourExtension\Hooks; use TYPO3\CMS\Core\DataHandling\DataHandler; class DataHandlerProcessor { public function processDatamap_preProcessFieldArray(&$fieldArray, $table, $id, DataHandler $dataHandler) { if ($table === 'fe_users') { $firstName = trim($fieldArray['first_name'] ?? ''); $lastName = trim($fieldArray['last_name'] ?? ''); $fullName = trim("$firstName $lastName"); if (!empty($fullName)) { $fieldArray['name'] = $fullName; } else { // Fallback to username or email prefix to avoid empty mandatory field $fieldArray['name'] = $fieldArray['username'] ?? substr($fieldArray['email'], 0, strpos($fieldArray['email'], '@')) ?? 'Unknown User'; } } } }This will run whenever any
fe_usersrecord is modified—including direct backend edits or changes from other extensions.
Optional: Hide the Name Field in Femanager
To prevent users from editing the auto-populated name field manually:
- Go to the Template module in the TYPO3 backend
- Edit your site’s template, open the Constant Editor
- Navigate to
plugin.tx_femanager.settingsand removenamefrom the form field configuration, or set it to hidden/readonly.
This ensures the name field is always managed automatically, no user interference needed.
内容的提问来源于stack exchange,提问作者lisardo

