Is there an idiots guide?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
eeji
Jr. Style Validator
Posts: 1251
Joined: Fri Dec 12, 2008 9:08 pm
Location: Manchester, UK
Contact:

Is there an idiots guide?

Post by eeji »

I'm wanting to add a few bits and bobs to the core phpBB code but want to do it via an extension. The problem is, I don't have a clue what I'm doing. I've read the wiki, the writing guide, and downloaded the hello world example but still have no idea where to start.

I heavily modified 3.0.x core to suit my needs and I'm fine with procedural PHP, but with the new object orientated approach I'm a bit stumped.

The main thing I want to start with is adding a bit of code into common.php and define a few new template variables.
My phpBB styles: phpbbstyles.oo.gd
My board: iansvivarium.com
User avatar
MuhClaren
Registered User
Posts: 126
Joined: Sat Feb 21, 2015 5:23 am

Re: Is there an idiots guide?

Post by MuhClaren »

Let's start with your requirements in order to determine where to begin and what services you might need.

What would you like the new template vars to provide? Where will you call them? For example, perhaps you'd like to use a new template variable to display the current user's IP address on the viewforum.php page.

Based on your requirements, we can determine how to build the extension. If you haven't yet reviewed the list of core events, check them out here. These are events which an extension can subscribe to using an event listener. When the event occurs, it triggers the method(s) bound to the event in the extension event listener. At such time, your extension then has access to the various "arguments" listed for that particular event, which essentially exposes phpbb system data for your extension to manipulate.

Also available to you are a variety of services, which are basically global variables, made available to the extension. For instance, the config service, which allows your extension an easy pathway to work with the phpbb config. Another example is the user service, which is also very commonly used by extensions.Another is the request service. While an official list of services isn't readily available, nicofuma has an extension used for debugging other extensions, and it can help provide such thing.

A really great place to begin, is with existing extensions. Find one which does the same, or similar objective as you want, and tear it open to examine the code. Some of the best code, in my opinion, can be found in extensions written by guys like Paybas, VSE, nicofuma, Rich McGirr, etc. so these are the examples I look at first if I am stuck.
User avatar
Lumpy Burgertushie
Registered User
Posts: 67986
Joined: Mon May 02, 2005 3:11 am
Contact:

Re: Is there an idiots guide?

Post by Lumpy Burgertushie »

very good post MuhClaren. well done.

robert
I'm baaaaaccckkkk. still doing work on donation basis. PM your needs.

Premium phpBB 3.3 Styles by PlanetStyles.net

If nobody is in the forest, does a tree really fall?
User avatar
eeji
Jr. Style Validator
Posts: 1251
Joined: Fri Dec 12, 2008 9:08 pm
Location: Manchester, UK
Contact:

Re: Is there an idiots guide?

Post by eeji »

I am building my whole site around phpBB with the forum as just a small part so I need overall_header.html and navbar_header.html to know which page they are on.

In 3.0.x I determined which directory the page was in and assigned a template variable to use in <!-- IF .... --> statements so relevant navigation can be used or a particular script be loaded etc.

eg...

Code: Select all

    <div class="navigation">
    	<ul>
	    <li<!-- IF HOME --> class="active"<!-- ENDIF -->><a href="{ROOT}">Home</a></li>

	    <li<!-- IF FORUM --> class="active"<!-- ENDIF -->><a href="{U_INDEX}">{L_FORUM}</a></li>

	    <li<!-- IF MORPHGUIDE --> class="active"<!-- ENDIF -->><a href="{ROOT}morphs">Cornsnake Morphs</a></li>  

	    <li<!-- IF PUNNETT --> class="active"<!-- ENDIF -->><a href="{ROOT}punnett">Punnett Square Calculator</a></li>

	    <li<!-- IF BREEDERS --> class="active"<!-- ENDIF -->><a href="{ROOT}breeders">Breeders</a></li>
...as well as other page specific bits and bobs
My phpBB styles: phpbbstyles.oo.gd
My board: iansvivarium.com
User avatar
Lumpy Burgertushie
Registered User
Posts: 67986
Joined: Mon May 02, 2005 3:11 am
Contact:

Re: Is there an idiots guide?

Post by Lumpy Burgertushie »

