Jump to content

Plugin Hooks vs Application Hooks


Aiwa
 Share

Go to solution Solved by Aiwa,

Recommended Posts

There appear to be limitations on the plugins hook system vs application hook system.  

For example, setting the default tab on profile view.  The following works with an Application.  Extending \IPS\core\modules\front\members\profile.  Ultimately, the manage() method looks to see if a tab has already been set.  If so, display it.  Using the below, I can modify the request parameter for tab and set it to what I want. 

	protected function manage()
	{
		try
		{
			if(\IPS\Settings::i()->steam_default_tab && !isset(\IPS\Request::i()->tab))
			{
				\IPS\Request::i()->tab = 'node_steam_steamprofile';
			}
			return parent::manage();
		}
		catch ( \RuntimeException $e )
		{
			if ( method_exists( get_parent_class(), __FUNCTION__ ) )
			{
				return parent::manage();
			}
			else
			{
				throw $e;
			}
		}
	}

Let's follow this through \IPS\core\modules\front\members\profile manage() and see what \IPS\Request::i()->tab has it it... You'll see the added variable $request has the correct 'node_steam_steamprofile' profile extension in it and we're setting the active tab to what came in with \IPS\Request

5a7ff013091c2_0211h02266la4i.png.0976923df422a03c35182bb1da6bb065.png

 

Let's look at Plugins.... Same thing. Extending \IPS\core\modules\front\members\profile... Only difference in the code is the IF statement, removes a setting bit specific to the previous application, and instead of hard-coding a value for 'tab', I'm using a setting.  I've checked the DB, the setting exists and is populated. See below for screenshot.

	protected function manage()
	{
		try
		{
			if(!isset(\IPS\Request::i()->tab))
			{
				\IPS\Request::i()->tab = \IPS\Settings::i()->dpt_default;
			}
			return parent::manage();
		}
		catch ( \RuntimeException $e )
		{
			if ( method_exists( get_parent_class(), __FUNCTION__ ) )
			{
				return parent::manage();
			}
			else
			{
				throw $e;
			}
		}
	}

Let's follow this through \IPS\core\modules\front\members\profile manage() and see what \IPS\Request::i()->tab has it it... NULL.  At which point we'll always get 'Activity' as the default tab.

5a7ff04a258bb_0211h02530d0l5.png.e55e60f708f9845a4d30daf51e81718e.png

5a7ff322644d7_0211h02085l4g7.png.70e4d342404e9df99b95ad899f7e003a.png

Any comment on the scope of those limitations?  

When we should always use an application vs when it's possible to use a plugin?

Pre-requisites that have to be met for a plugin?  

Otherwise, it looks to me like anything that would rely on a code hook needs to be an Application. Including something that could be done with only a handful of lines of code.

 

 

Edited by Aiwa
Link to comment
Share on other sites

The hooks system functions independent of plugins vs applications, as in hooks are a different "thing" that both can make use of. Behind the scenes they are mapped and loaded the exact same way.

IOW, what you are showing should work, so there must be another factor at play.

1) Check your hooks.php file in /plugins/ and make sure (a) this hook is mapped and set to be loaded properly, and (b) there are no other hooks that may be causing issues

2) After that, I would start debugging. See if your plugin hook file is loaded. var_dump() the setting value to make sure it's set properly, and so on.

Link to comment
Share on other sites

  • Solution
13 hours ago, bfarber said:

The hooks system functions independent of plugins vs applications, as in hooks are a different "thing" that both can make use of. Behind the scenes they are mapped and loaded the exact same way.

IOW, what you are showing should work, so there must be another factor at play.

1) Check your hooks.php file in /plugins/ and make sure (a) this hook is mapped and set to be loaded properly, and (b) there are no other hooks that may be causing issues

2) After that, I would start debugging. See if your plugin hook file is loaded. var_dump() the setting value to make sure it's set properly, and so on.

hooks.php

<?php
return array (
  '\\IPS\\core\\modules\\front\\members\\Profile' => 
  array (
    293 => 
    array (
      'file' => 'plugins/defaultprofiletab/hooks/defaultProfileTab.php',
      'class' => 'hook293',
    ),
  ),
);

Lesson learned, don't create hooks in the middle of the night and let your keyboard autocorrect / auto-capitalize things for you. 

\IPS\core\module\front\members\profile    not   Profile  

Thanks @All Astronauts and @bfarber for saving my sanity.

Oddly enough, the hook editor isn't smart enough to solve this for you.  When i hit edit, it knows what class to pull, but doesn't fix anything for you.  You'd think this would be smart enough to throw an error rather than make you think it's working.

5a8255dec0cab_0212h0247ppnhp.thumb.png.ef293115009989f9b5876b741e027867.png

Edited by Aiwa
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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