Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
DreamOn Posted April 18, 2016 Posted April 18, 2016 Hello, I am a web developper. I would like to add a custom field to "Create New Topic" form. I read Form Help (cf : https://invisionpower.com/4docs/advanced-usage/development/form-helper-r77/) but there is no doc where to hook the create new topic form to add our custom field. How can I do this ? Thanks.
Adriano Faria Posted April 18, 2016 Posted April 18, 2016 Take a look: You need to create a CODE hook to the \IPS\forums\Topic class. Example: /** * Get elements for add/edit form * * @param \IPS\Content\Item|NULL $item The current item if editing or NULL if creating * @param \IPS\Node\Model|NULL $container Container (e.g. forum), if appropriate * @return array */ public static function formElements( $item=NULL, \IPS\Node\Model $container=NULL ) { $return = parent::formElements( $item, $container ); $merge = array(); $merge['your_field'] = new \IPS\Helpers\Form\Text( 'your_new_column_on_forums_topics_table', $item ? $item->your_new_column_on_forums_topics_table : NULL, TRUE, array(), NULL, NULL, NULL, 'your_new_column_on_forums_topics_table' ); array_splice( $return, 0, 0, $merge ); return $return; } This will insert a new text field before topic title on topic post screen.
DreamOn Posted April 18, 2016 Author Posted April 18, 2016 Thanks a lot for your help ! So, it works. I hope I respect the IPS good practices. \IPS\forums\Topic hook : /** * Get elements for add/edit form * * @param \IPS\Content\Item|NULL $item The current item if editing or NULL if creating * @param \IPS\Node\Model|NULL $container Container (e.g. forum), if appropriate * @return array */ static public function formElements($item = NULL, \IPS\Node\Model $container = NULL) { $return = parent::formElements($item, $container); $options = array( 'autocomplete' => array( 'maxItems' => 1, 'minItems' => 1, 'unique' => TRUE, 'freeChoice' => FALSE, 'source' => \IPS\Settings::i()->getWpPosts(), 'prefix' => FALSE, 'desc' => 'Field description' ), 'placeholder' => 'Field placeholder' ); $merge = array(); $wp_post_field = new \IPS\Helpers\Form\Text( 'wp_post', $item ? $item->lci_wp_post : NULL, FALSE, $options, NULL, NULL, NULL, 'wp_post' ); $wp_post_field->label = 'WP post'; $merge['wp_post'] = $wp_post_field; array_splice($return, 2, , $merge); return $return; } \IPS\Helpers\Form\Text hook : (for validation) /** * Validate * * @throws \InvalidArgumentException * @throws \DomainException * @return TRUE */ public function validate() { parent::validate(); if ($this->__get('name') == 'wp_post') { if (!in_array($this->value, \IPS\Settings::i()->getWpPosts())) { throw new \DomainException('L\'article sélectionné n\'existe pas.'); } } } \IPS\Settings hook : /** * WP posts * * @return array */ public function getWpPosts() { // @todo: get wp posts with curl return ['test', 'test2', 'windows']; } There are two things to do : save wp post to topic item in database and replace "Add tag" name here : I see addTokenText: ips.getString( 'add_tag' ) in \dev\js\framework\common\ui\ips.ui.autocomplete.js. I don't have any idea how to replace that. Do you have one? ^^
DreamOn Posted April 19, 2016 Author Posted April 19, 2016 Hello, What is the hook when new topic/new question is saving ? Thanks.
Adriano Faria Posted April 19, 2016 Posted April 19, 2016 2 hours ago, DreamOn said: What is the hook when new topic/new question is saving ? /** * Process created object BEFORE the object has been created * * @param array $values Values from form * @return void */ protected function processBeforeCreate( $values ) { } /** * Process created object AFTER the object has been created * * @param \IPS\Content\Comment|NULL $comment The first comment * @param array $values Values from form * @return void */ protected function processAfterCreate( $comment, $values ) { } Example: Adding some text to the topic title. /** * Process created object BEFORE the object has been created * * @param array $values Values from form * @return void */ protected function processBeforeCreate( $values ) { $this->title = 'Bla bla bla ' . $values['topic_title']; /* Run parent */ parent::processBeforeCreate( $values ); } Both same file class of the first script.
DreamOn Posted April 19, 2016 Author Posted April 19, 2016 Great @Adriano Faria, thank you very much ! Any idea for the "Add tag" text ? Unwittingly abusing your kindness
Adriano Faria Posted April 19, 2016 Posted April 19, 2016 I'm not really the person to help in JS. Do you want to add tags (via PHP) or do you have an issue on JS ?
DreamOn Posted April 19, 2016 Author Posted April 19, 2016 @Adriano Faria Regardless JS or PHP, I want to replace "Add tag" text by "Add WP post" for exemple. But it seems it is done with JS (I guess) : addTokenText: ips.getString( 'add_tag' ),
Recommended Posts
Archived
This topic is now archived and is closed to further replies.