Help - Search - Members - Calendar
Full Version: Accessing $entry Information From Within A Plugin
Movable Type Community Forum > Additional Resources > Plugin Development and Usage
indigirl
Thanks in advance for any help on this topic...

I'm working on a plugin that launches a dialog from the edit/new entry page. It lets a user select a language and upload a file to be associated with that language. This is all working fine via a custom object I created and my extension of the upload_file subroutines.

What I'm stuck on is how to best associate those assets with the entry that's being edited or created. I can add another field to the custom object to contain an entry id that the assets should be associated with. However, I've tried accessing properties about the $entry from within the plugin:

my $entry = shift;
return $entry->id;


I get the following error:
"Can't call method "id" on an undefined value"


I believe that the entry object isn't available to this plugin for some reason. There isn't any difference whether it's a new entry or entry that's already been saved, and therefore has a row in the database.

Any pointers on where to go next? (And yes, I've read through the documentation and code extensively at this point.)

--Amy
staggernation
Can you give a little more context for where in your code you're trying to do this (which subroutine, and what's calling the subroutine)?
indigirl
QUOTE (staggernation @ Jan 8 2009, 07:49 PM) *
Can you give a little more context for where in your code you're trying to do this (which subroutine, and what's calling the subroutine)?


In my plugin, the subroutine called 'upload_lang_file' is called on submission of the plugin form that runs in the dialog.

Here's a walkthrough:
1. User begins creating a new entry or editing an entry.
2. User clicks on "Add Language" in the plugin menu on the right side of the entry screen.
3. "Add Language" dialog window opens with a file upload form.
4. User clicks submit, triggering the upload_lang_file subroutine in my plugin.
5. upload_lang_file() uses the standard MT::CMS::Asset::_upload_file() sub to actually upload the file.
6. after upload, the custom object is created and populated with the asset id, selected language id. This is where I want to access the entry id.
staggernation
So, it sounds like you'll need to pass the entry ID through in a hidden field in your upload form, which would take a couple of steps:

1) When the method that's responsible for displaying the form is called, it should be passed an $app->param('id') containing the id of the entry on which the action was clicked. So to pass that along to the template:

$param->{'id'} = $app->param('id');

(assuming $param is the hash you pass when you build the template)

2) Then, in the form on the upload file template:

<input type="hidden" name="id" value="<mt:var name="id">" />

3) You should now be able to call $app->param('id') in your upload_lang_file() method to get the entry ID.
indigirl
QUOTE (staggernation @ Jan 8 2009, 08:34 PM) *
So, it sounds like you'll need to pass the entry ID through in a hidden field in your upload form...


Brilliant! This is an approach I tried yesterday, but without thinking to pass the current params into the template. THANK YOU!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.