Dependency injection

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
MarkDHamill
Registered User
Posts: 3808
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Dependency injection

Post by MarkDHamill » Wed May 08, 2019 5:15 pm

There is an event in /incluces/acp/acp_email.php that interests me:

Code: Select all

		/**
		* Modify custom email template data before we display the form
		*
		* @event core.acp_email_display
		* @var	array	template_data		Array with template data assigned to email template
		* @var	array	exclude				Array with groups which are excluded from group selection
		* @var	array	usernames			Usernames which will be displayed in form
		*
		* @since 3.1.4-RC1
		*/
		$vars = array('template_data', 'exclude', 'usernames');
		extract($phpbb_dispatcher->trigger_event('core.acp_email_display', compact($vars)));

		$template->assign_vars($template_data);
My question is how to modify my services.yml page to carry over this data so I can use the event.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

User avatar
david63
Registered User
Posts: 16195
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Dependency injection

Post by david63 » Wed May 08, 2019 5:39 pm

Where do you want to use the event? Events can only be used in listeners so if you want that data otside of the listener you will need to save it in the listener and retrieve it at the place where you are going to use it.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
MarkDHamill
Registered User
Posts: 3808
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: Dependency injection

Post by MarkDHamill » Wed May 08, 2019 7:07 pm

I am using it in /event/main_listener.php. I've added my function to the subscribed events and tried various approaches to adding these parameters to my services.yml file, currently adding:

Code: Select all

            - '%template_data%'
            - '%exclude%'
            - '%usernames%'
and trying to hook them in:

Code: Select all

	public function __construct(\phpbb\language\language $language, \phpbb\controller\helper $helper, \phpbb\template\template $template, $php_ext, $template_data, $exclude, $usernames)
	{
		$this->language = $language;
		$this->helper   = $helper;
		$this->template = $template;
		$this->php_ext  = $php_ext;
		$this->template_data = $template_data;
		$this->exclude = $exclude;
		$this->usernames = $usernames;
	}
getting an error like this:
Exception: The service "phpbbservices.selectivemassemails.listener" has a dependency on a non-existent parameter "template_data". Did you mean this: "tables.styles_template_data"?
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 25196
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Dependency injection

Post by Paul » Wed May 08, 2019 7:52 pm

You don't inject it into the constructor, it is part of the array for the variable that gets included in the method call to the method you provided for that event.
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

User avatar
MarkDHamill
Registered User
Posts: 3808
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: Dependency injection

Post by MarkDHamill » Wed May 08, 2019 8:26 pm

Thank you. Getting there.

I notice in /includes/acp/acp_email.php the event is triggered like this:

Code: Select all

	$vars = array('template_data', 'exclude', 'usernames');
	extract($phpbb_dispatcher->trigger_event('core.acp_email_display', compact($vars)));
So my listener is coded as follows:

Code: Select all

	public function acp_email_display($template_data, $exclude, $usernames)
	{

		$template_data['S_GROUP_OPTIONS'][] = '<option value="-1">' . $this->language->lang('SELECTIVEMASSEMAILS_SELECTED') . '</option>';;

	}
I get this:
[phpBB Debug] PHP Notice: in file [ROOT]/ext/phpbbservices/selectivemassemails/event/main_listener.php on line 128: Indirect modification of overloaded element of phpbb/event/data has no effect
Because $template_data basically doesn't exist and no data is being passed.

I was thinking maybe I needed to add the $event object as a parameter

Code: Select all

	public function acp_email_display($event, $template_data, $exclude, $usernames)
or just pass $vars and extract that but none of this works.

I'm probably missing something basic about how events work.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 25196
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Dependency injection

Post by Paul » Wed May 08, 2019 8:53 pm

You only get one parameter, which is an array that contains as keys the fields from the event, and as values the actual values of those variables.
I suggest you read the extension development documentation, which can be found at area51
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

User avatar
MarkDHamill
Registered User
Posts: 3808
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: Dependency injection

Post by MarkDHamill » Wed May 08, 2019 10:47 pm

Thank you. It looks like for core events you get the $event object and that's it so you must assert it and only it as a parameter.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

Post Reply

Return to “Extension Writers Discussion”