Discuss and get help with staggernation.com's plugins for Movable Type.
You are not logged in.
Pages: 1
A couple of users who are using custom Perl scripts and the Movable Type API to create and modify entries from outside MT have asked if it's possible to manipulate the data in RightFields extra fields from their scripts. It is, and here's how. Thanks to Chad Everett for helping me test this code.
1) Add these two lines of code at the top of your script:
use lib 'plugins/RightFields/lib'; use RightFields qw( load_plugindata config_key new_obj );
This assumes your script is located at the top level of your MT directory, where mt.cgi is; if your script is elsewhere, adjust the relative path in the first line above, or use an absolute path (starting with a slash) based on MT's location on your server.
2) Somewhere in your script, add this subroutine:
sub extra_obj {
my ($blog_id, $e_id) = @_;
my $cfg_key = config_key($blog_id, 'extra');
my $cfg = load_plugindata($cfg_key) || {};
return new_obj($cfg, $e_id);
}3) Assuming you already have a $blog_id and an $entry_id (if your script is creating an entry, you will need to save it first in order to generate its ID), you can load and set the extra fields for that entry like this:
my $e_ext = extra_obj($blog_id, $entry_id);
$e_ext->id($entry_id); # if it doesn't exist yet; if using PluginData, call $e_ext->entry_id instead
$e_ext->field_one($field_one_value);
$e_ext->field_two('foo');
$e_ext->save || return $app->error($e_ext->errstr);(This assumes $app is your script's MT::App object.)
As indicated in the comment, the code varies slightly depending on what data storage method you're using for the extra fields. If you're using PluginData storage, change the second line above to:
$e_ext->entry_id($entry_id);
When using SQL data storage, the fieldnames (field_one, field_two) should be the column names without the datasource_ prefix--the same way they're displayed in the RightFields interface.
4) To access a value in an existing entry after loading the object, simply use:
my $field_one_value = $e_ext->field_one;
5) If you delete an entry and need to delete the corresponding extra fields data, use this:
my $e_ext = extra_obj($blog_id, $entry_id); $e_ext->remove;
In the next version of RightFields I hope to provide a slightly cleaner API for doing things like this, but the code above should do the job for now. Please post to this thread if you have any questions about this code or encounter any problems using it.
Offline
Hey Kevin -
This general function has been working well for me for some time, but with MT 4.x (specifically 4.1, but I think it actually broke in 4.0), I have had a devil of a time reading data from a SQL datasource using this technique. For instance, I use:
my $e_ext = extra_obj($blog_id, $entry_id);
And then try to read something out of it, like so:
my $field_one = $e_ext->field_one;
It doesn't work. I get an error about an undeclared array being used in BaseObject.pm. Line 268, if you're curious, which reads:
@terms{@$pk} = @$id;I'm assuming that $pk goes back to line 254, which is:
my $pk = $obj->primary_key_tuple;
But I'm not sure why it doesn't work. It works fine on a pseudo-record (something stored in PluginData). Any idea how to get it out of a SQL datasource?
Thanks!
Offline
Pages: 1