This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Ability to add field after another field when extend form To often needed to add field after specific field in another plugin. It is great feature
Last updated
Here is a way you can do that:
public function boot() {
UserModel::extend(function ($model) {
$model->hasOne['profile'] = ['KurtJensen\Profile\Models\Profile',
'key' => 'user_id',
'otherKey' => 'id'];
});
UsersController::extendFormFields(function ($form, $model, $context) {
// Prevent extending of related form instead of the intended User form
if (!$model instanceof UserModel) {
return;
}
if (!$model->exists) {
return;
}
if ($form->getContext() != 'update' && $form->getContext() != 'preview') {
return;
}
if (!ProfileModel::getFromUser($form->model)) {
return;
}
$groupsField = $form->getField('groups');
$form->removeField('groups');
$form->addTabFields([
'main_group' => [
'label' => 'Main User Group',
'comment' => 'Set the primary group association for this user.',
'tab' => 'rainlab.user::lang.user.account',
'type' => 'dropdown',
'options' => $this->getMainUsergroupOptions(),
],
'groups' => $groupsField->config,
]);
});
}
Above, I added another field above the original groups field.
Last updated
Thanks KurtJensen was just looking for this
I think a built in solution would certainly worth considering, potentially introducing an after
parameter, e.g:
$widget->addFields([
'birthday' => [
'label' => 'Birthday',
'comment' => 'Select the users birthday',
'type' => 'datepicker',
'after' => 'name'
]
]);
matthewpawley said:
Thanks KurtJensen was just looking for this
I think a built in solution would certainly worth considering, potentially introducing an
after
parameter, e.g:$widget->addFields([ 'birthday' => [ 'label' => 'Birthday', 'comment' => 'Select the users birthday', 'type' => 'datepicker', 'after' => 'name' ] ]);
is this solution added or added any other way ?
is this solution added or added any other way ?
Nope, unfortunately but if you want to add fields after existing fields, try this:
$form->addFields([...], 'primary');
it solved my issue.
Last updated
I use this function to place my fields when extending backend's forms :
/**
* Add positionned fileds in backend form
*
* @param \Backend\Behaviors\FormController $form
* @param array $config
* @param string $where
* @return void
*/
private function addPositionedFormFields($form, $config, $where = null)
{
$beforeFields = [];
$afterFields = [];
$sectionDetails = false;
$first = array_first($config, function () {
return true;
});
$beforeField = is_array($first) ? array_get($first, 'before') : null;
$afterField = is_array($first) ? array_get($first, 'after') : null;
$fields = $form->fields;
if ($where == 'primary') {
$fields = $form->tabs["fields"];
}
if ($where == 'secondary') {
$fields = $form->secondaryTabs["fields"];
}
foreach ($fields as $field => $value) {
$item = $form->getField($field);
$itemName = $item->fieldName;
if ($itemName == $afterField or $itemName == $beforeField or $sectionDetails) {
if ($itemName == $afterField and !$sectionDetails) {
$sectionDetails = true;
} else {
$afterFields[$itemName] = $item->config;
$sectionDetails = true;
$form->removeField($field);
}
}
}
switch ($where) {
case 'primary':
$form->addTabFields($config, $where);
$form->addTabFields($afterFields, $where);
break;
case 'secondary':
$form->addSecondaryTabFields($config, $where);
$form->addSecondaryTabFields($afterFields, $where);
break;
default:
$form->addFields($config, $where);
$form->addFields($afterFields, $where);
}
}
Use it like :
$this->addPositionedFormFields($widget, [
'birthday' => [
'tab' => 'My new tab,
'label' => 'Birthday',
'comment' => 'Select the users birthday',
'type' => 'datepicker',
"before" => "name", // positioned before the reference field
// "after" => "name", // positioned after the reference field
],
], 'primary');
Maybe not the best or most optimized solution, but it does the trick.
1-8 of 8