Custom Shortcode Error

I’m trying to create my own shortcode, via a plugin, to display a table of some basic metadata.

I was helped by the previous discussion “Simple Pages Shortcode Question” with advice from @jflatnes and @patrickmj

and then I looked closely at both the ShortcodeCarousel and ShortcodeAnyFile plugins to construct my own.

I think I’m missing a step, though, because I’m getting an error when I try to use the shortcode:
“Omeka_View_Exception: A current item variable has not been set to this view. in /home/arkirkland/ardenkirkland.com/omekasandbox/application/views/helpers/GetCurrentRecord.php:27.”

I’ve tried it in a SimplePage with a variety of parameters and nothing seems to work, so I don’t think any specific params are the problem. It seems like the foreach $items as $item isn’t working? But that does work in the Shortcode Carousel plugin, so . . .

I have tried including or not including various parts from the other examples I followed, so I’m hoping I’m just slightly off with this particular combination.

Thanks in advance for any corrections to this code that anyone can suggest!

in ShortcodeStorageTablePlugin.php:

    <?php

    class ShortcodeStorageTablePlugin extends Omeka_Plugin_AbstractPlugin
    {

        public function setUp()
        {
            add_shortcode('storagetable', array('ShortcodeStorageTablePlugin', 'storagetable'));
            parent::setUp();
        }

         /**
         * Build HTML for the display of a table of storage locations and basic metadata
         * @param array $args
         * @param Zend_View $view
         * @return string HTML to display
         */
        public static function storagetable($args, $view)
        {
            static $id_suffix = 0;
           
            if (isset($args['ids'])) {
                $params['range'] = $args['ids'];
            }

            if (isset($args['tags'])) {
                $params['tags'] = $args['tags'];
            }

            if (isset($args['sort'])) {
                $params['sort_field'] = $args['sort'];
            }

            if (isset($args['order'])) {
                $params['sort_dir'] = $args['order'];
            }
            
            if (isset($args['collection'])) {
                $params['collection'] = $args['collection'];
            }

            if (isset($args['num'])) {
                $limit = $args['num'];
            } else {
                $limit = 10;
            }
            
            $items = get_records('Item', $params, $limit);

            $html = $view->partial('storage_table.php', array('items' => $items, 'id_suffix' => $id_suffix));
            $id_suffix++;
            return $html;
        }
    }

in storage_table.php:

    <div>
        <table>
            <tr>
                <td>Storage Location</td>
                <td>Identifier</td>
                <td>Title</td>
                <td>Description</td>
            </tr>
            <?php foreach($items as $item): ?>
                <tr>
                <td>
                    <?php if (metadata('item', array('Item Type Metadata', 'Storage Location')) !='') {
                        echo metadata('item', array('Item Type Metadata', 'Storage Location'));
                        } ?>
                </td>
                <td>
                    <?php if (metadata('item', array('Dublin Core', 'Identifier')) !='') {
                        echo metadata('item', array('Dublin Core', 'Identifier'));
                        } ?>
                </td>
                <td>
                    <?php if (metadata('item', array('Dublin Core', 'Title')) !='') {
                        echo metadata('item', array('Dublin Core', 'Title'));
                        } ?>
                </td>
                <td>
                    <?php if (metadata('item', array('Dublin Core', 'Description')) !='') {
                        echo metadata('item', array('Dublin Core', 'Description'));
                        } ?>
                </td>
                </tr>
            <?php endforeach; ?>
        </table>
    </div>

(also on gitHub)

You have to set the “current” item to use 'item' as the argument in calls to metadata() and similar functions.

Switching out your foreach line with foreach (loop('items') as $item): will set that up for you automatically.

Thanks @jflatnes! I was in and out of this project and didn’t have the time to resolve this right away, but now I am happy to report back that, with your help, I was able to get this working. Following your suggestion, I changed the beginning of my loop to this:

<?php set_loop_records('items', $items);
   foreach (loop ('items') as $item): ?>

And that did the trick!

(I briefly had a conflict with this and the UV plugin, but I did get that worked out and will report on that in that other thread)

The resulting ShortcodeStorageTable plugin is on Github