Robbo
Well-known member
I would like some extensions done to controllers for more modular code. In almost all situations when saving data (usually actionSave()) you have to replace the whole function when extending the initial controller and this just feels oh so wrong! Then obviously you have the issues of add-on compatibility. What I suggest is methods to be called from within the save actions. Usually you will have something like $input = $this->_input->filter(...) which is then added to the datawriter with $dataWriterObject->bulkSet($input). Now in a lot of cases all you want your add-on to do is save something else and I have had to do this many times either for clients modifications or released add-ons. Every time I have had to copy and paste the whole method. This could be something as simple as an empty method added to XenForo_Controller... array getInputForSaveAction(string $action, array &$input) . Then have this called after you do your filter specified above. So you would get something like (taken from category admin controller in actionSave)...
Then do the normal data writer stuff which would work as the data writer would be extended like normal.
Now extending all you would have to do is...
After writing that out I can think of a better way to do it. Have that method protected (and possibly final) for starters and have it check for _action{$action}ExtraInput(array &$input). This would/could raise an issue for the extended method again though, they would have to check if the parent method existed before calling it. Still, anything is better than copying a whole function and essentially making add-ons incompatible.
Thoughts? Anyone got a better method?
Code:
$writerData = $this->_input->filter(array(
'title' => XenForo_Input::STRING,
'node_type_id' => XenForo_Input::BINARY,
'parent_node_id' => XenForo_Input::UINT,
'display_order' => XenForo_Input::UINT,
'display_in_list' => XenForo_Input::UINT,
'description' => XenForo_Input::STRING,
'style_id' => XenForo_Input::UINT,
));
$this->getInputForSaveAction('save', $input);
Now extending all you would have to do is...
Code:
public function getInputForSaveAction($action, array &input)
{
parent::getInputForSaveAction($action, $input);
if ($action == 'save')
{
$input['my_extra_database_field'] = $this->_input->filterSingle('my_field_yo');
}
}
}
After writing that out I can think of a better way to do it. Have that method protected (and possibly final) for starters and have it check for _action{$action}ExtraInput(array &$input). This would/could raise an issue for the extended method again though, they would have to check if the parent method existed before calling it. Still, anything is better than copying a whole function and essentially making add-ons incompatible.
Thoughts? Anyone got a better method?
Upvote
10