We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

writing extensions - Yikes! (for a nOOb)

Development and Programming

vinci's avatar
vinci
85 posts
17 years ago
vinci's avatar vinci

So… Ive been hacking, dicing and chopping bits of templates, playing with embedded codes… but what id really like to do is learn how to write and extention. Ive managed to write a module easily enough, but for some reason I cant get my head around how an extension works, how the hooks are used, where they go… And yes, ive read the wonderfully-written-very easy-to-understand-but-just-not-me-when-im-tired-and-frustrated tutorial (ive read it about 20 times)

so can anyone give me some time and explain in laymen’s terms hows these things work?

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones
And yes, ive read the horridly-written-badly-spelt-only-ubergeek-can-understand-maybe-written-in-a-rush-and-leaves-lots-of-things-out tutorial (ive read it about 20 times)

Notwithstanding the fact that you aren’t appealing to my kinder nature with that statement, have you started with Plugins or Modules? Quite frankly, writing Extensions is not for the layperson, so there really aren’t laymen’s terms that can be used. You have to have familiarity with ExpressionEngine’s core code as well as and understanding of the Extension API for anything to begin to click.

I’d begin with defining your goals clearly. What do you want your extension to do? With that basic piece of information, I can try to give you some general direction on setting up your extension and seeing how, where, and why it’s triggered.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

Let us know what you are trying to accomplish…I just wrote my first one a month ago and would be happy to share my experience with you.

       
vinci's avatar
vinci
85 posts
17 years ago
vinci's avatar vinci

Apologies, it was the wee early hours when I wrote that. frustrated and tired which makes me a mean impatient person with horns and a pitchfork.

as for goals - i dont have any at the moment. but lets say for example, id want to take a SAEF form and process it further before letting it be submitted to the DB. I think theres a hook for that.

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

You can find all available hooks here. The one you would want for that task is weblog_standalone_insert_entry.

Find it in the code in mod.weblog_standalone.php so you can see what is happening before and after, as well as the intent of the extension, which in this case, is to allow you to modify the POST data, or perform other tasks as you see fit. Your extension’s method using this hook receives no arguments to deal with, and doesn’t need to return any values. The most basic use of this hook would simply overwrite values in the $_POST array as desired to achieve your goals.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

Step one for me was getting the installation and uninstallation working properly. That way you can get yourself out of trouble if something goes wrong.

       
vinci's avatar
vinci
85 posts
17 years ago
vinci's avatar vinci

Thanks guys. I really appreciate the help on this. FYI- Ive managed to write a small plugin that works very sweetly. Its just a little code to work with the Form Helper plugin to get the custom fields and populate them correctly without having to enter each custom field in manually. It also arranges the categories in a nested manner. Also the categories method works with a module I wrote, which restricts members to certain category hierarchy. So in the edit form, only the categories (and its subcategories) to which a user has permissions appear.

That said, I have come up with the need to write and extension - and it should be quite a simple one (though probably not for my poor brain). I would like to alter the ‘file upload url’ in the standalone edit form:
The hook is: weblog_standalone_form_upload_url

So to start with, I sort out class, constructor, the activate/deactivate methods - fine, all easy enough.

Secondly, I write a method which takes over from the hook, untill it is told to return to the main code…. erm, this is where I get stuck.

take for example the tutorial’s examples:

“The following is an example of an ExpressionEngine Extension Hook that is available for use:”

// -------------------------------------------
// 'publish_form_start' hook.
//  - Allows complete rewrite of Publish page.
//
$edata = $EXT->call_extension('publish_form_start', $which, $submission_error, entry_id);
if ($EXT->end_script === TRUE) return;
//
// -------------------------------------------

“Here is what the method might look like:”

function replace_entry_form($which, $submission_error, $entry_id)
{
    global $DSP, $DB, $IN, $EXT;
    
    $EXT->end_script = TRUE;

    $DSP->body = 'New Entry Form Here';
}

So my question is how does EE know when to call the extension?

I’ll try to answer my own question: When it hits the hook, it looks at the extensions DB table and finds if there is an extension for the hook, and loads it.

So for the above example, the activate_extension method would include:

method'       => "replace_entry_form",
'hook'        => "publish_form_start",

would that be correct?

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

The method can be any name you like, but you have the hook exactly right.

       
vinci's avatar
vinci
85 posts
17 years ago
vinci's avatar vinci

ok, so then how does one use the variables in:

$edata = $EXT->call_extension('publish_form_start', $which, $submission_error, entry_id);
       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

All hooks are loaded from the DB at once, so it’s not querying the database each time a hook is encountered. To use variables made available to a hook, simply accept them as arguments of your method. For the example above, you could use:

function foo($which, $submission_error, $entry_id)

Of course, the name on your method’s end can be whatever you want, but the point being demonstrated is that this is the order that they will be sent to your method.

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
17 years ago
Leevi Graham's avatar Leevi Graham

I found the best way to learn how to write extensions was to check out already published ones.

Personally I started with Mark Huots extensions and modules trying to break them down while comparing them to the documentation.

The latest version of LG Better Meta might help you if your extension involves tabs and outputting content using plugins.

The code is fully commented using phpDoc syntax should it is a bit easier to understand. It also demonstrates how to upgrade an extension and use language files.

Hope it helps..

The only other advice I can give is ask specific questions in the forum and follow the dev guidelines. Also if you can try and submit the official repository. Derek and the team knocked one of my extensions back a couple of times but in the end I learnt a lot more about security and best practices in EE.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.