Mainly as a learning exercise, I wrote a simple extension to add a JavaScript onsubmit event to an SAEF. Works fine, except when I enable the “Stand Alone Entry Form Enctype” extension, which uses the same hook and I also need for the same form.
While I can easily add my code to the Enctype extension, how do I go about making the two extensions “play nice”? The last one listed in the admin is used, no matter what the priorities are. I’ve tried adding $EXT->last_call, but I don’t really know how to use it. Plus, its value is always FALSE, no matter where I call it from. Here’s the main method:
function addSubmitEvent($options)
{
global $EXT, $TMPL;
if ($TMPL->fetch_param('onsubmit')) {
$options['onsubmit'] = $TMPL->fetch_param('onsubmit');
}
return $options;
}
How do I add/use $EXT->last_call?
Basic usage, let’s say that the hook passed a variable to your function named $str:
global $EXT;
if ($EXT->last_call !== FALSE)
{
$str = $EXT->last_call;
}
Which hook are you using? If it’s a hook that doesn’t have a return value, then $EXT->last_call will have nothing to work with.
I’m using “form_declaration_modify_data” which is supposed to return the data, but I always get FALSE.
I’ve modified the extension so it has the ability to add onsubmit, onreset and enctype, so I don’t have to worry about collisions, but I still would like to know how to use last_call correctly. Thanks.
Is the other extension returning data properly? And if your extension has a greater priority, then yours will be running first, and you’d either need to alter the priorities so yours runs afterward, or modify the other extension to work with $EXT->last_call, which would have your extension’s return data.
OK, here’s the enctype extension found on the forum with comments deleted for length:
<?php
class saef_enctype
{
var $settings = array();
var $end_script = FALSE;
var $ext_class = "saef_enctype";
var $name = "Stand Alone Entry Form Enctype";
var $version = "1.0.0";
var $description = "Allows you to add enctype=\"multipart/form-data\" to forms.";
var $settings_exist = "n";
var $docs_url = "";
function saef_enctype ($settings = '')
{
$this->settings = $settings;
}
function activate_extension ()
{
global $DB;
// -- Add rewrite_header
$DB->query(
$DB->insert_string(
'exp_extensions', array('extension_id' => '',
'class' => $this->ext_class,
'method' => 'modform',
'hook' => 'form_declaration_modify_data',
'settings' => '',
'priority' => 1,
'version' => $this->version,
'enabled' => 'y'
)
)
);
}
function update_extension ($current = '')
{
global $DB;
if ($current == '' OR $current == $this->version)
{
return FALSE;
}
if ($current < '1.0.1' )
{
/*Update Query*/
}
$DB->query("UPDATE exp_extensions SET version = '".$DB->escape_str($this->version)."' WHERE class = '".$this->ext_class."'");
}
function modform ($options)
{
//if (isset($options['hidden_fields']['URI']) && $options['hidden_fields']['URI']=='/post/images/')
if (isset($options['hidden_fields']['URI']) && $options['hidden_fields']['weblog_id']=='3') {
$options['enctype']='multi';
}
return $options;
}
}
?>
And here’s mine:
<?php
if (!defined('EXT')) {
exit('Invalid file request');
}
class RH_onsubmit {
var $settings = array();
var $ext_class = 'RH_onsubmit';
var $name = 'Add Onsubmit Event';
var $version = '1.0.0';
var $description = 'Adds a JavaScript onsubmit event to forms';
var $settings_exist = 'n';
var $docs_url = '';
function RH_onsubmit($settings = '') {
$this->settings = $settings;
}
function activate_extension() {
global $DB;
$DB->query($DB->insert_string('exp_extensions',
array(
'extension_id' => '',
'class' => "RH_onsubmit",
'method' => "addSubmitEvent",
'hook' => "form_declaration_modify_data",
'settings' => "",
'priority' => 5,
'version' => $this->version,
'enabled' => "y"
)
)
);
}
function update_extension($current='1.0.0') {
global $DB;
if ($current == '' | $current == $this->version) {
return FALSE;
}
if ($current < '1.0.1') {
// update to next version 1.0.1
}
$DB->query("UPDATE exp_extensions
SET version = '".$DB->escape_str($this->version)."'
WHERE class = '".$this->ext_class."'");
}
function disable_extension() {
global $DB;
$DB->query("DELETE FROM exp_extensions WHERE class = '" . $this->ext_class . "'");
}
function addSubmitEvent($options) {
global $TMPL;
if ($TMPL->fetch_param('onsubmit')) {
$options['onsubmit'] = $TMPL->fetch_param('onsubmit');
}
return $options;
}
}
?>
Where are you referencing $EXT->last_call as shown here? Both extensions should really be doing so to be friendly neighbors, but with the current priorities, yours needs it for sure.
function addSubmitEvent($options)
{
global $EXT, $TMPL;
if ($EXT->last_call !== FALSE)
{
$options = $EXT->last_call;
}
if ($TMPL->fetch_param('onsubmit'))
{
$options['onsubmit'] = $TMPL->fetch_param('onsubmit');
}
return $options;
}
Yes, $options, corrected my code sample. Adding this to your extension in the addSubmitEvent() method gives the options array as returned by the SAEF enctype extension.
global $EXT;
echo '<pre>';print_r($EXT->last_call);echo'</pre>
<p>’;
Try the modification again, and if it still does not work, try it with the debugging code above, and if you are getting a (bool) FALSE, zip both extensions with your modifications, and attach them to this thread.
Zip attached (contains both). As expected, I got nothing for a result. (I’d used var_dump earlier).
Also, would this (http://ellislab.com/forums/viewthread/80012/) require an extension? I can’t seem to find a hook that I can use.
As expected, with your attached files, the SAEF Enctype extension outputs a (bool) false (changing the print_r() to a var_dump()), and your rh_onsubmit extension outputs the options array. You sure both extensions are installed and enabled? If you use var_dump() are you getting output from both methods to verify that the method is being hit?
With regards to what extension hook to use for your goal, any extension hook that is hit on a request can take and operate on submitted data, sending it to an external process. The session hooks are guaranteed to be called on every page request, so that might be a better hook for arbitrary processing of additional form data.
I put “$options=$EXT->last_call” in both of the extensions. They both now work. I must have been adding the code to the one that was loading first, so I never got anything but FALSE. Confusion (mostly) over.
As to the SAEF question, if I understand you correctly, I should add a custom index (video_filename) to the session variable and simply read it (and then delete it) further on down the line? Never thought of that.
But don’t I need to post the form to itself to get the post data in order to put the user data into a session variable? Then I can’t do a redirect. That’s why I was trying to use a hidden input and writing the value with js onsubmit. But it doesn’t carry that input’s value to the EE redirect set with SAEF.
I can’t see how I would get the file name otherwise, unless I write to a temporary db table on upload. Which seems like way more trouble than I should have to go to.
Confusion back.
I’m not saying to use the Session cache, but that since a Session hook will execute on every page request, when the field you are looking for is present in the POST data, to do whatever you need to do with it from there, whether that’s starting some service on the server or redirecting to a different landing page before redirecting to the SAEF’s final redirect location. Or really whatever you’d like to do. At that point, anything you can do with PHP or other technologies on your server is available to you.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.