XF 2.0 Extending XF\Admin\Controller\UserGroup

Darcness

Member
In the course of developing an add-on, I've added a couple of boolean flags to xf_user_group. I'd like to update those booleans from within the user_group_edit Admin template.

Should I be extending the userGroupSaveProcess() method? And, if so, how do I get data into the entity?

XF\Admin\Controller\UserGroup.userGroupSaveProcess() contains a call to $this->formAction()->basicEntitySave() and supplies the UserGroup entity and a collection of data for the object, which leads me to believe that the 'save' has already been completed well before the form's run() method gets called. Am I out of luck? How are others doing this?
 
The FormAction system is a way to split a form's actions into multiple distinct steps.

Methods such as basicEntitySave and apply and many others you might see related to the FormAction object do not actually execute when they are called, but instead their actions are logged to run later. They're not run until the run method is called on the FormAction object (which happens in the actionSave method).

With that in mind, you would simply extend the userGroupSaveProcess and get the parent's return value (which is the FormAction object):
PHP:
$formAction = parent::userGroupSaveProcess(\XF\Entity\UserGroup $userGroup);

// your code here

return $formAction;
And your code would look something like:
PHP:
$input = $this->filter([
    'your_bool_1' => 'bool',
    'your_bool_2' => 'bool'
]);
$formAction->setup(function() use($userGroup, $input)
{
    $userGroup->bulkSet($input);
});
All of the steps which are logged when the parent method calls are still run. This means you don't need to worry about calling any sort of validate or save methods and everything just happens in the correct and intended order.
 
Hey @Chris D,

thank you for pointing me in the right direction. I've got my UserGroup extension working thanks to this thread.

One thing though puzzles me still: The UserGroup cache getUserBannerCacheData doesn't update, when my custom setting is saved, but does normally, when I change some of the default settings.

My guess now is that calling $form->basicEntitySave($userGroup, $input); invalidates the UserGroup cache. Is there any method to invalidate the cache by hand? Or can I use an earlier hook to get into the formAction?

Thanks a lot!

P.s.: I come from the WordPress world, so getting into modifying xenforo was a bit of a mental leap, but once you get your head around the core concepts playing with the code is really, really fun. Very well done! Thank you!

P.p.s.: If I only had waited 3 minutes. To answer my own questions: Look into /src/XF/Entity/UserGroup and find the _postSave function to see how the caches are invalidated. Real easy.
 
Back
Top Bottom