Jump to content

How do we extend functionality contained within traits?


Go to solution Solved by Lindy,

Recommended Posts

2 hours ago, HeadStand said:

You CAN hook into it, as long as it's on a specific class. Meaning, you can hook into \IPS\forums\Topic, just not \IPS\Content\Reactable. Is that not sufficient for what you're doing?

 

Yeah, I can hook into specific classes but not the main one. For example I want to make all apps not save a point for the chosen reputation under certain circumstances. I'd have to go around making an hook on each class for each application I want to support. And I just can't go around adding support for any 3rd party apps 1 by 1. I could do it, but I won't. I'll use my time in better ways 🙂

Link to comment
Share on other sites

25 minutes ago, teraßyte said:

Yeah, I can hook into specific classes but not the main one. For example I want to make all apps not save a point for the chosen reputation under certain circumstances. I'd have to go around making an hook on each class for each application I want to support. And I just can't go around adding support for any 3rd party apps 1 by 1. I could do it, but I won't. I'll use my time in better ways 🙂

have you tried telling the class to use the class method instead of the traits method? like:

trait FooTrait {
	public function bar(){
		return "hello, i'm a trait method!";
	}
}
class myhook extends HOOK_TEMP
{
	use FooTrait {
		bar as traitBar;
	}

	public function bar()
	{
		return "I am not the trait method!";
	}
}

i haven't tested this, but i can think of a potential problem or two. 

Link to comment
Share on other sites

  • 1 year later...
  • 2 years later...
  • 4 months later...

...and almost 6 years later, nothing has changed. I need to hook in \IPS\Content\Reactable::removeReaction() and I can't.

I can't add this method to every content item model basically because I don't which apps will be installed on every board out there.

Does anyone know any workaround for this?

Edited by Adriano Faria
Link to comment
Share on other sites

@Adriano Faria In the past, I overloaded the function \IPS\Member::__set() checking for the key pp_reputation_points and then checking if the do parameter starts with (un)react. That should cover all areas I believe.

# We're updating the 'pp_reputation_points' value and we're (un)reacting
if ( $key == 'pp_reputation_points' AND \IPS\Request::i()->do AND ( mb_substr( \IPS\Request::i()->do, 0, 5 ) === 'react' OR mb_substr( \IPS\Request::i()->do, 0, 7 ) === 'unreact' ) AND \IPS\Member::loggedIn()->member_id )
{
	// Your code
}

You also need to account for when the reputation is rebuilt though. That check is not included in the example above. It requires hooking the function that rebuilds the count and passing along some kind of flag.

 

If you want to check only removeReaction() skip the do=reactXXX check. That said, this kind of code doesn't let you know which reaction is actually being removed. It only acts on the points being updated. If you require to run it on specific reactions only, I have no idea how, unfortunately.

Edited by teraßyte
Link to comment
Share on other sites

  • Recently Browsing   0 members

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