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

3 Responses to “Multiple Safecracker Instances in One Expression Engine Template”

  1. Crossfade says:

    This is the exact problem that has had me frustrated for near two hours now, and surprisingly, this is the only post that I have found that addresses it directly.

    (So thanks!)

    However, it appears that the current file structure and safecracker_lib.php file are now different, so line 758 is no longer the correct place for the fix.

    Would love an update to this post if possible ;-)

  2. Hi Crossfade,

    What version of EE are you using?

    I will be able to direct you better when I know the version…

    Thanks,

    Shann

  3. Crossfade says:

    Thanks for the reply :-)

    Expression Engine Version 2.2.2 Build 20110801

    Official word from Expression Engine is that multiple instances of safecracker on the same page is unsupported, but I don’t know whether that’s an “unsupported – we don’t advise doing so” or “unsupported – we’ve programmed it into things purposely to disallow it”

    It would certainly help the project I am working on at the moment if I could get it to work.

    Thanks again for the reply

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