Jump to content

Add custom field to "Create New Topic"


DreamOn

Recommended Posts

Posted

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.

Posted

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 :

2016-04-18 17_32_17-Cortana.png

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? ^^

Posted
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.

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...