Jump to content

ItemTopic trait


Recommended Posts

I implemented the IPS\Content\ItemTopic trait in my apps but two of them has a particularity: the topic should not be created right after the item is submitted; user must add questions/answers or prizes then click in a button so the topic is created. I mean, that's how it was on <4.3 because I didn't have the syncTopic(() on createFromForm; I had it on my controller function.

So the question is: how can I prevent the topic from being created right after submit the item when using the ItemTopic trait? I can't return parent in createFromForm on my item model, otherwise the topic will be created.

Tks.

Link to comment
Share on other sites

	public static function createFromForm( $values, \IPS\Node\Model $container = NULL, $sendNotification = TRUE )
	{
		$containercolumn = static::containerForumIdColumn();

		$item = parent::createFromForm( $values, $container, $sendNotification );
		if ( \IPS\Application::appIsEnabled('forums') and $item->container()->$containercolumn and !$item->hidden() )
		{
			$item->syncTopic();
		}
		return $item;
	}

There's no default/built-in way to do what you are talking about, but you might be able to fudge it. You could unset the forum ID value in the container temporarily and then set it back afterwards, for instance.

Link to comment
Share on other sites

If you overload the createFromForm method in your class, then the one in the trait will not run (see \IPS\downloads\File where we need to alias the changeAuthor method from the trait in order to call it, because that class defines it's own changeAuthor method).

See:

Link to comment
Share on other sites

	public static function createFromForm( $values, \IPS\Node\Model $container = NULL, $sendNotification = TRUE )
	{
		return parent::createFromForm( $values, $container, $sendNotification );
	}

In this case, the "parent" here is \IPS\Content\Item (or whatever class you're extending), not the trait, so syncTopic will not run. Think of methods from traits as "fillers." If the method doesn't exist in the class using the trait, then the trait fills it in. If it does, then it does nothing unless you resolve the conflict (like we do in \IPS\downloads\File).

Link to comment
Share on other sites

21 hours ago, Ryan Ashbrook said:

In this case, the "parent" here is \IPS\Content\Item (or whatever class you're extending), not the trait, so syncTopic will not run. Think of methods from traits as "fillers." If the method doesn't exist in the class using the trait, then the trait fills it in. If it does, then it does nothing unless you resolve the conflict (like we do in \IPS\downloads\File).

No luck. Just tried and it still creates the topic. Regarding Downloads, I don't see how it was resolved there. Can you elaborate?

22 hours ago, bfarber said:

There's no default/built-in way to do what you are talking about, but you might be able to fudge it. You could unset the forum ID value in the container temporarily and then set it back afterwards, for instance.

No luck too. Topic created.

Link to comment
Share on other sites

Archived

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

  • Recently Browsing   0 members

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