3

I'm developing a component. I want to have the save as copy button appear next to each element on my front end list view.

I have copied the save as copy buttons html from the edit view of a single item and pasted the code in the list view:

<button onclick="Joomla.submitbutton('jj_pakke.save2copy');" class="btn btn-small button-save-copy">
<span class="icon-save-copy" aria-hidden="true"></span>
Save as Copy</button>

The button works, but it just creates new items with all fields empty.

How can I pass the item data with the "save2copy" method?

I tried using a form like this, but it didn't work:

<form action="<?php echo JRoute::_('index.php?option=com_pakker&view=jj_pakke&layout=edit&id=4'); ?>"
method="post" name="adminForm" id="adminForm">
<?php JFactory::getApplication()->input->post->set('data', $item); ?>
<input type="hidden" name="task" value="jj_pakke.save2copy" />
<?php echo JHtml::_('form.token'); ?>
<input type="submit">

I also did an enqueueMessage to see what the data is set to at the data method which have the save2copy task:

public function save($data)
{
    $encoded = json_encode($data);
    JFactory::getApplication()->enqueueMessage($encoded, 'warning');

The output is:

{"id":0,"tags":null}

Update

Inside the model, I got this method:

public function save($data)

Inside that method, there is the save2copy:

if ($input->get('task') === 'save2copy')
    {
        $origTable = clone $this->getTable();
        $origTable->load($input->getInt('id'));

        if ($data['jjpakketitel'] == $origTable->jjpakketitel)
        {
            list($jjpakketitel, $alias) = $this->_generateNewTitle($data['alias'], $data['jjpakketitel']);
            $data['jjpakketitel'] = $jjpakketitel;
            $data['alias'] = $alias;
        }
        else
        {
            if ($data['alias'] == $origTable->alias)
            {
                $data['alias'] = '';
            }
        }

        $data['published'] = 0;
    }
  • 1
    Good question. (I always upvote questions that show effort.) – mickmackusa Sep 25 at 11:23
  • I don't understand a lot of what's going on after reading your code above... What is $data - in this context? Where do you get it? What are you doing with it? – FFrewin Sep 27 at 13:08
  • I don´t know, but I will try to find out where it comes from. Above the method is says: * Method to save the form data. – JonasB Sep 27 at 13:14
  • But the $data must come form somewhere else. It´s the only apperence in the whole model php file – JonasB Sep 27 at 13:15
  • I don't understand... Is it a component you develop or you inherited from someone? The save2copy is something you added inside the save method? And it's a save method of what? I think you need a save2copy method, where you would prepare the data to be saved (duplicated) and pass them to the standard save method. Again I don't know how your component is build so it's only basic guidance on the approach. – FFrewin Sep 27 at 15:51
1

Based on what you have described in your question, I can only give you some basic guidelines at the moment.

I don't know how you are developing the component - but when you clone/duplicate a list item, then what you are actually doing is to duplicating a row in the database, creating a new one with a new primary key but same other values.

You need first to gather all the data of the row that you are duplicating.
By clicking the button you should have an id of that item to be copied. Use this id to gather all the remaining data of that row that you will use to create/save a new item.
You probably might have methods in your models to fetch an item object by ID or something similar.

Once you have the source data in your save2copy function, go ahead with saving a new item with that data.

I hope this helps you a little.

  • Thank you! That makes sense. But how do I pass the data to save2copy? is <?php JFactory::getApplication()->input->post->set('data', $item); ?> the correct way? – JonasB Sep 26 at 10:33
  • It's better to think it like you actually don't have item data in the list - you are on a different view (list) and you want to perform a task that is for item (save item). So the important thing you have is the id of the item you are duplicating that will give you access to data that you want to use for your new item. Use that ID and get the item's full data from the database, or your component's model if you have such methods - data that will be ready for a new item. @JonasB – FFrewin Sep 26 at 12:05
  • Thanks @FFrewin. I updated the question with the save2copy method. See above... I already loaded all the items by the ID. I got all the data loaded, I just don´t know how to get it along with the save2copy. The save2copy works, but only saves empty items – JonasB Sep 27 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.