Problem
My plugin creates new Omeka records, and I want to make theming functions that follow Omeka's usual way of looping through them for display.
Solution 1
The usual Omeka way of looping through records to display looks roughly like this, which would display info about items in Omeka
<?php while(loop_items()): ?> <h2><?php echo item('Dublin Core', 'Title'); ?></h2> <!-- other code to display more metadata --> <?php endwhile; ?>
The heart of this is building what you need to use Omeka's loop_records function, which loop_items() calls. It takes four arguments:
- $recordType - The name of the record type, usually the class name, but can by string used to distinguish between loops. This lets multiple loops over different sets of records work without colliding.
- $records - An array of records to loop through
- $setCurrentRecordCallback - The name of a callback function as a string that sets the current record while looping
The usual functions to create are the following, where you replace 'my_record' with the name of the record type you will loop through:
- set_my_record_current_record($record) - Sets the current record in the loop
- loop_my_records() - Do the loop.
- get_my_records_for_loop() - Gets the set of records to loop over
- get_my_record_current_record() - Return the current record in the loop
- set_my_records_for_loop($records) - Sets the records to loop over
The actual functions would look like:
/** * This sets the current record as a property of the view being displayed * It is the callback that goes in loop_records (see below) * You can name the property whatever you want, but name it to avoid clashes * with other plugins */ function set_my_record_current_record($record) { __v()->myRecordCurrent = $record } function loop_my_records() { return loop_records('my_records', get_my_records_for_loop(), 'set_my_record_current_record'; } /** * Usually, a controller will have already set the records on the view * but not always. If not, do your different logic and use set_my_records_for_loop */ function get_my_records_for_loop() { return __v()->myRecords; } /** * Sets the records so the are available to get_my_records_for_loop() when * called in loop_my_records (see above) * Often, this is not needed because your controller will have set this property, * but some situations might call for a different way of setting the records */ function set_my_records_for_loop($records) { __v()->myRecords = $records; }