not sure how that way ever worked in phpbb 3.0 because this was the proper way and still is:

Code: Select all

<!-- IF SCRIPT_NAME eq index -->
Insert your code here
<!--  ENDIF -->
or

Code: Select all

<!-- IF SCRIPT_NAME eq home -->
Insert your code here
<!--  ENDIF -->
or

Code: Select all

<!-- IF SCRIPT_NAME eq forum -->
Insert your code here
<!--  ENDIF -->
etc. etc.


robert
I'm baaaaaccckkkk. still doing work on donation basis. PM your needs.

Premium phpBB 3.3 Styles by PlanetStyles.net

If nobody is in the forest, does a tree really fall?
User avatar
eeji
Jr. Style Validator
Posts: 1251
Joined: Fri Dec 12, 2008 9:08 pm
Location: Manchester, UK
Contact:

Re: Is there an idiots guide?

Post by eeji »

I couldn't use that because each section of the site is in a sub directory of the root so most of the script names are 'index' :(

Code: Select all

http://www.example.com/this_is_the_bit_i_need/index.php
My phpBB styles: phpbbstyles.oo.gd
My board: iansvivarium.com
User avatar
MuhClaren
Registered User
Posts: 126
Joined: Sat Feb 21, 2015 5:23 am

Re: Is there an idiots guide?

Post by MuhClaren »

eeji wrote:I couldn't use that because each section of the site is in a sub directory of the root so most of the script names are 'index' :(

Code: Select all

http://www.example.com/this_is_the_bit_i_need/index.php
If each index.php script is unique, you could set a template variable in each script, and check for it in your markup.

For a simplified example:

http://www.yoursite.com/this_is_section_one/index.php

Code: Select all

// set the template variable
$this->template->assign_vars(
					[
						'SECTION_ONE'        => TRUE,
					]
				);
http://www.yoursite.com/this_is_section_two/index.php

Code: Select all

// set the template variable
$this->template->assign_vars(
					[
						'SECTION_TWO'        => TRUE,
					]
				);
Then in your overall_header or your navbar HTML test for this template variable and conditionally do something:

Code: Select all

{% if SECTION_ONE %}
	<div class="section_one">
		<h3>{{ lang('SECTION_ONE_TITLE') }}</h3>
		<p>{{ SECTION_ONE_CONTENT }}</p>
	</div>
{% elseif SECTION_TWO %}
	<div class="section_two">
		<h3>{{ lang('SECTION_TWO_TITLE') }}</h3>
		<p>{{ SECTION_TWO_CONTENT }}</p>
	</div>
{% endif %}
I prefer to use TWIG instead of the old phpbb template language, which is converted by the back-end into TWIG anyways. You may choose to use the old style, just keep in mind that while they are functionally equivalent, there will eventually be a time when the old style is phased out lieu TWIG. Either way, the idea is the same when it comes to setting a template variable as an identifier and checking for it in your markup.
User avatar
eeji
Jr. Style Validator
Posts: 1251
Joined: Fri Dec 12, 2008 9:08 pm
Location: Manchester, UK
Contact:

Re: Is there an idiots guide?

Post by eeji »

That is how I have it set up in my 3.0.x site at the moment, but ideally for 3.1.x I don't want to make any alterations to the core phpBB code (this would be needed to identify forum pages) to make updating super simple. I know an extension for a few lines of code may seem a bit over the top, but it will also help me in the future if anything more complicated needs doing.
My phpBB styles: phpbbstyles.oo.gd
My board: iansvivarium.com
User avatar
MuhClaren
Registered User
Posts: 126
Joined: Sat Feb 21, 2015 5:23 am

Re: Is there an idiots guide?

Post by MuhClaren »

eeji wrote:That is how I have it set up in my 3.0.x site at the moment, but ideally for 3.1.x I don't want to make any alterations to the core phpBB code (this would be needed to identify forum pages) to make updating super simple. I know an extension for a few lines of code may seem a bit over the top, but it will also help me in the future if anything more complicated needs doing.
You can certainly achieve that using an extension with an event listener bound to an event that lets you inject the page identification variables (template variables) into the PHP.
Post Reply

Return to “Extension Writers Discussion”