Search code examples
phpmysqljoomla

Joomla Subform field


We have inherited a Joomla project and need to implement a repeating field for product images.

We're having trouble understanding how to implement this in the database. Is there some naming convention to tell Joomla what table/foreign keys to use?

We've got this in the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<field
    name="gallery_images"
    type="subform"
    label="Gallery Images"
    description="Images for the gallery"
    multiple="true"
    min="1"
    max="10"
    >
    <form>
        <field
            name="image_url"
            type="media"
            label="Image"
            />
    </form>
</field>

Then we have the is pulled into the form edit.php file like this:

<div class="control-group">
    <div class="control-label"><?php echo $this->form->getLabel('gallery_images'); ?></div>
    <div class="controls"><?php echo $this->form->getInput('gallery_images'); ?></div>
</div>

This actually all works perfectly on the backend end. But it doesn't save any where. We have tried making a field on the products table thinking maybe it would store as JSON. We've tried created another table for the images with a product_id on it thinking maybe Joomla just works out the foreign key?

The Joomla documentation doesn't seem to say anything about how it works in the DB. (https://docs.joomla.org/Subform_form_field_type)

Thanks in advance.


Solution

  • So since the gallery_images field was being saved into by joomla, we wrote some code to do this manually.

    class ModelProduct extends JModelAdmin
    {
        public function save($data) {
            if (parent::save($data)) {
                $registry = new Registry;
                $registry->loadArray($data['gallery_image']);
                $data['gallery_image'] = (string) $registry;
                $db = JFactory::getDbo();
                $query = $db->getQuery(true);
    
                // Fields to update.
                $fields = array(
                    $db->quoteName('gallery_image') . ' = ' . $db->quote($data['gallery_image']),
                );
    
                // Conditions for which records should be updated.
                $conditions = array(
                    $db->quoteName('id') . ' = '.$data['id'],
                );
    
                $query->update($db->quoteName('v4yn2_product'))->set($fields)->where($conditions);
                $db->setQuery($query);
                $result = $db->execute();
                return true;
            }
            return false;
        }
    }