Page 1 of 2

How do I access Custom Field data on the PHP side of things?

Posted: Thu Nov 15, 2007 3:40 pm
by dakotaroar
If I am using Custom Fields, and I want to call a variable in a template, I can use {ep_CustomField1].

What is the corresponding function in PHP? Suppose my CustomField1 = 5 in the serendipity_entryproperties table. I've tried
serendipity_fetchEntryProperties($entry['id']), but I am not sure how this array is structured, since
$variable1 = $entry_properties['ep_CustomField1'];
does not work.

Any help would be appreciated.

Re: How do I access Custom Field data on the PHP side of thi

Posted: Thu Nov 15, 2007 5:24 pm
by garvinhicking
Hi!

Where do you want to put that PHP code?

Try a print_r($eventData) if using inside a hook.

Regards,
Garvin

Posted: Thu Nov 15, 2007 6:07 pm
by dakotaroar
I'm using it to set options for the SMF script I am writing, such as user ID. The user ID would be given by the person making the entry, and then the SMF post would be made by the user coinciding with that user ID.

It's a matter of getting the user ID in the custom field out into a variable so I can dump it into the script for SMF to read it.

Posted: Fri Nov 16, 2007 9:59 am
by garvinhicking
Hi!

Yeah, then a print_r() on the $eventData in your plugin hook should reveal you all variables you need.

Regards,
Garvin

Posted: Fri Nov 16, 2007 4:04 pm
by dakotaroar
I have no idea how to use hooks. I just want to hard code this for now to see if it will work, and then it may take someone else to make it into a real plugin.

Can't I just use serendipity_fetchEntryProperties($id)? That seems to be the most appropriate way to do this.

Posted: Mon Nov 19, 2007 9:28 am
by garvinhicking
Hi!

Please show us the code you have so far, then I might be able to understand what you're trying to do.
Can't I just use serendipity_fetchEntryProperties($id)? That seems to be the most appropriate way to do this.
Sure, you can! You just need the id from the $eventData array in your plugin event hook.

Regards,
Garvin

Posted: Mon Nov 19, 2007 11:10 pm
by dakotaroar
I don't know how this will help you any more, but maybe you can tell me how this entry_properties array should be called?

I already have $entry['id'] at this point. All I want to know is how to get into what I have called $entry_properties. It's probably something really stupid.

Code: Select all

/* ADDED CODE - PART II
*
* This actually posts the post to the SMF forum.
*We had to wait until now so we could find the PermaLink
*
*/
$entry_properties = serendipity_fetchEntryProperties($entry['id']);


$ID_MEMBER = $entry_properties['ep_CustomField1'];        //This is where the CustomField1 value should go.
//$ID_MEMBER = 6;

$perma_link = serendipity_archiveURL($entry['id'], $entry['title']);

$msgBody = $msgBodytotal . '<br /><br />[url=' . $perma_link . ']Full article . . .[/url]';

$msgBody = prepBody($msgBody);

Posted: Tue Nov 20, 2007 12:39 pm
by garvinhicking
Hi!

Where did you insert this code? I'm not understanding which file you'Re editing.

Your initial posting was talking of your own plugin file, but the code you posted cannot work on its own.

Regards,
Garvin

Posted: Tue Nov 20, 2007 3:22 pm
by dakotaroar
I'm not talking about my own plugin. All the other coding sections of this board have big labels that say "DON'T POST IF YOU AREN'T A DEVELOPER!!!!!", so I posted it here.

I don't have time to make a plugin. Maybe someone else will later, but all I am trying to do is make it work.

This code is from functions_entries.inc.php. No, it's not the entire section.

Posted: Tue Nov 20, 2007 4:12 pm
by garvinhicking
Hi!

One should NEVER need to modify core s9y files to do anything. We are fully operational through event plugins, so this is where external data calls should be done. But I can't tell you where to call what, unless you tell me exactly what you need to get working.

So if you use the PHP code at the right place, where $entries['properties'] is set (like in the PHP plugin hook "frontend_display"), you have access to the custom property fields. Also in any template file you have access. You are NOT ALLOWED to put HTML code into the PHP things, that's what the smarty template files are for.

Please take some time write a full posting that indicates what exactly you want to do how and where. You're mixing up things and postings and plans and ideas, so I (and supposedly no one else here) no longer understand what you want to do at all.

Regards,
Garvin

Posted: Sat Nov 24, 2007 9:31 pm
by dakotaroar
All I want to know is the structure of the array that serendipity_fetchEntryProperties($id) returns.

This is probably some misunderstanding of PHP on my part. How do I get a value back out of this array if I know that the property I am looking for is, for example, "CustomField1"?

Posted: Sun Nov 25, 2007 2:05 am
by garvinhicking
Hi!

Simply use:

Code: Select all

$props = serendipity_fetchEntryProperties(1);
print_r($props);
and you'll se the result. print_r is always your friend. :)
This is probably some misunderstanding of PHP on my part. How do I get a value back out of this array if I know that the property I am looking for is, for example, "CustomField1"?
$props['ep_CustomField1'] should be it.

Regards,
Garvin

Posted: Mon Nov 26, 2007 5:08 pm
by dakotaroar
Thank you! That is exactly what I was looking for.

Unfortunately I have run into another problem. When are the extended properties actually put into the database? I am trying to add a section of code that will run right after the entry is posted, but there seem to be no values that are accessible. I can access values from previous entries (e.g., entry['id']-1), but not from the one that was just posted. This leads me to believe that I am out of order, which is probably why I've had such a hard time with this (because I was trying to use an empty variable).

For example, if I was to use the following code from functions_entries_inc.php (in function serendipity_updertEntry()),

Code: Select all

 
       $res = serendipity_db_insert('entries', $entry);

        if ($res) {
            $entry['id'] = $serendipity['lastSavedEntry'] = serendipity_db_insert_id('entries', 'id');
            if (is_array($categories)) {
                foreach ($categories as $cat) {
                    if (is_numeric($cat)) {
                        serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}entrycat (entryid, categoryid) VALUES ({$entry['id']}, {$cat})");
                    }
                }
            }

			
	serendipity_insertPermalink($entry);
	
//start my added code
$id = $entry['id'];
$props = serendipity_fetchEntryProperties($id);
print_r($props); 

$ID_MEMBER = $props['ep_SMF_ID-number'];
$props['ep_SMF_ID-number'] would come out empty.

Do database queries run by plugins run at a certain time relative to other functions? I wish to use these additional post properties to determine how the post to the SMF forum will be formatted, or if the post will be sent at all.


Thanks for your help so far, you've been very patient.

Posted: Mon Nov 26, 2007 9:33 pm
by garvinhicking
Hi!

The properties are stored in the backend_publish / backend_save hooks. You can access the properties at that point by looking into $serendipity['POST'].

If you really need it from the DB,you can use the hook "'backend_trackbacks'".
Do database queries run by plugins run at a certain time relative to other functions?
Yes, they do-plugin API events are called in a linear way.

HTH,
GArvin

Posted: Tue Nov 27, 2007 3:08 am
by dakotaroar
Wow, print_r really is my friend!

Thanks for your patience, I've finally figured it out. It took a while of dealing with the arrays, but everything works now. Using POST was a good idea, it even saves having to do the database query.

Thanks again!