Post-processing entries before sent to database?
Posted: 17 November 2008 05:08 AM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  207
Joined  07-30-2008

I need to post-process entries to a weblog when they are created or modified. Specifically I want to auto-fill certain fields if the user has left them empty, so that they end up non-empty in the database. note that i am using the EE publish/edit form and not a saef.

It seems I need to write an extension that uses the submit_new_entry_end hook? but it’s unclear from the docs whether this hook is just to notify when an entry is created or modified… I need to be able to change the data before it’s sent to the database…

please advise on the best way to approach this, and if there’s an extension already written that would be a good starting point for this task please let me know. thanks!

Profile
 
 
Posted: 17 November 2008 08:59 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23744
Joined  05-20-2002

I’m going to shift to ‘Extensions’- because you’re right, that’s the way to go on this one.  I suspect there are a number of existing extensions you could riff off of.  I’ve got one open- uses the SAEF, but I suspect it’s just a matter of changing the hook.  The gist is:

<?php

/*
=========================================================
This Extension is intended for use with ExpressionEngine.
ExpressionEngine is Copyright (c) 2003 pMachine, Inc.
http://www.pmachine.com/
=========================================================
by da cow
========================================================
*/

if ( ! defined('EXT'))
{
    
exit('Invalid file request');
}

class Saef_modify
{
    
var $settings = array();
    var
$name = 'SAEF Modify';
    var
$version = '1.0';
    var
$description = 'SAEF - modify post data for the cracktastic forms';
    var
$settings_exist = 'n';
    var
$docs_url = '';
    
     var
$url_fields = array(11, 15, 61, 62, 63);

    function
mod_post()
    
{    
        
global $FNS, $IN, $EXT, $OUT, $STAT;
        
        if (
$EXT->last_call !== FALSE)
        
{
            $s
= $EXT->last_call;
        
}

        $this
->variable_check();
        
}


    
function variable_check()
    
{
        
global $FNS, $IN, $EXT, $OUT;
    
        foreach (
$this->url_fields AS $val)
        
{

                
if (isset($_POST['field_id_'.$val]) AND $_POST['field_id_'.$val] != '' AND substr($_POST['field_id_'.$val], 0, 4) != "http" AND ! ereg('://', $_POST['field_id_'.$val]))
                
{
                    $_POST[
'field_id_'.$val] = "http://".$_POST['field_id_'.$val];
                
}
    
        
        }
     }

    
function activate_extension()
    
{
        
global $DB;
        
        
$DB->query($DB->insert_string('exp_extensions',
                array(
                
'extension_id'    => '',
                
'class'            => "Saef_modify",
                
'method'        => "mod_post",
                
'hook'            => "weblog_standalone_insert_entry",
                
'settings'        => "",
                
'priority'        => 1,
                
'version'        => $this->version,
                
'enabled'        => "y"
                
)
            )
        );
    
}
        
    
function disable_extension()
    
{
        
global $DB;
        
        
$DB->query("DELETE FROM exp_extensions WHERE class = 'Saef_modify'");
        
    
}
    
}
?>


I’d use the submit_new_entry_start, instead of the saef hook I was using.  And the above is simplified- sorta.  No reason for variable_check() to exist- just pull it all into the mod_post function and return.  I did a lot of ripping out to simplify things.

That help?

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 18 November 2008 01:21 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
RankRank
Total Posts:  207
Joined  07-30-2008

working great now, starting with your template. thanks!

note that you were missing <?php from the top of the example you gave…

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 66389 Total Logged-in Users: 46
Total Topics: 84714 Total Anonymous Users: 21
Total Replies: 454691 Total Guests: 198
Total Posts: 539405    
Members ( View Memberlist )