Results 1 to 5 of 5

Thread: How to use hooks

  1. #1
    User
    Join Date
    08-15-09.
    Location
    Sweden
    Posts
    82

    Default How to use hooks

    Hi,
    Guess this is really simple, but if you never have done it before, it sure helps if someone points you in the right direction.

    I need to write a custom php-script that fires each time a new frontend member is registered. The values I need to get is: first name, last name and e-mail.

    I understand that I need to use the processFormData hook. I have never used hooks before and therefore need a little bit of help. The only information I found is this syntax:

    Code:
    // config.php
    $GLOBALS['TL_HOOKS']['processFormData'][] = array('MyClass', 'myProcessFormData');
     
    // MyClass.php
    public function myProcessFormData($arrPost, $arrForm, $arrFiles)
    {
    // Do something
    }
    But from here I don't know where to go. :? Where is the config.php and MyClass.php files that I need to alter? And how do I write the function to get the name and e-mail fields?

    Thanks in advance!

  2. #2
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: How to use hooks

    Hi Ola,

    From someone who was exactly where you are just a few weeks ago, I'd be happy to share what I've learned so far...

    First off, it sounds like what you are describing would use the 'createNewUser' hook instead -- it's on the Hooks page of the Website as well, and has a very similar syntax.

    I would also read Tru's blog series on writing a custom module. In your case the first part of his series would be useful -- where he creates the file and folder structure of his custom module, and explains the logic behind why those are set up the way they are -- http://blog.qzminski.com/2010/04/create ... -part-one/.

    You'd essentially be creating a custom module that has two files:

    /system/modules/myCustomModule/config/config.php, Which Contains:
    Code:
    $GLOBALS['TL_HOOKS']['createNewUser'][] = array('myCustomModuleClass', 'myCustomClassMethod');
    /system/modules/myCustomModule/myCustomModuleClass.php, Which Contains:
    Code:
    class myCustomModuleClass // Make sure this class name matches the first item in the above line of code
    {
    	public function __construct() {}
    	
    	public function myCustomClassMethod($intId, $arrData) // And make sure this method matches as well
    	{	
    		print_r($intId); // Print the ID of the new User
    		print_r($arrData); // Print out the user's data, which should include the fields you need.
    	}	
    }
    And then that method would automatically run every time a new user registers.

    Does that help?
    Brian

  3. #3
    User
    Join Date
    08-15-09.
    Location
    Sweden
    Posts
    82

    Default Re: How to use hooks

    Hi Medianomaly,

    Very kind of you! This should be what I need to get things working. You should copy this information to the tutorial section in this forum for future reference.

    One last question though: Is the only way to use hooks by building custom modules? Is it not possible to declare functions that uses hooks in localconfig.php or some other file?

  4. #4
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: How to use hooks

    I don't think it's the only way -- you could probably paste all the code into localconfig.php and have it work just the same, but making a module does seem to be the "recommended" way of doing it.

    It looks like you do need to call a method of a class from your hook (instead of a direct call to a function), but I don't know if there's a way around that.
    Brian

  5. #5
    User
    Join Date
    07-26-09.
    Posts
    175

    Default Re: How to use hooks

    Just a lil note: if I remember good, the some HOOKs may not display the data you want to print, due to redirection or something. What you can do is to call a fictional class or simply put the exit; statement at the end of the HOOK code:
    [code=php:2r6yvkbz]<span class="syntaxdefault">
    public*function*someClass</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$arrData</span><span class="syntaxkeyword">)
    {
    </span><span class="syntaxdefault">****print_r</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$arrData</span><span class="syntaxkeyword">);

    </span><span class="syntaxdefault">****new*Asd</span><span class="syntaxkeyword">();</span><span class="syntaxdefault">*</span><span class="syntaxcomment">//*or
    </span><span class="syntaxdefault">****exit</span><span class="syntaxkeyword">;
    }
    </span><span class="syntaxdefault">*</span>[/code:2r6yvkbz]

    And according to medianomaly's code, there is no need to put
    [code=php:2r6yvkbz]<span class="syntaxdefault">
    public*function*__construct</span><span class="syntaxkeyword">()</span><span class="syntaxdefault">*</span><span class="syntaxkeyword">{}
    </span><span class="syntaxdefault">*</span>[/code:2r6yvkbz]
    and you can also extend your class with e.g. Backend, that will give you a possibility to connect to the database. I don't know if it's safe though :roll:

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •