More Fields for Collections

I don't know if I'm going about this the correct way, but I am trying to change the code in our Omeka installation to allow for additional fields for our Collections. In particular I am trying to add Extent, Rights, and Creator fields in addition to the Name and Description. I was able to change the form.php and show.php files on the admin side for collections and I now have those text boxes for entering data. But when I click Save Changes from the edit screen no data is saved. I also added the following to the CollectionFunctions.php helper:

case 'extent':
$text = $collection->extent;
break;
case 'terms':
$text = $collection->terms;
break;
case 'creator':
$text = $collection->creator;
break;

Not sure what to do next or if I am missing a major step to get these fields into the database.

A better approach might be to write a basic plugin that uses the various hooks available to append to the Collection form (e.g. admin_append_to_collections_form. Full list here). The hooks would let you add to the forms and display without altering Omeka's core code, making maintenance much easier. The drawback is that you would not be able to change the order of the fields displayed, but overall that's usually preferable to changing the core because any updates to Omeka would wipe out those changes.

Then, your plugin might use the after_save_form_collection hook to save the data to the database.

Hope that helps

I'm trying my hand at writing a plugin as you suggested (I haven't written one before and have limited coding experience). I think I'm getting confused on how exactly I'm supposed to use these fields in the plugin. My plugin.php file crashes Omeka after I attempt to install it, am I on the right track with this or way off?:

<?php

// Plugin hooks.
add_plugin_hook('install', 'CollectionFieldsPlugin::install');
add_plugin_hook('uninstall', 'CollectionFieldsPlugin::uninstall');
add_plugin_hook('after_save_form_collection', 'CollectionFieldsPlugin::afterSaveFormCollection');
add_plugin_hook('admin_append_to_collections_form', 'CollectionFieldsPlugin::adminAppendToCollectionsForm');

class CollectionFieldsPlugin
{
public function admin_append_to_collections_form()
{

// Add the additional collection fields to the admin collections form.

<div class="field">
<?php echo $this->formLabel('description', __('General note(s)')); ?>

<?php echo form_error('notes'); ?>
<div class="inputs">
<?php echo textarea(array('name'=>'notes', 'class'=>'textinput', 'id'=>'notes','rows'=>'5','cols'=>'60'),$collection->notes); ?>
</div>
</div>

<?php fire_plugin_hook('after_save_form_collection', $collection); ?>

}

}

It depends a little on whether you are working with the newest version, 1.5, or a previous version. Omeka 1.5 introduces an Omeka_Plugin_Abstract class that is meant to make a lot of this easier in an Object-Oriented mode. If you are working in an earlier release, the more functional approach is the way to go. What you have here looks like kind of a mix of the two. Admittedly, we have some plugins that still also have that mix. That reflects the developing idea.

So, here's a beginning of how I'd tackle what it looks like you are aiming for, if you are working in Omeka 1.5:

class CollectionFieldsPlugin extend Omeka_Plugin_Abstract
{
    protected $_hooks = array('admin_append_to_collections_form', 'after_save_collection');

    public function hookAdminAppendToCollectionsForm()
    {
         $html = "";
         //build up your html form
         return $html;
    }

    public function hookAfterSaveCollection($collection)
    {
         //save your notes data to your database table
    }

}

$cfp = new CollectionFieldsPlugin();
$cfp->setUp();

The pattern of the hooks is to have that protected array, which lists the hooks you want to use, named just as they are on our list of hooks. The names of the public functions in the class convert those underscored names into CamelCase names, preceded by 'hook'.

Jim Safley's ItemOrder plugin might be a good example of how the pieces fit together. Take a look at both plugin.php and ItemOrderPlugin.php

Two additional quick details:

you don't need the code that says "fire_plugin_hook" That's part of Omeka's code that will run your hook.

The two underscores function -- __('General note(s)') -- is part of our internationalization work in the core. For plugins, we're not quite ready to work on that, so probably best to just give the text string.

Hope that helps!

Thanks! This is definitely helping me go in the right direction, but my changes are still hanging the system after installation. I am using Omeka 1.5. Below are the changes I made from what you explained:

For plugin.php:

<?php
require_once 'CollectionFieldsPlugin.php';
$cfp = new CollectionFieldsPlugin;
$cfp->setUp();

For collectionfieldsplugin.php:

<?php

class CollectionFieldsPlugin extend Omeka_Plugin_Abstract
{
protected $_hooks = array('admin_append_to_collections_form', 'after_save_collection');

public function hookAdminAppendToCollectionsForm()
{
<fieldset id="editcollection">
<div class="field">
<?php echo $this->formLabel('notes', ('General note(s)')); ?>
<?php echo form_error('notes'); ?>
<div class="inputs">
<?php echo textarea(array('name'=>'notes', 'class'=>'textinput', 'id'=>'notes','rows'=>'5','cols'=>'60'),$collection->notes); ?>
</div>
</div>
</fieldset>

}

public function hookAfterSaveCollection($collection)
{
//save your notes data to your database table
}

}

The code in Append to Collections Form I am taking directly from form.php in the admin collections folder (taking out the two underscores and changing it to apply to my "notes" field). In your example this was where you had code for html but I wasn't sure if I needed those or could just input the php directly? I tried it first with no database changes and secondly after manually adding a notes column to my omeka database, but still no luck. Is the issue the code I have Append to Collections Form?

It look like the only thing is adding some closing and opening PHP tags. It can be easy to miss in this kind of mix of object oriented code and display code. When you go into the method itself, you need to close the php, and then start it again at the end:

public function hookAdminAppendToCollectionsForm()
{
?>

//your code here

<?php
}

Hmm, okay. I tried adding those in but no results so far (just goes to a white screen after installation). I looked at the plugin for CollectionTree and tried some of the code from the examples there and that didn't seem to work either. Is my "your code here" section a blend of old and new code mixing that is confusing the system maybe?

File is now:

<?php

class CollectionFieldsPlugin extend Omeka_Plugin_Abstract
{

protected $_hooks = array('admin_append_to_collections_form', 'after_save_collection');

public function hookAdminAppendToCollectionsForm()
{
?>
<div class="field">
<?php echo $this->formLabel('notes',('General notes')); ?>
<div class="inputs">
<?php echo textarea(array('name'=>'notes', 'class'=>'textinput', 'id'=>'notes','rows'=>'5','cols'=>'60'),$collection->notes); ?>
</div>
</div>
<?php
}

public function hookAfterSaveCollection($collection)
{
//save data to your database table
}

}

Do I need any of the following? (from Collection Tree examples):
* require_once 'Omeka/Plugin/Abstract.php';
* public function hookAfterSaveFormCollection (instead of hookAfterSaveCollection)
* $collection in parentheses after hookAdminAppendToCollectionsForm
* __v()->formLabel ('collection_notes','General notes') (instead of as formatted above)

Hi again, just checking if anyone has any solutions in trying to get this plugin working? I think I have the correct tags above but it still hangs the system. Any thoughts?

Things I noticed:

  • extend should be extends in the class line
  • The parens around 'General Notes' aren't needed, but shouldn't harm anything.

Also, yes, you probably want to have $collection as an argument to your hookAdminAppendToCollectionsForm method.

Our documentation for that hook previously didn't show that it takes an argument, but it does.