szalik.dev Posted March 28, 2023 Posted March 28, 2023 Hi, i'm trying to add few new settings to forums, i've managed to extend Forum Node to add settings to Forum creator/editor but i'm wondering if there's actually a way to create new column inside of forums_forums table or do i have to save those settings within new table generated by app? What i've made so far is this: Hook extending IPS\forums\Forum node: //<?php /* To prevent PHP errors (extending class does not exist) revealing path */ if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) ) { exit; } class cpfiafi_hook_forum_settings extends _HOOK_CLASS_ { public function form( &$form ) { parent::form($form); $form->addTab( 'tab_settings' ); $form->addHeader( 'header_settings' ); $form->add( new \IPS\Helpers\Form\Text('input_icon') ); } } and i've stopped here while working on something else but this still troubles me on how to do it.
Nathan Explosion Posted March 28, 2023 Posted March 28, 2023 9 hours ago, CodePixel.dev said: but i'm wondering if there's actually a way to create new column inside of forums_forums table If you are intending to release the resource on the MP then you shouldn't think about doing this and instead should... 9 hours ago, CodePixel.dev said: ...save those settings within new table generated by app? To then retrieve those settings when loading a forum, you'd need to hook into constructLoadQuery in \IPS\forums\Forum and do something like this: protected static function constructLoadQuery( $id, $idField, $extraWhereClause ){ $parent = parent::constructLoadQuery($id, $idField, $extraWhereClause); $parent->join('your_table', 'your_table.forumid=forums_forum.id'); return $parent; } Adriano Faria 1
szalik.dev Posted March 28, 2023 Author Posted March 28, 2023 2 hours ago, Nathan Explosion said: If you are intending to release the resource on the MP then you shouldn't think about doing this and instead should... To then retrieve those settings when loading a forum, you'd need to hook into constructLoadQuery in \IPS\forums\Forum and do something like this: protected static function constructLoadQuery( $id, $idField, $extraWhereClause ){ $parent = parent::constructLoadQuery($id, $idField, $extraWhereClause); $parent->join('your_table', 'your_table.forumid=forums_forum.id'); return $parent; } Okay this is good, exactly what i need to load data, question is how to save data in those additional fields?
Nathan Explosion Posted March 28, 2023 Posted March 28, 2023 Not looked but I would expect that a hook into save in \IPS\forums\Forum will be needed to allow you to grab your fields to allow you to then insert them to your table, and to also unset them from the values so that it doesn't attempt to save in forums_forum.
szalik.dev Posted March 28, 2023 Author Posted March 28, 2023 1 minute ago, Nathan Explosion said: unset them from the values so that it doesn't attempt to save in forums_forum This gave me an idea, you've helped a lot, thanks!
szalik.dev Posted March 28, 2023 Author Posted March 28, 2023 Back to the issue at hand, i've decided to make a new Node for that settings i want to add, everything cool and all, works and saves but i get error all the time that colum input_icon is undefined in field list, i tried doing unse unset($values['input_icon']); but that didn't help. I'm stuck completely and i don't know how to unset this variable so it doesn't try to save inside Forum node.
szalik.dev Posted March 28, 2023 Author Posted March 28, 2023 class cpfiafi_hook_forum_settings extends _HOOK_CLASS_ { public function form( &$form ) { parent::form($form); if(isset(\IPS\Request::i()->id)) { try { $icon = \IPS\cpfiafi\ForumIcon::load( \IPS\Request::i()->id ); } catch ( \OutOfRangeException $e ) { // $icon = new \IPS\cpfiafi\ForumIcon(); } $form->addTab( 'tab_settings' ); $form->addHeader( 'header_settings' ); $form->add( new \IPS\Helpers\Form\Text('fa_icon') ); if ( $values = $form->values() ) { $icon->id = $this->id; $icon->fa_icon = $values['fa_icon']; $icon->save(); } } } }
Solution szalik.dev Posted March 28, 2023 Author Solution Posted March 28, 2023 Problem solved, all i had to do was unset that value inside formatFormValues function. /** * [Node] Format form values from add/edit form for save * * @param array $values Values from the form * @return array */ public function formatFormValues( $values ) { if(isset($values['fa_icon'])) { unset($values['fa_icon']); } return parent::formatFormValues( $values ); } SeNioR- 1
Recommended Posts