1

I have a plugin which loads a custom form in a new tab inside the article edit view. So far, no problem with this part:

public function onContentPrepareForm(Form $form, $data)
{
    // Check we are manipulating a valid form.
    $name = $form->getName();
    
    if (!in_array($name, array('com_content.article')))
    {
        return true;
    }

    // Load the form
    Form::addFormPath(__DIR__ . '/forms');
    $form->loadFile('tagimageform');

    return true;
}

enter image description here

The problem comes when I try make this form save its values from the form to an already created new table that I have. I was thinking to use the following event:

public function onContentBeforeSave($context, $table, $isNew)
{
    // Check we are handling the article form.
    if ($context !== 'com_content.article')
    {
        return true;
    }
}

and there handle the actions to save the values to the table, but I can not find a way to do it so far.

Later will come the requirement to load the correct data in case a user wishes to edit the article, but this is not the most immediate concern.

Can someone explain the appropriate technique to use to save this custom form data.

1
  • Welcome to JSE. Please take our tour.
    – mickmackusa
    Oct 29 '20 at 20:09
1

SAVE THE DATA

I want to share how I have created the solution to my problem.

Along with the plugin there is a component than is relate with the work I doing, and both will work together. The component has other purposes, but taking advantage of the fact that much of the logic that the plugin needs, is already made or is reusable through the model classes, such as JModelAdmin class. With that said, I reworked my onContentPrepareForm function to:

public function onContentPrepareForm(Form $form, $data)
{
    // Check we are manipulating a valid form.
    $name = $form->getName();
    
    if (!in_array($name, array('com_content.article')))
    {
        return true;
    }
    
    // Load the form
    $model = $this->_getModel('Tagimageform');
    $model->loadFormFile('tagimageform', $form);

    return true;
}

The _getModel end like:

protected function _getModel($name, $prefix = 'TagimagesModel')
{
    // Add the models
    JModelLegacy::addIncludePath($this->_com_path . '/models', $prefix);

    // Extra paths where the model will look for
    Form::addFormPath($this->_com_path . '/models/forms');
    Table::addIncludePath($this->_com_path . '/tables');

    // Get an instance of the generic form model
    $model = JModelLegacy::getInstance($name, $prefix, array('ignore_request' => true));
    
    return $model;
}

So far, the result still the same, we get the form when we are at the article edit view: enter image description here

For save the values of the custom form, who is our goal, we used the onContentAfterSave event:

public function onContentAfterSave($context, $article, $isNew)
{
    // Check we are handling the article form.
    if ($context !== 'com_content.article')
    {
        return true;
    }
    
    $model  = $this->_getModel('Tagimageform');

    // Get the whole article form data including the custom form data
    $input      = Factory::getApplication()->input;
    $formData   = new Input($input->get('jform', '', 'array'));
    
    // Get only the custom form data we will save
    // Will be a array if was filled and null if not
    $data = $formData->getRaw('config');

    // Save the form data to the table
    if (is_array($data)) 
    {
        // Our custom form is a "subform" type field
        // We need save all the forms the user created because is a repeatable "subform"
        foreach ($data as $value) {
            try
            {
                $result = $model->save($value);
            }
            catch (\Exception $e)
            {
                Factory::getApplication()->enqueueMessage(
                    Text::sprintf('One of the form could not be saved', $value['cart'], 'error'));
            }
        }
    }       
}

And done, the new values were added to our table in the database successfully!

Now there are other actions such as loading the correct form/s if the user edits the article, but that is part of another story.

Thank you to @Benjamin Trenkle for the suggestion on Glip, of the corrects plugins events to work on.

2
  • See if you can encourage Benjamin to join us. :)
    – mickmackusa
    Oct 30 '20 at 7:37
  • That will be hard to do ;) Oct 30 '20 at 19:37

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.