Hiya,
I’m trying my level best to create an extension so that I can perform some actions on the ExpressionEngine database once an entry has been submitted. I’m not too sure what the exact difference between the submit_new_entry_end and submit_new_entry_absolute_end is so I tried creating an extension that uses submit_new_entry_end. I’m not fussed if I have to use the other hook of course if that will get what I want to do working though.
I tried this code below which is a sort of edit from an extension I found somewhere and I’ve pretty much stripped it down as much as I can see to just test the extension out.
Code
<?php
/**
* Example Extension
*
* Perform extra work after entry is submitted
*
* This file must be placed in the
* /system/admin/extensions/ folder inside your ExpressionEngine installation on your server.
*
* @version 1.0
* @see http://www.markbowendesign.com
* @copyright Copyright (c) 2009-2010 Mark Bowen
* @license {@link http://creativecommons.org/licenses/by-nc-nd/3.0/ Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported}
*
*
* @author Mark Bowen <http://www.markbowendesign.com>
* @package Example Extension
*/
/**
Changelog
V1.0 Initial extension creation
*/
class Example_extension {
var $settings = array();
var $name = 'Example Extension';
var $version = '1.0';
var $description = 'Perform extra work after an entry is submitted';
var $settings_exist = 'y';
var $docs_url = '';
// -------------------------------------------------
// Constructor - Extensions use this for settings
// -------------------------------------------------
/*
*
*
* @param unknown $settings (optional)
*/
/**
*
*
* @param unknown $settings (optional)
*/
function Example_extension($settings='') {
$this->settings = $settings;
}
/**
* Configuration for the extension settings page
*
* @return array The settings array
*/
function settings() {
$settings = array();
return $settings;
}
// -------------------------------------------------
// Function - MAIN FUNCTION
// -------------------------------------------------
/**
*
*/
function example_extension_function($data, $entry_id)
{
global $DB, $FNS, $SESS, $EXT, $OUT, $LANG, $IN;
$printThis = $data['entry_id'];
echo $printThis;
}
// END - Let's celebrate :wink:
/**
*
*
* @return unknown
*/
function activate_extension() {
global $DB;
// Default Setting Values
// $enter_information_here = 'Enter information here...';
// Default Settings
$default_settings = serialize(
array(
)
);
// Hooks array (Thanks to Leevi Graham for the idea)
$hooks = array(
'submit_new_entry_end' => 'example_extension_function',
);
foreach ($hooks as $hook => $method) {
$sql[] = $DB->insert_string( 'exp_extensions',
array(
'extension_id' => '',
'class' => get_class($this),
'method' => $method,
'hook' => $hook,
'settings' => $default_settings,
'priority' => 10,
'version' => $this->version,
'enabled' => 'y'
)
);
}
// Run all our sql queries
foreach ($sql as $query) {
$DB->query($query);
}
return TRUE;
}
/**
*
*/
function disable_extension() {
global $DB;
$DB->query("DELETE FROM exp_extensions WHERE class = 'Example_extension'");
}
}
// END CLASS
/* End of file pi.example_extension.php */
/* Location: system/extensions/ext.example_extension.php */First up when publishing a new entry I’m not even sure that the extension is firing as the entry just gets saved to the database but my echo of the entry_id doesn’t seem to fire.
I’ve tried quite a few different things here such as print_r($data), print_r($_POST) but even when I do get some information sent back to the browser I never ever see the entry_id which is what I’m really after to be able to do what I need further with the entry.
Can anyone see what I’m doing wrong here. Also if there’s any extraneous code in the extension that I don’t need then please do feel free to point that out to me too.
Thanks for any help on this.
Best wishes,
Mark
Anyone care to throw me a bone before Easter? 😉
If I could even just get the most bare bones example of an extension that I can echo out the newly created entries entry_id and then I’ll be able to do what I need but I just can’t for the life of me see where I’m going wrong.
Any help on this would be greatly appreciated.
Best wishes,
Mark
I’ve just tested that code and it does basically work, but you’ve got the parameters in the wrong order on example_extension_function(). If you check the docs for submit_new_entry_end you’ll see the hook is setup like this:
$edata = $EXT->call_extension('submit_new_entry_end', $entry_id, $data, $ping_message);So your function should be changed from:
function example_extension_function($data, $entry_id)To:
function example_extension_function($entry_id, $data, $ping_message)I find it a bit weird that print_r($data) wasn’t working for you, as with the current code that returns the entry ID - exactly what you were after.
Hiya,
Thanks for looking at this for me. Unfortunately I still can’t get this working on my end. Not really too sure what’s causing it though.
I just changed the extension to reflect what you’ve said above and I just placed in :
echo "<pre>";
print_r($data);
echo "</pre>
<p>”;Unfortunately when submitting a new entry I only get this information shown to me though :
Array ( [entry_id] => [weblog_id] => 1 [author_id] => 1 [site_id] => 1 [ip_address] => 127.0.0.1 [title] => whoodle [url_title] => who [entry_date] => 1270730292 [edit_date] => 20100408143914 [versioning_enabled] => y [year] => 2010 [month] => 04 [day] => 08 [expiration_date] => 0 [comment_expiration_date] => 0 [sticky] => n [status] => open [allow_comments] => y [allow_trackbacks] => n [forum_topic_id] => 0 [dst_enabled] => n )
The entry_id is missing / not showing for some reason :-(
Not too sure why that is though?
Best wishes,
Mark
I don’t understand that, I wouldn’t have thought there would be an entry_id entry in the $data array at all, since it’s provided as a variable of its own. What does print_r($entry_id) produce?
If that doesn’t show anything then I’d check you haven’t got any other extensions on that hook, in case they’re not tidying up after themselves.
I don’t understand that, I wouldn’t have thought there would be an entry_id entry in the $data array at all, since it’s provided as a variable of its own. What does print_r($entry_id) produce?
Looks like you beat me to it 😉 Just tried that now and was just about to post to say that I am now getting the entry_id spat out by doing things that way instead. Seems weird that it doesn’t show in the data array to me though or is that just me? I mean why have the entry_id separate like that if you could get hold of it anyway by just doing $data[‘entry_id’]? I mean if it were there that is 😊
Thanks again for the help on this. I now have working what I wanted to get working. The only problem I have now is trying to get my head around a settings page. That has always gotten me beat so far apart from if my settings page is just a very very very simple text input box but for this one I need multiple settings based on current weblog settings…
…agggghh!!
Not too sure even where to start on that part.
Best wishes,
Mark
No problem. I wouldn’t say it’s weird that entry_id isn’t in the data array, given that it’s provided as a separate var, but it is weird that there’s an empty entry for it.
With the settings, if you can get by with the abstracted settings layer then it’s pretty straightforward. When you have to go down the custom route things get fiddly, admittedly.
No problem. I wouldn’t say it’s weird that entry_id isn’t in the data array, given that it’s provided as a separate var, but it is weird that there’s an empty entry for it.
Ah well. I’m guessing they must have done that for some reason so I won’t question it any more. As it’s working now I’m not too bothered I suppose.
With the settings, if you can get by with the abstracted settings layer then it’s pretty straightforward. When you have to go down the custom route things get fiddly, admittedly.
I’ll have to take another look into that and see if it can do what I need to do. I’m thinking not though as what I really need is a settings page which adds a row of options for each weblog in the system. As this will be different in different installs then this needs to be dynamically created and I’m not too sure how to go about doing that side of things.
Best wishes,
Mark
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.