PHP Error using language related functions

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
friedfry
Registered User
Posts: 6
Joined: Sun Nov 04, 2018 6:54 pm

PHP Error using language related functions

Post by friedfry »

Hey guys,
I'm writing my first extension right now and have troubles with getting the language file loaded or using the language file I created.
So I worked myself through the tutorial and modified it a bit, trying to make a MCP module, which should later show the contents of a mysql database. Unfortunatly there is a PHP error while calling the add_lang() or load() function, here the error log from apache:

Code: Select all

[Sun Nov 04 19:13:53.510943 2018] [php7:error] [pid 23380] [client 127.0.0.1:60750] PHP Fatal error:  Uncaught Error: Call to a member function add_lang() on null in /srv/http/illumina-phpbb3/ext/illumina/chemtags/mcp/main_module.php:16\nStack trace:\n#0 /srv/http/illumina-phpbb3/includes/functions_module.php(676): illumina\\chemtags\\mcp\\main_module->main('\\\\illumina\\\\chemt...', 'settings')\n#1 /srv/http/illumina-phpbb3/mcp.php(312): p_master->load_active()\n#2 {main}\n  thrown in /srv/http/illumina-phpbb3/ext/illumina/chemtags/mcp/main_module.php on line 16, referer: http://127.0.0.1/illumina-phpbb3/mcp.php?sid=25243112b1c3ce08b2230e66feb3451f&i=149
And the corresponding code piece:

Code: Select all

public function main($id, $mode)
	{
		global $language, $template, $request, $config;

		// Load a single language file from illumina/chemtags/language/en/common.php
		$language->add_lang(‘info_mcp_chemtags’, ‘illumina/chemtags’);

		$this->tpl_name = 'mcp_chemtags_body';
		$this->page_title = $language->lang('MCP_CHEMTAGS_TITLE');
		//$this->page_title = 'testkacke';
		
	
        	add_form_key('illumina_chemtags_settings');
	
        	if ($request->is_set_post('submit'))
        	{
        	    if (!check_form_key('illumina_chemtags_settings')) // check form key, to verify a valid form
        	    {
					trigger_error('FORM_INVALID');
	            }
	
	            $config->set('illumina_chemtags_goodbye', $request->variable('illumina_chemtags_goodbye', 0));
				trigger_error(/*$language->lang('MCP_CHEMTAGS_SETTING_SAVED')*/'Settings saved' /*. mcp_back_link($this->u_action)*/);// TODO: backlink
        	}
I uploaded the complete extension to github: https://github.com/fried-gluttony/illu- ... a/chemtags
I assume something is maybe wrong about the namespace or so, that it doesn't find the function, but also adding the corresponding namespace in front made no success. I'm also not really a PHP programmer and just learning it by the way, even if most syntax is selfexplaining (I'm a bit used to Java and more to C).
I was searching quite a bit and tried fiddling around with it, but had no success. Oh and maybe another question: I'm having trouble finding correct functions, is there any reference documentation or so besideds area51.phpbb.com ? E.g. I'm currently looking for the mcp variant of "acp_back_link()" or similar.
I'm appreciating any help from you, thanks!

P.S: phpBB 3.2.2 and PHP Version 7.2.11 are being used.
User avatar
Restless Rancor
Registered User
Posts: 196
Joined: Tue Sep 18, 2018 1:51 pm

Re: PHP Error using language related functions

Post by Restless Rancor »

On github your language directory is in mcp/language, does moving it up a directory help?
These are not the droids you're looking for...
friedfry
Registered User
Posts: 6
Joined: Sun Nov 04, 2018 6:54 pm

Re: PHP Error using language related functions

Post by friedfry »

It's also in the "en" folder, I just moved it to language as a try. But the error somehow seems like the function is not found.
User avatar
kasimi
Former Team Member
Posts: 4900
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: PHP Error using language related functions

Post by kasimi »

friedfry wrote: Sun Nov 04, 2018 7:09 pm

Code: Select all

global $language
The language service is not available globally, you need to get it from the container:

Code: Select all

global $phpbb_container;
$language = $phpbb_container->get('language');
friedfry
Registered User
Posts: 6
Joined: Sun Nov 04, 2018 6:54 pm

Re: PHP Error using language related functions

Post by friedfry »

Oh, sorry and thanks Restless Rancor, you were right, it should be one directory up, I thought you meant the lanuage file.
And also thanks kasimi, that was the missing point, thank you very much, it works now.
User avatar
kasimi
Former Team Member
Posts: 4900
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: PHP Error using language related functions

Post by kasimi »

One more thing: info_*.php language files are loaded automatically by phpBB. In order to not load unneeded language files throughout your xCP, a common way to solve this is to put only the module title (and log messages, if you have any) in the info_ language file, and move all other language strings into a second language file that you load explicitly in your module with the language service.
User avatar
Restless Rancor
Registered User
Posts: 196
Joined: Tue Sep 18, 2018 1:51 pm

Re: PHP Error using language related functions

Post by Restless Rancor »

No problem, I found the Skeleton Extension very useful when I started writing my own extensions a couple of weeks ago. The base files can be generated and you can work on them from there :)
These are not the droids you're looking for...
friedfry
Registered User
Posts: 6
Joined: Sun Nov 04, 2018 6:54 pm

Re: PHP Error using language related functions

Post by friedfry »

kasimi wrote: Sun Nov 04, 2018 8:45 pm One more thing: info_*.php language files are loaded automatically by phpBB. In order to not load unneeded language files throughout your xCP, a common way to solve this is to put only the module title (and log messages, if you have any) in the info_ language file, and move all other language strings into a second language file that you load explicitly in your module with the language service.
Ah yeah, thanks for the reminder, it was just an act of despair making it global. Now renamed it
@Restless Rancor: yeah, I've seen it but somehow wanted to go through the starting process to get some knowledge about how the things work.
What other resources do you use for the steps after the tutorial/skeleton?
User avatar
Restless Rancor
Registered User
Posts: 196
Joined: Tue Sep 18, 2018 1:51 pm

Re: PHP Error using language related functions

Post by Restless Rancor »

I started by running through the extension documentation to create the acme/demo extension, but when I fell into trouble trying to create an ACP module I used the Skeleton Extension and was able to check the skeleton's working code against my own to see where I had gone wrong.

The Event List is also handy for finding the right event.

Other than that, trial and error to get things to work. Seeing how other extensions are functioning was also extremely helpful. If you're unsure how to do something and know an extension that does something similar, you can have a look at it's coding to see what's happening. And, if all else fails it doesn't hurt to ask in these forums.

Something I can't recommend enough though is using a localhost installation (with a program such as XAMPP) as this cuts out the need to constantly upload changes via FTP- just edit and refresh.

I'm still very much in the first steps as you are, I was playing around trying to add a log in the Forum Logs for when someone downloads an attachment yesterday, but didn't get very far. I'll give another crack at it this week.
These are not the droids you're looking for...
Post Reply

Return to “Extension Writers Discussion”