Jump to content

Code hook at the end of the method


Sonya*

Recommended Posts

Posted

Hi,

I would like extend a method and need to execute my code AFTER the parent method is run but before it returns.

The method is searchResult(). I have to manipulate $status object BEFORE it used in template. 

	public static function searchResult( array $indexData, array $authorData, array $itemData, array $containerData = NULL, array $reputationData, $reviewRating, $iPostedIn, $view, $asItem, $canIgnoreComments=FALSE )
	{			
		$status = static::constructFromData( $itemData );
		$status->reputation = $reputationData;
		
		$profileOwner = isset( $itemData['profile'] ) ? $itemData['profile'] : NULL;
		$profileOwnerData = $profileOwner ?: $authorData;
		$status->_url = \IPS\Http\Url::internal( "app=core&module=members&controller=profile&id={$profileOwnerData['member_id']}&status={$itemData['status_id']}&type=status", 'front', 'profile', array( $profileOwnerData['members_seo_name'] ) );
        
        // ################ HERE ################

		return \IPS\Theme::i()->getTemplate( 'statuses', 'core', 'front' )->statusContainer( $status, $authorData, $profileOwner, $view == 'condensed' );
	}

It works, when I patch the code at the place marked with HERE. And now how do I add the code to the hook?

	static public function searchResult( $indexData, $authorData, $itemData, $containerData, $reputationData, $reviewRating, $iPostedIn, $view, $asItem, $canIgnoreComments=false )
	{                
		return call_user_func_array( 'parent::searchResult', func_get_args() );
	}

I cannot add it before return call_user_func_array as $status object does not exist yet. And I cannot add it after return logically.

Thanks.

Posted

Could you describe what you're trying to change in the status object?

There are several ways to manipulate the status object.

1. You could override status::constructFromData, set a custom variable inside of the searchResult method, then call parent::searchResult and customize the status::constructFromData response if your custom variable is set, which indicates that it was called from the searchResult method...

2. You could just override status::constructFromData and modify it always

3. You could create a theme hook to run custom code before the template is returned... ( That's the way which I would recommend ) 

Posted

You could create a Theme Hook (instead of Code Hook) and select core → front → statuses in the Template Group field.

2016-11-21_15-26-35.png

Open ipsThemeCoreFrStatuses.php file located in the hooks folder. Now you can create statusContainer() method which accepts nothing. Then use func_get_args() function to get all passed arguments, modify what you need and then return the parent method.

2016-11-21_16-07-31.png

 

This example replaces all statuses with 'something': ipsThemeCoreFrStatuses.php

Posted
5 hours ago, Daniel F said:

Could you describe what you're trying to change in the status object?

I have implemented tags functionality on status with code hooks. It works and is saved in search index. I can also search now for tags in statuses. The only thing I miss: displaying tags in template for activity stream statusContainer(). The tags are in $indexData['index_tags'] parameter of searchResult(). But $indexData is not used in template statusContainer (see the method above). That's why I need to add it to $status manually. 

Archived

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

  • Recently Browsing   0 members

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