4

I have a safecracker form that submits an entry. The form consists of title, url_title, and description. I want to create an extension hook that filters out certain words if they exist in the title of the entry.

I already have a function that take care of the cleaning function clean(){....}. I understand that we need to use an extension hook so we can clean the title upon saving the entry.

What extension hook do i need to use for that. can you give me a complete example of an extension hook. I'm very good with PHP but still new to hooks and how they should be implemented. I already read the EE documentation but still find some confusion of how a hook is used

Pinkie
  • 10,126
  • 22
  • 78
  • 124

1 Answers1

5

First head over to http://pkg.io/ and get your base extension file.

You'll probably want to use the 'safecracker_submit_entry_start' hook to throw an error if unclean word is entered. The most important part of the extension is registering the method and hook you want to use, otherwise none of the code will run.

Your code should look something like this:

public function activate_extension()
{
    // Setup custom settings in this array.
    $this->settings = array();

    $data = array(
        'class'     => __CLASS__,
        'method'    => 'clean', // point to the method that should run
        'hook'      => 'safecracker_submit_entry_end', // point to the hook you want to use to trigger the above method.
        'settings'  => serialize($this->settings),
        'version'   => $this->version,
        'enabled'   => 'y'
    );

    $this->EE->db->insert('extensions', $data);         

}

Once the method has been called you can start your cleaning. Make sure you pass the safecracker object to your clean method when defining it. For example:

public function clean($sc){
    print_r($sc);
}
Philip Zaengle
  • 947
  • 4
  • 10
  • Thank @philip. This is really great help. Thanks for the link. My only question is how do i tell the clean function to clean $_POST['title'] since i want to clean the title of the submitted entry. Inside the clean function should i have something like $_POST['title'] = clean($_POST['title']);. I want the title to be cleaned before it is sent to the database. – Pinkie Mar 21 '12 at 02:39
  • You could do that right inside of the method being called by the hook. Do you want it to actually clean it? or reject titles with certain keywords? – Philip Zaengle Mar 21 '12 at 16:57
  • You may also be looking for: $this->EE->output->show_user_error("FooBar") – Philip Zaengle Mar 21 '12 at 17:00
  • I have the extension setup. I'm using `'method' => 'clean', 'hook' => 'safecracker_submit_entry_end'` My clean function looks like `public function clean(){$_POST['title'] = $this->sanitize($_POST['title']);}` where sanitize is another function that handles cleaning the title. However this is not working. You said above make sure you pass the object to the clean method. I'm a little confused what you mean by this. What am i doing wrong and how do i fix the clean function so it works. – Pinkie Mar 21 '12 at 19:47
  • title is not being cleaned. Ignoring the sanitize function all together, I also tried `$_POST['title'] = 'test'` so that means whatever title i type in the form, upon submit the title should be replaced with the word `test` but that is not happening, the entry is saved with the original title. – Pinkie Mar 21 '12 at 20:08
  • You can see the full code at http://expressionengine.com/forums/viewthread/213643/ – Pinkie Mar 21 '12 at 20:49
  • That's because your working with $_post data and not the safecracker objects data. – Philip Zaengle Mar 21 '12 at 22:08
  • 1
    How do i change the $_post using safecracker object data. That's what i'm stuck at. – Pinkie Mar 21 '12 at 23:28
  • Why don't i see anything when i do print_r($sc); as per your example above. When safecracker is submitted, it returns to another page but i do not see any print_r info. – Pinkie Mar 24 '12 at 01:40
  • I am in the same problem as Pinkie. There is no documentation on how to use the Safecracker object to manipulate post data. – developarvin Jan 02 '13 at 08:06
  • +1 for mentioning pkg.io. I've been missing out for a long time. – Matt Stein Aug 30 '13 at 22:45