Adding new item attribute like Title or Summary

zombor

Joined: 2003-08-06
Posts: 56
Posted: Thu, 2008-05-08 22:42

I'm trying to add a new field to the main Item object, called "Photographer."

I tried doing this with custom fields, but they did not meet my requirements:

1. Every photo needs this field.
2. It needs to be required when a user uploads a new photo.
3. It needs to be displayed prominently on every photo.

I attempted the following steps:

1. Modified modules/core/templates/ItemEditItem.tpl and added my new field as an input box
2. Modified modules/core/classes/GalleryItem.class and added var $photographer along with the getter and setter methods, and added them to the appropriate places in the class
3. Added a 'g_photographer' field in the g2_item table

These three steps did not work. When I edit a photo, I get the new field, but when I enter a value, it does not save it.

What am I missing?

Login or register to post comments
alecmyers

Joined: 2006-08-01
Posts: 613
Posted: Thu, 2008-05-08 22:56

You will also need to modify ItemEditItem.inc to copy the data from the form to the entity in HandleRequest(...) - see lines 55, and 241 to 245 - and also to move the data the other way in LoadTemplate(...) - see lines 297-302.

Don't forget to keep a copy of your mods, as they will be wiped out (not the .tpl changes but the .inc files) when you upgrade, and cross your fingers that none of these changes will conflict with any updates in the future!

Login or register to post comments
zombor

Joined: 2003-08-06
Posts: 56
Posted: Thu, 2008-05-08 23:20

Thanks!

I added $item->setPhotographer($form['photographer']); and 'photographer' in the foreach in the .inc file, but still no success. =/

I did a die($photographer) in the setPhotographer() method, and it came out with the text I typed in...

Anything else I missed?

Login or register to post comments
alecmyers

Joined: 2006-08-01
Posts: 613
Posted: Thu, 2008-05-08 23:28

yes, you have to recover the information from the database to put it into the form, otherwise you'll store it in the db but never see it.

I wrote:
and also to move the data the other way in LoadTemplate(...) - see lines 297-302.

If you want to debug the contents of a variable use this:

echo '<pre>';print_r($form['photographer']);echo'</pre>';

Login or register to post comments
zombor

Joined: 2003-08-06
Posts: 56
Posted: Thu, 2008-05-08 23:33

Yes, I looked in the database to see if it was in the DB record, but the g_photographer field is blank. I did modify 298-304 to add in the getPhotographer() method.

Login or register to post comments
alecmyers

Joined: 2006-08-01
Posts: 613
Posted: Thu, 2008-05-08 23:56

OK, well there's a well defined procedure for adding a field to an entity, which basically involves adding not just the get.. and set.. methods but also the new DB schema entries in the class file. Also a schema update file in XML format, all of which need to be compiled with the relevant makefiles to regenerate the classes/Entities.inc, classes/GalleryStorage/schema.tpl and some other miscellaneous stuff. Then you'd need to increment the module's version number and include the upgrade code to run your schema upgrade - from the plugin page. You've bypassed (or attempted to bypass) this mechanism, and some of what you've not done is the stuff to do with actually saving and reloading the entity - code that's generated automatically (possibly in Entities.inc?) I suspect. There are other db tables involved with updating schemas too, so I think you're probably not going to get much further with that, sorry...

I think my approach would have been to write a new module that has its own db map (needs just two columns: ImageId and photographer), which you can link into the ItemEditItem.tpl and ItemEditItem.inc files similarly to how you're currently attempting to add to the Entity itself. Editing the code is clearly no problem for you, so learning the extra to write a short module which has its own map won't be difficult.

Alternatively you could write your own Entity based on the PhotoItem (CreditedPhotoItem, say) with the extra field.

The advantage with both of those approaches is that they (mostly) survive upgrades and are less likely to leave your db in a distressed state.

Login or register to post comments
zombor

Joined: 2003-08-06
Posts: 56
Posted: Fri, 2008-05-09 13:43

Great! I got it working from your first paragraph. Didn't know schema updates required a new gallery version bump and upgrade. I'm not *too* worried about new released versions breaking my changes, since I've done so many custom changes to this to the innards of this gallery site so far, an in-place upgrade would probably fail anyway, and I'm content with that.

Login or register to post comments