Multiple Safecracker Instances in One Expression Engine Template


June 22nd, 2011

Are you getting heavy with User Generated Content (UGC) ? Using Expression Engine 2 and Safecracker? If so, then you could run into a nasty surprise relatively quickly.

Having only once instance of Safecracker in your EE template is just fine and dandy – everyone is happy. What happens when you add more than one instance of Safecracker is a big let down and one of the reasons I feel let down by Ellislabs decision to bundle Safecracker with EE2.

If you place two or more Safecracker forms on a page you may hit PHP errors or a complete inability for the template to be rendered at all (internal server error etc). The reason for this is that Safecracker messes with some of the internals ($this->EE variables) of Expression Engine with a complete ignorance for anything that might come after it in the template.

In essence, the first instance of Safecracker is being outputted fine. It is the second instance that hits the road block. Here is a quick fix for this issue and it will allow you to place as many Safecracker instances as you like in a template.

Edit this file:

/system/core/expressionengine/third_party/safecracker/libraries/safecracker_lib.php

before line 758 (the foreach)

put this:


foreach($this->EE->cp->js_files as $type => $files) {
     if(!is_array($files)) {
        $files  =       explode(',', $files);
    }

    $this->EE->cp->js_files[$type]  =       $files;
}

Any questions – hit the comments.

Tags: , ,

Categories: Expression Engine, Programming

"Thought leadership is how winners are differentiated in business."