Dynamically change subform field properties #42834
jameswadsworth
started this conversation in
Ideas
Replies: 2 comments 3 replies
-
That can work |
Beta Was this translation helpful? Give feedback.
2 replies
-
This is the code I use in my plugin to dynamically update the subform fields in a user group plugin public function onContentPrepareForm(EventInterface $event)
{
[$form, $data] = array_values($event->getArguments());
// Check we are manipulating a valid form.
$formName = $form->getName();
if (!\in_array($formName, ['com_advkontent.user', 'com_users.user', 'com_users.registration', 'com_users.profile'])) {
return true;
}
FormHelper::addFieldPrefix('DeAnima\\Plugin\\User\\UserExtendedProfile\\Field');
FormHelper::addFormPath(JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name . '/forms');
$form->loadFile('userextendedprofile_multi');
$userextendedprofileFieldset = $form->getFieldset('userextendedprofile');
$userextendedprofileForm = $userextendedprofileFieldset['jform_userextendedprofile'];
$uepSubform = $userextendedprofileForm->loadSubForm();
$fieldInfo = [];
foreach ($uepSubform->getFieldset('user-details') as $field)
{
$fieldInfo[$field->fieldname] = $field->group;
}
foreach ($fieldInfo as $field => $group) {
// Case using the advkontent users manager in admin
if (\in_array($formName, ['com_advkontent.user', 'com_users.user', 'com_users.registration', 'com_users.profile'])) {
if ($this->params->get('uep_required_' . $field, 1) == 0) {
$uepSubform->removeField($field, $group);
} elseif ($this->params->get('uep_required_' . $field, 0) == 1) {
$uepSubform->setFieldAttribute($field, 'required', false, $group);
} elseif ($this->params->get('uep_required_' . $field, 1) == 2) {
$uepSubform->setFieldAttribute($field, 'required', true, $group);
}
}
}
return;
} I've attached my xml form as a text file. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
While updating a component to work with Joomla 4+, we discovered Joomla subforms. It is great way of making 1 to many relationships and has streamlined our existing code. However an issue we encountered was that we were unable to to dynamically change the field properties for fields in the child fieldset. Well we could dynamically change the field properties for when displaying a new child fieldset but not for a child fieldset which is already populated with data.
After inspecting the code we found that the in the file /libraries/src/Form/Field/SubformField.php the function loadSubFormData when loading the data for multiple data values, creates the new form instance using the $formsource, which loads the data from the original subform field file, therefore ignoring any dynamic changes that might have taken place in a plugin.
The new new field structure is available in the function loadSubFormData because it is passed in the variable Form $subForm. So changing the line
to
means that the fields in the child subform now display reflecting any changes made in a plugin.
Therefore we propose making this change to the core code as being able to dynamically change field properties of the fields in the child fieldsets is a very usefully feature. It will allow developers to manipulate the field properties of the fields in the child fieldsets as we already can with other fieldset definitions. We don't think there will be any backward compatibility issues.
Let us know what you think.
Beta Was this translation helpful? Give feedback.
All reactions